From 545501d5756a352b8e47b9f47677b8962f4022ad Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Wed, 2 Sep 2020 17:16:34 +0300 Subject: [PATCH] Added events for sub/unsub/messages sent --- docs/advanced-usage/events.md | 46 +++++++++++++++++++++++++++++ src/Events/MessagesBroadcasted.php | 30 +++++++++++++++++++ src/Events/Subscribed.php | 39 ++++++++++++++++++++++++ src/Events/Unsubscribed.php | 39 ++++++++++++++++++++++++ src/WebSockets/Channels/Channel.php | 22 +++++++++++--- 5 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 docs/advanced-usage/events.md create mode 100644 src/Events/MessagesBroadcasted.php create mode 100644 src/Events/Subscribed.php create mode 100644 src/Events/Unsubscribed.php diff --git a/docs/advanced-usage/events.md b/docs/advanced-usage/events.md new file mode 100644 index 0000000..7e8ba3a --- /dev/null +++ b/docs/advanced-usage/events.md @@ -0,0 +1,46 @@ +--- +title: Triggered Events +order: 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](https://laravel.com/docs/7.x/events#registering-events-and-listeners) and attaching Listeners to them. + +```php +/** + * 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: + +```php +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; + } +} +``` diff --git a/src/Events/MessagesBroadcasted.php b/src/Events/MessagesBroadcasted.php new file mode 100644 index 0000000..4503164 --- /dev/null +++ b/src/Events/MessagesBroadcasted.php @@ -0,0 +1,30 @@ +sentMessagesCount = $sentMessagesCount; + } +} diff --git a/src/Events/Subscribed.php b/src/Events/Subscribed.php new file mode 100644 index 0000000..9bdae48 --- /dev/null +++ b/src/Events/Subscribed.php @@ -0,0 +1,39 @@ +channelName = $channelName; + $this->connection = $connection; + } +} diff --git a/src/Events/Unsubscribed.php b/src/Events/Unsubscribed.php new file mode 100644 index 0000000..66c412a --- /dev/null +++ b/src/Events/Unsubscribed.php @@ -0,0 +1,39 @@ +channelName = $channelName; + $this->connection = $connection; + } +} diff --git a/src/WebSockets/Channels/Channel.php b/src/WebSockets/Channels/Channel.php index 2828d8a..c282563 100644 --- a/src/WebSockets/Channels/Channel.php +++ b/src/WebSockets/Channels/Channel.php @@ -3,6 +3,9 @@ namespace BeyondCode\LaravelWebSockets\WebSockets\Channels; use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger; +use BeyondCode\LaravelWebSockets\Events\MessagesBroadcasted; +use BeyondCode\LaravelWebSockets\Events\Subscribed; +use BeyondCode\LaravelWebSockets\Events\Unsubscribed; use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface; use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature; use Illuminate\Support\Str; @@ -116,6 +119,8 @@ class Channel ])); $this->replicator->subscribe($connection->app->id, $this->channelName); + + Subscribed::dispatch($this->channelName, $connection); } /** @@ -136,6 +141,8 @@ class Channel 'channel' => $this->channelName, ]); } + + Unsubscribed::dispatch($this->channelName, $connection); } /** @@ -173,6 +180,8 @@ class Channel foreach ($this->subscribedConnections as $connection) { $connection->send(json_encode($payload)); } + + MessagesBroadcasted::dispatch(count($this->subscribedConnections)); } /** @@ -217,11 +226,16 @@ class Channel return; } - foreach ($this->subscribedConnections as $connection) { - if ($connection->socketId !== $socketId) { - $connection->send(json_encode($payload)); - } + $connections = collect($this->subscribedConnections) + ->reject(function ($connection) use ($socketId) { + return $connection->socketId === $socketId; + }); + + foreach ($connections as $connection) { + $connection->send(json_encode($payload)); } + + MessagesBroadcasted::dispatch($connections->count()); } /**