2

I am having trouble making pusher work . I have followed the documentation but I donot know the problem is.. The console returns null.

public function broadcastOn()
{

    return new PrivateChannel('my-channel');
}

and here is my js for pusher.

 <script src="https://js.pusher.com/4.0/pusher.min.js"></script>
<script>
(function () {



    // Enable pusher logging - don't include this in production
    Pusher.logToConsole = true;

var pusher = new Pusher('6049410e84e42d918b14', { encrypted: true });

    var channel = pusher.subscribe('my-channel');

    channel.bind('\Dms\Events\NewNotification', addMessage);

    function addMessage(data) {
        var listItem = $("<li class='list-group-item'></li>");
        listItem.html(data.message);
        $('#messages').prepend(listItem);

        console.log(data.message)
    }

})()

Above is all the code that I have used as test. Please anyone who have done this assist. Laravel 5.4 is what am using right now. Below is the error code. enter image description here

1 Answer 1

1

In Laravel 5.4, Private channels have a prefix of private- added to it. So try changing this:

 var channel = pusher.subscribe('my-channel');

to this:

 var channel = pusher.subscribe('private-my-channel');

Secondly, check your string, you will need to escape backslashes. So '\Dms\Events\NewNotification' should be this: '\\Dms\\Events\\NewNotification'

Finally, I would recommend using Laravel Echo as it makes things really easy to work with Pusher and Laravel. With Echo, these two lines of code:

var channel = pusher.subscribe('my-channel');

channel.bind('\Dms\Events\NewNotification', addMessage);

Will look like this:

Echo.private('my-channel')
    .listen('\\Dms\\Events\\NewNotification', addMessage);

Notice you don't have to write the prefix private- anymore.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but now I have this kind of error Pusher : Couldn't get auth info from your webapp : 404 warn @ pusher.min.js:8 r.onreadystatechange @ pusher.min.js:8 pusher.min.js:8 Pusher : Couldn't get auth info from your webapp : 404 pusher.min.js:8 Pusher : No callbacks on private-test for pusher:subscription_error
@philipNjuguna Hmm, as this is a different error now, I suggest you ask a new StackOverflow question so people can answer for that error. If my current answer has solved the issue in your original question, I hope you can mark it as the accepted answer. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.