|
| 1 | +--- |
| 2 | +title: Dispatched Events |
| 3 | +order: 5 |
| 4 | +--- |
| 5 | + |
| 6 | +# Dispatched Events |
| 7 | + |
| 8 | +Laravel WebSockets takes advantage of Laravel's Event dispatching observer, in a way that you can handle in-server events outside of it. |
| 9 | + |
| 10 | +For example, you can listen for events like when a new connection establishes or when an user joins a presence channel. |
| 11 | + |
| 12 | +## Events |
| 13 | + |
| 14 | +Below you will find a list of dispatched events: |
| 15 | + |
| 16 | +- `BeyondCode\LaravelWebSockets\Events\NewConnection` - when a connection successfully establishes on the server |
| 17 | +- `BeyondCode\LaravelWebSockets\Events\ConnectionClosed` - when a connection leaves the server |
| 18 | +- `BeyondCode\LaravelWebSockets\Events\SubscribedToChannel` - when a connection subscribes to a specific channel |
| 19 | +- `BeyondCode\LaravelWebSockets\Events\UnsubscribedFromChannel` - when a connection unsubscribes from a specific channel |
| 20 | +- `BeyondCode\LaravelWebSockets\Events\WebSocketMessageReceived` - when the server receives a message |
| 21 | +- `BeyondCode\LaravelWebSockets\EventsConnectionPonged` - when a connection pings to the server that it is still alive |
| 22 | + |
| 23 | +## Queued Listeners |
| 24 | + |
| 25 | +Because 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](non-blocking-queue-driver.md) documentation that helps you create the Async Redis queue driver that is going to fix the Blocking I/O issue. |
| 26 | + |
| 27 | +If set up, you can use the `async-redis` queue driver in your listeners: |
| 28 | + |
| 29 | +```php |
| 30 | +<?php |
| 31 | + |
| 32 | +namespace App\Listeners; |
| 33 | + |
| 34 | +use BeyondCode\LaravelWebSockets\Events\NewConnection; |
| 35 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 36 | + |
| 37 | +class HandleNewConnections implements ShouldQueue |
| 38 | +{ |
| 39 | + /** |
| 40 | + * The name of the connection the job should be sent to. |
| 41 | + * |
| 42 | + * @var string|null |
| 43 | + */ |
| 44 | + public $connection = 'async-redis'; |
| 45 | + |
| 46 | + /** |
| 47 | + * Create the event listener. |
| 48 | + * |
| 49 | + * @return void |
| 50 | + */ |
| 51 | + public function __construct() |
| 52 | + { |
| 53 | + // |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Handle the event. |
| 58 | + * |
| 59 | + * @param NewConnection $event |
| 60 | + * @return void |
| 61 | + */ |
| 62 | + public function handle(NewConnection $event) |
| 63 | + { |
| 64 | + // |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +The `EventServiceProvider` might look like this, registering the listeners that are going to be used by the event dispatching: |
| 70 | + |
| 71 | +```php |
| 72 | +/** |
| 73 | + * The event listener mappings for the application. |
| 74 | + * |
| 75 | + * @var array |
| 76 | + */ |
| 77 | +protected $listen = [ |
| 78 | + \BeyondCode\LaravelWebSockets\Events\NewConnection::class => [ |
| 79 | + App\Listeners\HandleNewConnections::class, |
| 80 | + ], |
| 81 | +]; |
| 82 | +``` |
0 commit comments