Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 545501d

Browse files
committed
Added events for sub/unsub/messages sent
1 parent 0596d1a commit 545501d

File tree

5 files changed

+172
-4
lines changed

5 files changed

+172
-4
lines changed

docs/advanced-usage/events.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: Triggered Events
3+
order: 4
4+
---
5+
6+
# Triggered Events
7+
8+
When an user subscribes or unsubscribes from a channel, a Laravel event gets triggered.
9+
10+
- Connection subscribed channel: `\BeyondCode\LaravelWebSockets\Events\Subscribed`
11+
- Connection left channel: `\BeyondCode\LaravelWebSockets\Events\Unsubscribed`
12+
13+
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.
14+
15+
```php
16+
/**
17+
* The event listener mappings for the application.
18+
*
19+
* @var array
20+
*/
21+
protected $listen = [
22+
'BeyondCode\LaravelWebSockets\Events\Subscribed' => [
23+
'App\Listeners\SomeListener',
24+
],
25+
];
26+
```
27+
28+
You will be provided the connection and the channel name through the event:
29+
30+
```php
31+
class SomeListener
32+
{
33+
public function handle($event)
34+
{
35+
// You can access:
36+
// $event->connection
37+
// $event->channelName
38+
39+
// You can also retrieve the app:
40+
$app = $event->connection->app;
41+
42+
// Or the socket ID:
43+
$socketId = $event->connection->socketId;
44+
}
45+
}
46+
```

src/Events/MessagesBroadcasted.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Events;
4+
5+
use Illuminate\Foundation\Events\Dispatchable;
6+
use Illuminate\Queue\SerializesModels;
7+
use Ratchet\ConnectionInterface;
8+
9+
class MessagesBroadcasted
10+
{
11+
use Dispatchable, SerializesModels;
12+
13+
/**
14+
* The amount of messages sent.
15+
*
16+
* @var int
17+
*/
18+
protected $sentMessagesCount;
19+
20+
/**
21+
* Initialize the event.
22+
*
23+
* @param int $sentMessagesCount
24+
* @return void
25+
*/
26+
public function __construct(int $sentMessagesCount = 0)
27+
{
28+
$this->sentMessagesCount = $sentMessagesCount;
29+
}
30+
}

src/Events/Subscribed.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Events;
4+
5+
use Illuminate\Foundation\Events\Dispatchable;
6+
use Illuminate\Queue\SerializesModels;
7+
use Ratchet\ConnectionInterface;
8+
9+
class Subscribed
10+
{
11+
use Dispatchable, SerializesModels;
12+
13+
/**
14+
* The channel name the user has subscribed to.
15+
*
16+
* @var string
17+
*/
18+
protected $channelName;
19+
20+
/**
21+
* The connection that initiated the subscription.
22+
*
23+
* @var \Ratchet\ConnectionInterface
24+
*/
25+
protected $connection;
26+
27+
/**
28+
* Initialize the event.
29+
*
30+
* @param string $channelName
31+
* @param \Ratchet\ConnectionInterface $connection
32+
* @return void
33+
*/
34+
public function __construct(string $channelName, ConnectionInterface $connection)
35+
{
36+
$this->channelName = $channelName;
37+
$this->connection = $connection;
38+
}
39+
}

src/Events/Unsubscribed.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Events;
4+
5+
use Illuminate\Foundation\Events\Dispatchable;
6+
use Illuminate\Queue\SerializesModels;
7+
use Ratchet\ConnectionInterface;
8+
9+
class Unsubscribed
10+
{
11+
use Dispatchable, SerializesModels;
12+
13+
/**
14+
* The channel name the user has unsubscribed from.
15+
*
16+
* @var string
17+
*/
18+
protected $channelName;
19+
20+
/**
21+
* The connection that initiated the unsubscription.
22+
*
23+
* @var \Ratchet\ConnectionInterface
24+
*/
25+
protected $connection;
26+
27+
/**
28+
* Initialize the event.
29+
*
30+
* @param string $channelName
31+
* @param \Ratchet\ConnectionInterface $connection
32+
* @return void
33+
*/
34+
public function __construct(string $channelName, ConnectionInterface $connection)
35+
{
36+
$this->channelName = $channelName;
37+
$this->connection = $connection;
38+
}
39+
}

src/WebSockets/Channels/Channel.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace BeyondCode\LaravelWebSockets\WebSockets\Channels;
44

55
use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger;
6+
use BeyondCode\LaravelWebSockets\Events\MessagesBroadcasted;
7+
use BeyondCode\LaravelWebSockets\Events\Subscribed;
8+
use BeyondCode\LaravelWebSockets\Events\Unsubscribed;
69
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
710
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature;
811
use Illuminate\Support\Str;
@@ -116,6 +119,8 @@ public function subscribe(ConnectionInterface $connection, stdClass $payload)
116119
]));
117120

118121
$this->replicator->subscribe($connection->app->id, $this->channelName);
122+
123+
Subscribed::dispatch($this->channelName, $connection);
119124
}
120125

121126
/**
@@ -136,6 +141,8 @@ public function unsubscribe(ConnectionInterface $connection)
136141
'channel' => $this->channelName,
137142
]);
138143
}
144+
145+
Unsubscribed::dispatch($this->channelName, $connection);
139146
}
140147

141148
/**
@@ -173,6 +180,8 @@ public function broadcast($payload)
173180
foreach ($this->subscribedConnections as $connection) {
174181
$connection->send(json_encode($payload));
175182
}
183+
184+
MessagesBroadcasted::dispatch(count($this->subscribedConnections));
176185
}
177186

178187
/**
@@ -217,11 +226,16 @@ public function broadcastToEveryoneExcept(stdClass $payload, ?string $socketId,
217226
return;
218227
}
219228

220-
foreach ($this->subscribedConnections as $connection) {
221-
if ($connection->socketId !== $socketId) {
222-
$connection->send(json_encode($payload));
223-
}
229+
$connections = collect($this->subscribedConnections)
230+
->reject(function ($connection) use ($socketId) {
231+
return $connection->socketId === $socketId;
232+
});
233+
234+
foreach ($connections as $connection) {
235+
$connection->send(json_encode($payload));
224236
}
237+
238+
MessagesBroadcasted::dispatch($connections->count());
225239
}
226240

227241
/**

0 commit comments

Comments
 (0)