docs/advanced-usage/dispatched-events.md
Laravel WebSockets takes advantage of Laravel's Event dispatching observer, in a way that you can handle in-server events outside of it.
For example, you can listen for events like when a new connection establishes or when an user joins a presence channel.
Below you will find a list of dispatched events:
BeyondCode\LaravelWebSockets\Events\NewConnection - when a connection successfully establishes on the serverBeyondCode\LaravelWebSockets\Events\ConnectionClosed - when a connection leaves the serverBeyondCode\LaravelWebSockets\Events\SubscribedToChannel - when a connection subscribes to a specific channelBeyondCode\LaravelWebSockets\Events\UnsubscribedFromChannel - when a connection unsubscribes from a specific channelBeyondCode\LaravelWebSockets\Events\WebSocketMessageReceived - when the server receives a messageBeyondCode\LaravelWebSockets\EventsConnectionPonged - when a connection pings to the server that it is still aliveBecause the default Redis connection (either PhpRedis or Predis) is a blocking I/O method and can cause problems with the server speed and availability, you might want to check the Non-Blocking Queue Driver documentation that helps you create the Async Redis queue driver that is going to fix the Blocking I/O issue.
If set up, you can use the async-redis queue driver in your listeners:
<?php
namespace App\Listeners;
use BeyondCode\LaravelWebSockets\Events\NewConnection;
use Illuminate\Contracts\Queue\ShouldQueue;
class HandleNewConnections implements ShouldQueue
{
/**
* The name of the connection the job should be sent to.
*
* @var string|null
*/
public $connection = 'async-redis';
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param NewConnection $event
* @return void
*/
public function handle(NewConnection $event)
{
//
}
}
The EventServiceProvider might look like this, registering the listeners that are going to be used by the event dispatching:
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
\BeyondCode\LaravelWebSockets\Events\NewConnection::class => [
App\Listeners\HandleNewConnections::class,
],
];