al.mootasem's avatar

Custom broadcast connection\driver?

Recently I've been trying to implement bidirectional realtime communication in my app, I've tried many things like "pusher", what's so called pusher replacement beyondcode\laravel-websockets, "socket.io" and all the things you come into when researching websockets and realtime communication, however, it was difficult for me to use any of these options to achieve "bi-directionality" so I switched to a totally custom implementation (well.. not totally) where I use cboden/ratchet as a websocket server together with custom very simple front-end\client javascript code.

Now (naturally) broadcasting events like so event(new SomeEvent($data)) doesn't work anymore and I believe to get it back to work again I need to write my own broadcasting "driver \ connection" and my problem is that I don't know how to go about that and where to start, it's a whole new level for me :| so if any of you guys can be of any help it would be great! and thanks anyway!

0 likes
1 reply
Talinon's avatar

@al.mootasem

I think it would be just a matter of creating a new class that extends Illuminate\Broadcasting\Broadcasters\Broadcaster.php

That implements a contract where you will need to provide the logic behind the methods auth(), validAuthenticationResponse() and broadcast()

You can take a look at Illuminate\Broadcasting\Broadcasters\PusherBroadcaster.php for an example on how Pusher is handled.

Once you create your class, you're going to need to register it with the framework. A good place for this would be within a Service Provider. You could either create your own, or you can probably just use the app/Providers/BroadcastServiceProvider.php already provided.

To register it, you would need to add something like this:

public function boot(BroadcastManager $broadcastManager)
{
    $broadcastManager->extend('mybroadcaster', function (Application $app, array $config) {
        return new MyCustomBroadcaster();  // you can optionally pass $config information into the constructor for your class
    });
}

Then just add it to your config/broadcasting.php file:

'connections' => [

        'mybroadcaster' => [
            'driver' => 'mybroadcaster',  // same as first parameter in extend()
            'options' => [
                //
            ],
        ],
1 like

Please or to participate in this conversation.