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

Commit b39b33c

Browse files
authored
Merge pull request #490 from beyondcode/fix/backend-streaming
[2.x] Removed 'websockets' broadcaster driver
2 parents 8baf234 + fe01abd commit b39b33c

File tree

12 files changed

+45
-267
lines changed

12 files changed

+45
-267
lines changed

docs/horizontal-scaling/getting-started.md

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,8 @@ To enable the replication, simply change the `replication.driver` name in the `w
2727
],
2828
```
2929

30+
Now, when your app broadcasts the message, it will make sure the connection reaches other servers which are under the same load balancer.
31+
3032
The available drivers for replication are:
3133

3234
- [Redis](redis)
33-
34-
## Configure the Broadcasting driver
35-
36-
Laravel WebSockets comes with an additional `websockets` broadcaster driver that accepts configurations like the Pusher driver, but will make sure the broadcasting will work across all websocket servers:
37-
38-
```php
39-
'connections' => [
40-
'pusher' => [
41-
...
42-
],
43-
44-
'websockets' => [
45-
'driver' => 'websockets',
46-
'key' => env('PUSHER_APP_KEY'),
47-
'secret' => env('PUSHER_APP_SECRET'),
48-
'app_id' => env('PUSHER_APP_ID'),
49-
'options' => [
50-
'cluster' => env('PUSHER_APP_CLUSTER'),
51-
'encrypted' => true,
52-
'host' => env('PUSHER_APP_HOST', '127.0.0.1'),
53-
'port' => env('PUSHER_APP_PORT', 6001),
54-
'scheme' => env('PUSHER_APP_SCHEME', 'http'),
55-
'curl_options' => [
56-
CURLOPT_SSL_VERIFYHOST => 0,
57-
CURLOPT_SSL_VERIFYPEER => 0,
58-
],
59-
],
60-
],
61-
```
62-
63-
Make sure to change the `BROADCAST_DRIVER`:
64-
65-
```
66-
BROADCAST_DRIVER=websockets
67-
```
68-
69-
Now, when your app broadcasts the message, it will make sure the connection reaches other servers which are under the same load balancer.

resources/views/dashboard.blade.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,7 @@ class="rounded-full px-3 py-1 inline-block text-sm"
405405
406406
axios
407407
.post('/event', payload)
408-
.then(() => {
409-
this.form = {
410-
channel: null,
411-
event: null,
412-
data: null,
413-
};
414-
})
408+
.then(() => {})
415409
.catch(err => {
416410
alert('Error sending event.');
417411
})

src/Contracts/PushesToPusher.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace BeyondCode\LaravelWebSockets\Contracts;
44

5-
use BeyondCode\LaravelWebSockets\PubSub\Broadcasters\RedisPusherBroadcaster;
65
use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster;
76
use Pusher\Pusher;
87

@@ -16,16 +15,13 @@ trait PushesToPusher
1615
*/
1716
public function getPusherBroadcaster(array $app)
1817
{
19-
if (config('websockets.replication.driver') === 'redis') {
20-
return new RedisPusherBroadcaster(
21-
new Pusher($app['key'], $app['secret'], $app['id'], config('broadcasting.connections.websockets.options', [])),
22-
$app['id'],
23-
app('redis')
24-
);
25-
}
26-
2718
return new PusherBroadcaster(
28-
new Pusher($app['key'], $app['secret'], $app['id'], config('broadcasting.connections.pusher.options', []))
19+
new Pusher(
20+
$app['key'],
21+
$app['secret'],
22+
$app['id'],
23+
config('broadcasting.connections.pusher.options', [])
24+
)
2925
);
3026
}
3127
}

src/HttpApi/Controllers/Controller.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers;
44

55
use BeyondCode\LaravelWebSockets\Apps\App;
6+
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
67
use BeyondCode\LaravelWebSockets\QueryParameters;
78
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
89
use Exception;
@@ -51,15 +52,24 @@ abstract class Controller implements HttpServerInterface
5152
*/
5253
protected $channelManager;
5354

