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

Commit a897ce2

Browse files
committed
Fixed backend streaming
1 parent 1877819 commit a897ce2

File tree

11 files changed

+46
-252
lines changed

11 files changed

+46
-252
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.

src/Contracts/PushesToPusher.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@ trait PushesToPusher
1616
*/
1717
public function getPusherBroadcaster(array $app)
1818
{
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-
2719
return new PusherBroadcaster(
28-
new Pusher($app['key'], $app['secret'], $app['id'], config('broadcasting.connections.pusher.options', []))
20+
new Pusher(
21+
$app['key'],
22+
$app['secret'],
23+
$app['id'],
24+
config('broadcasting.connections.pusher.options', [])
25+
)
2926
);
3027
}
3128
}

src/HttpApi/Controllers/Controller.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use React\Promise\PromiseInterface;
2020
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
2121
use Symfony\Component\HttpKernel\Exception\HttpException;
22+
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
2223

2324
abstract class Controller implements HttpServerInterface
2425
{
@@ -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 & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,6 @@
1313

1414
class FetchChannelsController extends Controller
1515
{
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-
3716
/**
3817
* Handle the incoming request.
3918
*

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
}

src/WebSocketsServiceProvider.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
99
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowStatistics;
1010
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
11-
use BeyondCode\LaravelWebSockets\PubSub\Broadcasters\RedisPusherBroadcaster;
1211
use BeyondCode\LaravelWebSockets\Server\Router;
1312
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
1413
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
@@ -47,8 +46,6 @@ public function boot()
4746
Console\CleanStatistics::class,
4847
Console\RestartWebSocketServer::class,
4948
]);
50-
51-
$this->configurePubSub();
5249
}
5350

5451
/**
@@ -85,31 +82,6 @@ public function register()
8582
});
8683
}
8784

88-
/**
89-
* Configure the PubSub replication.
90-
*
91-
* @return void
92-
*/
93-
protected function configurePubSub()
94-
{
95-
$this->app->make(BroadcastManager::class)->extend('websockets', function ($app, array $config) {
96-
$pusher = new Pusher(
97-
$config['key'], $config['secret'],
98-
$config['app_id'], $config['options'] ?? []
99-
);
100-
101-
if ($config['log'] ?? false) {
102-
$pusher->setLogger($this->app->make(LoggerInterface::class));
103-
}
104-
105-
return new RedisPusherBroadcaster(
106-
$pusher,
107-
$config['app_id'],
108-
$this->app->make('redis')
109-
);
110-
});
111-
}
112-
11385
/**
11486
* Register the dashboard routes.
11587
*

tests/Dashboard/SendMessageTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public function can_send_message_on_redis_replication()
5050
'data' => json_encode(['data' => 'yes']),
5151
])
5252
->seeJson([
53-
'ok' => true,
53+
'exception' => 'Failed to connect to Pusher.',
54+
'ok' => false,
5455
]);
5556
}
5657

0 commit comments

Comments
 (0)