Laravel Echo does not received message from channel

Viewed 48

I stuck very bad . in real-time laravel(version 9) project using pusher for websocket. backend can get message but problem is Echo does not received message which sent from backend ...

Event :

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

    public $message ;
    public function __construct($message){
        $this->message=$message;
    }

    public function broadcastOn()
    {
        \Log::debug("{$this->message}");
        return new Channel('chat');
    }

    public function broadcatAs(){
        return 'chat';
    }
}

JS :

 Echo.channel('.chat').listen('MessageSent', (e) => {
        alert(e.message)
    });

further information :

1- according to Event I can log message to laravel.log file which means message sent from frontend to backend .

2-there is no error in console area

3-see debug console of pusher official site

debug console

1 Answers

There are 3 issues I believe. The first is you should not need to do .chat in your channel function on the frontend. The second is you mispelled broadcastAs function as broadcatAs customizes the event that is being listened for in the frontend. However, because it is mispelled you are referring to the event correctly though if you update it you will then need to change your listener to .chat . The last issue is you may not have started your queue. You need to run a daemon to handle that.

Echo.channel('chat').listen('MessageSent', (e) => {
    alert(e.message)
});

however, if you update your broadcastAs spelling correctly it will need to be

Echo.channel('chat').listen('chat', (e) => {
    alert(e.message)
});
Related