This package allows to send Laravel events directly to the socket.io server
- 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!