Added events for sub/unsub/messages sent

This commit is contained in:
Alex Renoki 2020-09-02 17:16:34 +03:00
parent 0596d1ad48
commit 545501d575
5 changed files with 172 additions and 4 deletions

View File

@ -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;
}
}
```

View File

@ -0,0 +1,30 @@
<?php
namespace BeyondCode\LaravelWebSockets\Events;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Ratchet\ConnectionInterface;
class MessagesBroadcasted
{
use Dispatchable, SerializesModels;
/**
* The amount of messages sent.
*
* @var int
*/
protected $sentMessagesCount;
/**
* Initialize the event.
*
* @param int $sentMessagesCount
* @return void
*/
public function __construct(int $sentMessagesCount = 0)
{
$this->sentMessagesCount = $sentMessagesCount;
}
}

39
src/Events/Subscribed.php Normal file
View File

@ -0,0 +1,39 @@
<?php
namespace BeyondCode\LaravelWebSockets\Events;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Ratchet\ConnectionInterface;
class Subscribed
{
use Dispatchable, SerializesModels;
/**
* The channel name the user has subscribed to.
*
* @var string
*/
protected $channelName;
/**
* The connection that initiated the subscription.
*
* @var \Ratchet\ConnectionInterface
*/
protected $connection;
/**
* Initialize the event.
*
* @param string $channelName
* @param \Ratchet\ConnectionInterface $connection
* @return void
*/
public function __construct(string $channelName, ConnectionInterface $connection)
{
$this->channelName = $channelName;
$this->connection = $connection;
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace BeyondCode\LaravelWebSockets\Events;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Ratchet\ConnectionInterface;
class Unsubscribed
{
use Dispatchable, SerializesModels;
/**
* The channel name the user has unsubscribed from.
*
* @var string
*/
protected $channelName;
/**
* The connection that initiated the unsubscription.
*
* @var \Ratchet\ConnectionInterface
*/
protected $connection;
/**
* Initialize the event.
*
* @param string $channelName
* @param \Ratchet\ConnectionInterface $connection
* @return void
*/
public function __construct(string $channelName, ConnectionInterface $connection)
{
$this->channelName = $channelName;
$this->connection = $connection;
}
}

View File

@ -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());
}
/**