Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel Echo does not listen to channel and events

To broadcast an event on a channel I used laravel Echo , redis and socket-io.

this is my event :

    class ChatNewMessage implements ShouldBroadcast
    {
        use InteractsWithSockets, SerializesModels;

        public $targetUsers;
        public $message;

        public function __construct ($message)
        {
            $this->targetUsers = $message->chat->users->pluck('user_id');
            $this->message     = $message;
        }

        public function broadcastOn ()
        {
            $userChannels = [];
            foreach ($this->targetUsers as $id) {
                $userChannels[] = 'user-channel.' . $id;
            }
            return $userChannels;

        }

        public function broadcastAs()
        {
            return 'NewMessage';
        }


    }

For Socket.io server I'm using laravel-echo-server and below is configuration:

{
    "authHost": "http://api.pars-app.dev",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "1924bf1d59b3759d",
            "key": "eaf9e3c843493421f4be488fba11f49d"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": "127.0.0.1",
    "port": "3000",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": ""
} 

And this is Echo structure in bootstrap.js file :

import Echo from "laravel-echo";
window.echo = new Echo({
    broadcaster: 'socket.io',
    host: 'http://127.0.0.1:3000',
    auth: {
        headers: {
            Authorization: 'bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjUsImlzcyI6Imh0dHA6XC9cL2FwaS5wYXJzLWFwcC5kZXZcL3YxXC9hdXRoXC9zaWduSW4iLCJpYXQiOjE0ODkzMTY1ODgsImV4cCI6MTQ5MTkwNDk4OCwibmJmIjoxNDg5MzE2NTg4LCJqdGkiOiIwNzdiN2NjYjNlODE4MDU2NjBiMWNjYTkzZjI1ZTZmMyJ9.Y_BvVnJIq8k3uN1sQQuBna1CcLsrn7VnpH3RxXS8JEQ'
        }
    }
});
echo.channel('user-channel.5').listen('NewMessage', (e) => {
    console.log(e);
});

After running laravel-echo-server and call event, below logs show in console that shows event broadcasts to channel :

[2:26:05 PM] - Xt_VSaSRHirHHxtUAAAE left channel: user-channel.5 (transport close)
[2:26:06 PM] - FwfOFMZqfrI9_H11AAAF joined channel: user-channel.5
Channel: user-channel.2
Event: NewMessage
CHANNEL user-channel.2
Channel: user-channel.1
Event: NewMessage
CHANNEL user-channel.1
Channel: user-channel.5
Event: NewMessage
CHANNEL user-channel.5

But Echo could not listen to channels and occurred events.

What is problem?

like image 646
A.B.Developer Avatar asked Mar 28 '17 10:03

A.B.Developer


1 Answers

I found the solution.

According to this comment on laravel-echo-server github page,The event name needs to be prefixed with a dot. like this :

echo.channel('user-channel.5').listen('.NewMessage', (e) => {
    console.log(e);
});
like image 198
A.B.Developer Avatar answered Nov 20 '22 06:11

A.B.Developer