55+
/**
56+
* The replicator driver.
57+
*
58+
* @var \BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface
59+
*/
60+
protected $replicator;
61+
5462
/**
5563
* Initialize the request.
5664
*
5765
* @param \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager $channelManager
66+
* @param \BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface $replicator
5867
* @return void
5968
*/
60-
public function __construct(ChannelManager $channelManager)
69+
public function __construct(ChannelManager $channelManager, ReplicationInterface $replicator)
6170
{
6271
$this->channelManager = $channelManager;
72+
$this->replicator = $replicator;
6373
}
6474

6575
/**

src/HttpApi/Controllers/FetchChannelsController.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers;
44

5-
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
6-
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
75
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel;
86
use Illuminate\Http\Request;
97
use Illuminate\Support\Collection;
@@ -13,27 +11,6 @@
1311

1412
class FetchChannelsController extends Controller
1513
{
16-
/**
17-
* The replicator driver.
18-
*
19-
* @var \BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface
20-
*/
21-
protected $replicator;
22-
23-
/**
24-
* Initialize the class.
25-
*
26-
* @param \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager $channelManager
27-
* @param \BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface $replicator
28-
* @return void
29-
*/
30-
public function __construct(ChannelManager $channelManager, ReplicationInterface $replicator)
31-
{
32-
parent::__construct($channelManager);
33-
34-
$this->replicator = $replicator;
35-
}
36-
3714
/**
3815
* Handle the incoming request.
3916
*

src/HttpApi/Controllers/TriggerEventController.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,26 @@ public function __invoke(Request $request)
1818
{
1919
$this->ensureValidSignature($request);
2020

21-
foreach ($request->json()->get('channels', []) as $channelName) {
21+
$channels = $request->channels ?: [];
22+
23+
foreach ($channels as $channelName) {
2224
$channel = $this->channelManager->find($request->appId, $channelName);
2325

24-
optional($channel)->broadcastToEveryoneExcept((object) [
26+
$payload = (object) [
2527
'channel' => $channelName,
26-
'event' => $request->json()->get('name'),
27-
'data' => $request->json()->get('data'),
28-
], $request->json()->get('socket_id'), $request->appId);
28+
'event' => $request->name,
29+
'data' => $request->data,
30+
];
31+
32+
optional($channel)->broadcastToEveryoneExcept($payload, $request->socket_id, $request->appId);
33+
34+
// If the setup is horizontally-scaled using the Redis Pub/Sub,
35+
// then we're going to make sure it gets streamed to the other
36+
// servers as well that are subscribed to the Pub/Sub topics
37+
// attached to the current iterated app & channel.
38+
// For local setups, the local driver will ignore the publishes.
39+
40+
$this->replicator->publish($request->appId, $channelName, $payload);
2941

3042
DashboardLogger::log($request->appId, DashboardLogger::TYPE_API_MESSAGE, [
3143
'channel' => $channelName,

src/PubSub/Broadcasters/RedisPusherBroadcaster.php

Lines changed: 0 additions & 142 deletions
This file was deleted.

src/PubSub/Drivers/RedisClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,23 +293,23 @@ public function onMessage(string $redisChannel, string $payload)
293293
return;
294294
}
295295

296-
$socket = $payload->socket ?? null;
296+
$socketId = $payload->socketId ?? null;
297297
$serverId = $payload->serverId ?? null;
298298

299299
// Remove fields intended for internal use from the payload.
300-
unset($payload->socket);
300+
unset($payload->socketId);
301301
unset($payload->serverId);
302302
unset($payload->appId);
303303

304304
// Push the message out to connected websocket clients.
305-
$channel->broadcastToEveryoneExcept($payload, $socket, $appId, false);
305+
$channel->broadcastToEveryoneExcept($payload, $socketId, $appId, false);
306306

307307
DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_MESSAGE_RECEIVED, [
308308
'channel' => $channel->getChannelName(),
309309
'redisChannel' => $redisChannel,
310310
'serverId' => $this->getServerId(),
311311
'incomingServerId' => $serverId,
312-
'incomingSocketId' => $socket,
312+
'incomingSocketId' => $socketId,
313313
'payload' => $payload,
314314
]);
315315
}

0 commit comments

Comments
 (0)