arnislielturks/socketio-laravel-broadcaster

Socket.io laravel broadcaster

Downloads

125

Stars

3

Version

0.3.1

socketio-laravel-broadcaster

This package allows to send Laravel events directly to the socket.io server

Installation

  1. Install the package via composer:
composer require arnislielturks/socketio-laravel-broadcaster
  1. Register the provider in config/app.php
// 'providers' => [
   ...
   
   ArnisLielturks\SocketIOBroadcaster\SocketIOBRoadcasterProvider::class
// ];
  1. Add configuration file (config/socketio.php) with the following content. This should point to the socket.io service
return [
    'server' => 'http://127.0.0.1:3000'
];
  1. Change the broadcast driver to socketio in (config/broadcasting.php
default' => env('BROADCAST_DRIVER', 'socketio'),

OR set this value in .env file

BROADCAST_DRIVER=socketio
  1. Update config/broadcasting.php
'connections' => [
        ...
        
        'socketio' => [
            'driver' => 'socketio'
        ]

    ],
  1. Create event which will send out the broadcast via Socket.io service
class TestEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    //All public attributes will be sent with the message
    public $id;
    public $event = 'test_event';

    public function __construct()
    {
        $this->id = 123;
    }

    public function broadcastOn()
    {
        //List of channels where this event should be sent
        return ['/test_event'];
    }

}
  1. Send out the event via Controller
class TestController extends Controller
{
    public function test() {
        event(new TestEvent());
        return view('main');
    }
}
  1. Outgoing message to socketio will look like this:
{ 
  id: 123,
  event: 'test_event'
}

That's it!

ArnisLielturks

Author

ArnisLielturks