sockets - laravel Echo does not listen to channel and events -
to broadcast event on channel used laravel echo , redis , socket-io.
this 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 $id) { $userchannels[] = 'user-channel.' . $id; } return $userchannels; } public function broadcastas() { return 'newmessage'; } }
for socket.io server i'm using laravel-echo-server , below 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 echo structure in bootstrap.js
file :
import echo "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 , call event, below logs show in console shows event broadcasts 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 not listen channels , occurred events.
what problem?
i found solution.
according this comment on laravel-echo-server github page,the event name needs prefixed dot. :
echo.channel('user-channel.5').listen('.newmessage', (e) => { console.log(e); });
Comments
Post a Comment