Added events for sub/unsub/messages sent
This commit is contained in:
parent
0596d1ad48
commit
545501d575
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,9 @@
|
||||||
namespace BeyondCode\LaravelWebSockets\WebSockets\Channels;
|
namespace BeyondCode\LaravelWebSockets\WebSockets\Channels;
|
||||||
|
|
||||||
use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger;
|
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\PubSub\ReplicationInterface;
|
||||||
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature;
|
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
@ -116,6 +119,8 @@ class Channel
|
||||||
]));
|
]));
|
||||||
|
|
||||||
$this->replicator->subscribe($connection->app->id, $this->channelName);
|
$this->replicator->subscribe($connection->app->id, $this->channelName);
|
||||||
|
|
||||||
|
Subscribed::dispatch($this->channelName, $connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -136,6 +141,8 @@ class Channel
|
||||||
'channel' => $this->channelName,
|
'channel' => $this->channelName,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Unsubscribed::dispatch($this->channelName, $connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -173,6 +180,8 @@ class Channel
|
||||||
foreach ($this->subscribedConnections as $connection) {
|
foreach ($this->subscribedConnections as $connection) {
|
||||||
$connection->send(json_encode($payload));
|
$connection->send(json_encode($payload));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MessagesBroadcasted::dispatch(count($this->subscribedConnections));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -217,11 +226,16 @@ class Channel
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->subscribedConnections as $connection) {
|
$connections = collect($this->subscribedConnections)
|
||||||
if ($connection->socketId !== $socketId) {
|
->reject(function ($connection) use ($socketId) {
|
||||||
|
return $connection->socketId === $socketId;
|
||||||
|
});
|
||||||
|
|
||||||
|
foreach ($connections as $connection) {
|
||||||
$connection->send(json_encode($payload));
|
$connection->send(json_encode($payload));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
MessagesBroadcasted::dispatch($connections->count());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue