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

Update ChannelManager #41

Merged
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
9 changes: 9 additions & 0 deletions config/websockets.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,13 @@
*/
'passphrase' => null,
],

/*
* Channel Manager
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some more info here on what this does and in which cases you would change this option

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

* This class handles how channel persistence is handled.
* By default, persistence is stored in an array by the running webserver.
* The only requirement is that the class should implement
* `ChannelManager` interface provided by this package.

* This class handles how channel persistence is handled.
* By default, persistence is stored in an array by the running webserver.
* The only requirement is that the class should implement
* `ChannelManager` interface provided by this package.
*/
'channel_manager' => \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager::class,
];
75 changes: 6 additions & 69 deletions src/WebSockets/Channels/ChannelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,15 @@

use Ratchet\ConnectionInterface;

class ChannelManager
interface ChannelManager
{
/** @var string */
protected $appId;
public function findOrCreate(string $appId, string $channelName): Channel;

/** @var array */
protected $channels = [];
public function find(string $appId, string $channelName): ?Channel;

public function findOrCreate(string $appId, string $channelName): Channel
{
if (! isset($this->channels[$appId][$channelName])) {
$channelClass = $this->determineChannelClass($channelName);
public function getChannels(string $appId): array;

$this->channels[$appId][$channelName] = new $channelClass($channelName);
}
public function getConnectionCount(string $appId): int;

return $this->channels[$appId][$channelName];
}

public function find(string $appId, string $channelName): ?Channel
{
return $this->channels[$appId][$channelName] ?? null;
}

protected function determineChannelClass(string $channelName): string
{
if (starts_with($channelName, 'private-')) {
return PrivateChannel::class;
}

if (starts_with($channelName, 'presence-')) {
return PresenceChannel::class;
}

return Channel::class;
}

public function getChannels(string $appId): array
{
return $this->channels[$appId] ?? [];
}

public function getConnectionCount(string $appId): int
{
return collect($this->getChannels($appId))
->sum(function ($channel) {
return count($channel->getSubscribedConnections());
});
}

public function removeFromAllChannels(ConnectionInterface $connection)
{
if (! isset($connection->app)) {
return;
}

/*
* Remove the connection from all channels.
*/
collect(array_get($this->channels, $connection->app->id, []))->each->unsubscribe($connection);

/*
* Unset all channels that have no connections so we don't leak memory.
*/
collect(array_get($this->channels, $connection->app->id, []))
->reject->hasConnections()
->each(function (Channel $channel, string $channelName) use ($connection) {
unset($this->channels[$connection->app->id][$channelName]);
});

if (count(array_get($this->channels, $connection->app->id, [])) === 0) {
unset($this->channels[$connection->app->id]);
}
}
public function removeFromAllChannels(ConnectionInterface $connection);
}
85 changes: 85 additions & 0 deletions src/WebSockets/Channels/ChannelManagers/ArrayChannelManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers;

use Ratchet\ConnectionInterface;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\Channel;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PrivateChannel;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel;

class ArrayChannelManager implements ChannelManager
{
/** @var string */
protected $appId;

/** @var array */
protected $channels = [];

public function findOrCreate(string $appId, string $channelName): Channel
{
if (! isset($this->channels[$appId][$channelName])) {
$channelClass = $this->determineChannelClass($channelName);

$this->channels[$appId][$channelName] = new $channelClass($channelName);
}

return $this->channels[$appId][$channelName];
}

public function find(string $appId, string $channelName): ?Channel
{
return $this->channels[$appId][$channelName] ?? null;
}

protected function determineChannelClass(string $channelName): string
{
if (starts_with($channelName, 'private-')) {
return PrivateChannel::class;
}

if (starts_with($channelName, 'presence-')) {
return PresenceChannel::class;
}

return Channel::class;
}

public function getChannels(string $appId): array
{
return $this->channels[$appId] ?? [];
}

public function getConnectionCount(string $appId): int
{
return collect($this->getChannels($appId))
->sum(function ($channel) {
return count($channel->getSubscribedConnections());
});
}

public function removeFromAllChannels(ConnectionInterface $connection)
{
if (! isset($connection->app)) {
return;
}

/*
* Remove the connection from all channels.
*/
collect(array_get($this->channels, $connection->app->id, []))->each->unsubscribe($connection);

/*
* Unset all channels that have no connections so we don't leak memory.
*/
collect(array_get($this->channels, $connection->app->id, []))
->reject->hasConnections()
->each(function (Channel $channel, string $channelName) use ($connection) {
unset($this->channels[$connection->app->id][$channelName]);
});

if (count(array_get($this->channels, $connection->app->id, [])) === 0) {
unset($this->channels[$connection->app->id]);
}
}
}
4 changes: 3 additions & 1 deletion src/WebSocketsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\DashboardApiController;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
use BeyondCode\LaravelWebSockets\Statistics\Http\Middleware\Authorize as AuthorizeStatistics;
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
Expand Down Expand Up @@ -51,7 +52,8 @@ public function register()
});

$this->app->singleton(ChannelManager::class, function () {
return new ChannelManager();
return config('websockets.channel_manager') !== null && class_exists(config('websockets.channel_manager'))
? app(config('websockets.channel_manager')) : new ArrayChannelManager();
});

$this->app->singleton(AppProvider::class, function () {
Expand Down