I am new in laravel so I googled a lot for different approaches how to create websocket with redis, socket.io in laravel framework. And finally my websocket works as I expected. However I still have unanswered questions related with websockets. Could you please help me find answer?
class TestEvent implements ShouldBroadcast
this class definition expects broadcastOn method that broadcasting channel or channels with data to the listeners. Listener in my case is server.js
redis.subscribe('test-channel', 'test-channel-new');
redis.on('message', function (channel, message) {..
as you can see, I want to subscribe two channels, but with different return values for each channel. And I have no luck find any explanation how it achieved. Have I create new event for each channel separately or there exist some trick using broadcastWith?
Thanks a lot
Socket servers usually were not that easy to setup, but Laravel Echo Server changes this. This is completely free, you only have to run the socket server yourself.
Pusher Channels acts as a realtime layer between your client and server events, maintaining persistent connections to the clients.
To inform Laravel that a given event should be broadcast, you must implement the Illuminate\Contracts\Broadcasting\ShouldBroadcast interface on the event class. This interface is already imported into all event classes generated by the framework so you may easily add it to any of your events.
It's very easy! just return arrays of Channels in broadcastOn method I just did this: Example:
public function broadcastOn()
{
return [
new PrivateChannel('App.Message.' . $this->message->to_user_id),
new PrivateChannel('App.Message.'. $this->message->to_user_id .'.From.'. $this->message->from_user_id)
];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With