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

[2.x] Removed 'websockets' broadcaster driver #490

Merged
merged 4 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 2 additions & 37 deletions docs/horizontal-scaling/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,8 @@ To enable the replication, simply change the `replication.driver` name in the `w
],
```

Now, when your app broadcasts the message, it will make sure the connection reaches other servers which are under the same load balancer.

The available drivers for replication are:

- [Redis](redis)

## Configure the Broadcasting driver

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:

```php
'connections' => [
'pusher' => [
...
],

'websockets' => [
'driver' => 'websockets',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'encrypted' => true,
'host' => env('PUSHER_APP_HOST', '127.0.0.1'),
'port' => env('PUSHER_APP_PORT', 6001),
'scheme' => env('PUSHER_APP_SCHEME', 'http'),
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
],
],
],
```

Make sure to change the `BROADCAST_DRIVER`:

```
BROADCAST_DRIVER=websockets
```

Now, when your app broadcasts the message, it will make sure the connection reaches other servers which are under the same load balancer.
8 changes: 1 addition & 7 deletions resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,7 @@ class="rounded-full px-3 py-1 inline-block text-sm"

axios
.post('/event', payload)
.then(() => {
this.form = {
channel: null,
event: null,
data: null,
};
})
.then(() => {})
.catch(err => {
alert('Error sending event.');
})
Expand Down
16 changes: 6 additions & 10 deletions src/Contracts/PushesToPusher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace BeyondCode\LaravelWebSockets\Contracts;

use BeyondCode\LaravelWebSockets\PubSub\Broadcasters\RedisPusherBroadcaster;
use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster;
use Pusher\Pusher;

Expand All @@ -16,16 +15,13 @@ trait PushesToPusher
*/
public function getPusherBroadcaster(array $app)
{
if (config('websockets.replication.driver') === 'redis') {
return new RedisPusherBroadcaster(
new Pusher($app['key'], $app['secret'], $app['id'], config('broadcasting.connections.websockets.options', [])),
$app['id'],
app('redis')
);
}

return new PusherBroadcaster(
new Pusher($app['key'], $app['secret'], $app['id'], config('broadcasting.connections.pusher.options', []))
new Pusher(
$app['key'],
$app['secret'],
$app['id'],
config('broadcasting.connections.pusher.options', [])
)
);
}
}
12 changes: 11 additions & 1 deletion src/HttpApi/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers;

use BeyondCode\LaravelWebSockets\Apps\App;
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
use BeyondCode\LaravelWebSockets\QueryParameters;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use Exception;
Expand Down Expand Up @@ -51,15 +52,24 @@ abstract class Controller implements HttpServerInterface
*/
protected $channelManager;

/**
* The replicator driver.
*
* @var \BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface
*/
protected $replicator;

/**
* Initialize the request.
*
* @param \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager $channelManager
* @param \BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface $replicator
* @return void
*/
public function __construct(ChannelManager $channelManager)
public function __construct(ChannelManager $channelManager, ReplicationInterface $replicator)
{
$this->channelManager = $channelManager;
$this->replicator = $replicator;
}

/**
Expand Down
23 changes: 0 additions & 23 deletions src/HttpApi/Controllers/FetchChannelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers;

use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
Expand All @@ -13,27 +11,6 @@

class FetchChannelsController extends Controller
{
/**
* The replicator driver.
*
* @var \BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface
*/
protected $replicator;

/**
* Initialize the class.
*
* @param \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager $channelManager
* @param \BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface $replicator
* @return void
*/
public function __construct(ChannelManager $channelManager, ReplicationInterface $replicator)
{
parent::__construct($channelManager);

$this->replicator = $replicator;
}

/**
* Handle the incoming request.
*
Expand Down
22 changes: 17 additions & 5 deletions src/HttpApi/Controllers/TriggerEventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,26 @@ public function __invoke(Request $request)
{
$this->ensureValidSignature($request);

foreach ($request->json()->get('channels', []) as $channelName) {
$channels = $request->channels ?: [];

foreach ($channels as $channelName) {
$channel = $this->channelManager->find($request->appId, $channelName);

optional($channel)->broadcastToEveryoneExcept((object) [
$payload = (object) [
'channel' => $channelName,
'event' => $request->json()->get('name'),
'data' => $request->json()->get('data'),
], $request->json()->get('socket_id'), $request->appId);
'event' => $request->name,
'data' => $request->data,
];

optional($channel)->broadcastToEveryoneExcept($payload, $request->socket_id, $request->appId);

// If the setup is horizontally-scaled using the Redis Pub/Sub,
// then we're going to make sure it gets streamed to the other
// servers as well that are subscribed to the Pub/Sub topics
// attached to the current iterated app & channel.
// For local setups, the local driver will ignore the publishes.

$this->replicator->publish($request->appId, $channelName, $payload);

DashboardLogger::log($request->appId, DashboardLogger::TYPE_API_MESSAGE, [
'channel' => $channelName,
Expand Down
142 changes: 0 additions & 142 deletions src/PubSub/Broadcasters/RedisPusherBroadcaster.php

This file was deleted.

8 changes: 4 additions & 4 deletions src/PubSub/Drivers/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,23 +293,23 @@ public function onMessage(string $redisChannel, string $payload)
return;
}

$socket = $payload->socket ?? null;
$socketId = $payload->socketId ?? null;
$serverId = $payload->serverId ?? null;

// Remove fields intended for internal use from the payload.
unset($payload->socket);
unset($payload->socketId);
unset($payload->serverId);
unset($payload->appId);

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

DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_MESSAGE_RECEIVED, [
'channel' => $channel->getChannelName(),
'redisChannel' => $redisChannel,
'serverId' => $this->getServerId(),
'incomingServerId' => $serverId,
'incomingSocketId' => $socket,
'incomingSocketId' => $socketId,
'payload' => $payload,
]);
}
Expand Down
Loading