laravel-websockets/docs/advanced-usage/events.md

1.1 KiB

title order
Triggered Events 4

Triggered Events

When an user subscribes or unsubscribes from a channel, a Laravel event gets triggered.

  • Connection subscribed channel: \BeyondCode\LaravelWebSockets\Events\Subscribed
  • Connection left channel: \BeyondCode\LaravelWebSockets\Events\Unsubscribed

You can listen to them by registering them in the EventServiceProvider and attaching Listeners to them.

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'BeyondCode\LaravelWebSockets\Events\Subscribed' => [
        'App\Listeners\SomeListener',
    ],
];

You will be provided the connection and the channel name through the event:

class SomeListener
{
    public function handle($event)
    {
        // You can access:
        // $event->connection
        // $event->channelName

        // You can also retrieve the app:
        $app = $event->connection->app;

        // Or the socket ID:
        $socketId = $event->connection->socketId;
    }
}