19

I am using Pusher API for sending notifications in Laravel 5.4 with JQuery. I found out that whenever I send notification from Firefox or Safari...it reaches chrome browser successfully but not vice versa. Another problem is that when I send message, it is being received by me!!! Although I used toOthers() method

My Code is below. Please let me know if you need more info.

Controller Code

broadcast(new SendMessageEvent("hi", \Auth::user()))->toOthers();

Blade

$(function() {
    Pusher.logToConsole = true;
    var pusher = new Pusher('Pusher API key', {
        authEndpoint: 'broadcasting/auth',
        auth: {headers: {'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')}}
    });
    var channel = pusher.subscribe('private-user-channel.{!! \Auth::user()->UserID !!}');
    channel.bind('App\\Events\\SendMessageEvent', function(data) {
        console.log(data);
    });
});

Event

class SendMessageEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $msg;
    public $user;
    public $userid;

    public function __construct($msg, $user) {
        $this->msg = $msg;
        $this->user = $user;
        $this->userid = $this->user->UserID;
    }

    public function broadcastOn()
    {
        return new PrivateChannel('user-channel.1');
    }
}

Channel

Broadcast::channel('user-channel.{userid}', function ($user, $userid) {
    return (int) $user->UserID === (int) $userid;
});
sayan saha
  • 91
  • 2
  • 8
Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • 3
    From the documentation: “In Laravel 5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.” More information: https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0 – Martin Bean Apr 07 '17 at 23:00
  • 2
    i think on your `SendMessageEvent` class method `broadcastOn`, you need to change it like this: `return new PrivateChannel('user-channel.'.$this->userid);` – elegisandi Jul 31 '17 at 06:05
  • question still open? – cre8 Aug 03 '17 at 14:17
  • Yes me too. I can send notification from firefox to chrome (chrome catch the push notification) but not chrome to firefox (can't catch it) . – Goper Leo Zosa Dec 04 '17 at 03:03
  • can i ask why are you send sending notification through controller? – phpdroid Nov 18 '19 at 09:42

3 Answers3

1

I found this while searching, if the question is still open :

your event broadcasts everything to userID 1

 return new PrivateChannel('user-channel.1');

but your other users connect to their own channel with their own userID. Change to public channel and test again with your code, the rest seems to be correct.

BitBay
  • 17
  • 5
1

you should probably use Laravel Echo, which works better than raw pusher, and works with private channels and whatever push notification you got from Laravel :

    Echo.private('{{'App.User.'.Auth::id()}}')
                    .listen('.notify', (e) => {
                        ...
                    })                  
                .notification((notification) => {
                    ...
                });
Shuyi
  • 906
  • 2
  • 13
  • 28
1

I'm not sure about other browser but I know toOthers() issue (For VueJs)

If you are using Vue and Axios, the socket ID will automatically be attached to every outgoing request as a X-Socket-ID header. Then, when you call the toOthers method Only to others

If you want to set X-Socket-ID manually Set request header

bhavinjr
  • 1,663
  • 13
  • 19