Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

broadcasting laravel event and multiple channels

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

like image 705
Kyre Avatar asked Dec 15 '18 22:12

Kyre


People also ask

Is Laravel echo free?

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.

What is pusher in Laravel?

Pusher Channels acts as a realtime layer between your client and server events, maintaining persistent connections to the clients.

How do I broadcast an event in Laravel?

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.


1 Answers

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)
    ];
}
like image 69
Riajul Avatar answered Sep 21 '22 17:09

Riajul