arnislielturks / socketio-laravel-broadcaster
Socket.io laravel broadcaster
0.3.1
2020-05-14 08:03 UTC
Requires
- php: >=7.0
- illuminate/broadcasting: ~5 | ~6 | ~7
- illuminate/support: ~5 | ~6 | ~7
- wisembly/elephant.io: ^3.3
This package is auto-updated.
Last update: 2024-11-06 18:49:50 UTC
README
This package allows to send Laravel events directly to the socket.io server
Installation
- Install the package via composer:
composer require arnislielturks/socketio-laravel-broadcaster
- Register the provider in config/app.php
// 'providers' => [ ... ArnisLielturks\SocketIOBroadcaster\SocketIOBRoadcasterProvider::class // ];
- 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' ];
- 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
- Update config/broadcasting.php
'connections' => [
...
'socketio' => [
'driver' => 'socketio'
]
],
- 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']; } }
- Send out the event via Controller
class TestController extends Controller { public function test() { event(new TestEvent()); return view('main'); } }
- Outgoing message to socketio will look like this:
{ id: 123, event: 'test_event' }
That's it!