diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 58a6426..632ce81 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -207,6 +207,8 @@ 'subscribed', 'client-message', 'api-message', + 'replicator-subscribed', + 'replicator-unsubscribed', ].forEach(channelName => this.subscribeToChannel(channelName)) }, diff --git a/src/Dashboard/DashboardLogger.php b/src/Dashboard/DashboardLogger.php index 874d0d2..24d400a 100644 --- a/src/Dashboard/DashboardLogger.php +++ b/src/Dashboard/DashboardLogger.php @@ -9,14 +9,25 @@ use stdClass; class DashboardLogger { const LOG_CHANNEL_PREFIX = 'private-websockets-dashboard-'; + const TYPE_DISCONNECTION = 'disconnection'; + const TYPE_CONNECTION = 'connection'; + const TYPE_VACATED = 'vacated'; + const TYPE_OCCUPIED = 'occupied'; + const TYPE_SUBSCRIBED = 'subscribed'; + const TYPE_CLIENT_MESSAGE = 'client-message'; + const TYPE_API_MESSAGE = 'api-message'; + const TYPE_REPLICATOR_SUBSCRIBED = 'replicator-subscribed'; + + const TYPE_REPLICATOR_UNSUBSCRIBED = 'replicator-unsubscribed'; + public static function connection(ConnectionInterface $connection) { /** @var \GuzzleHttp\Psr7\Request $request */ @@ -74,6 +85,20 @@ class DashboardLogger ]); } + public static function replicatorSubscribed(string $appId, string $channel, string $serverId) + { + static::log($appId, static::TYPE_REPLICATOR_SUBSCRIBED, [ + 'details' => "Server ID: {$serverId} on Channel: {$channel}", + ]); + } + + public static function replicatorUnsubscribed(string $appId, string $channel, string $serverId) + { + static::log($appId, static::TYPE_REPLICATOR_UNSUBSCRIBED, [ + 'details' => "Server ID: {$serverId} on Channel: {$channel}", + ]); + } + public static function log($appId, string $type, array $attributes = []) { $channelName = static::LOG_CHANNEL_PREFIX.$type; diff --git a/src/HttpApi/Controllers/FetchChannelsController.php b/src/HttpApi/Controllers/FetchChannelsController.php index 0512210..a1a06e1 100644 --- a/src/HttpApi/Controllers/FetchChannelsController.php +++ b/src/HttpApi/Controllers/FetchChannelsController.php @@ -8,6 +8,7 @@ use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel; use Illuminate\Http\Request; use Illuminate\Support\Collection; use Illuminate\Support\Str; +use stdClass; use Symfony\Component\HttpKernel\Exception\HttpException; class FetchChannelsController extends Controller @@ -54,15 +55,18 @@ class FetchChannelsController extends Controller return $this->replicator ->channelMemberCounts($request->appId, $channelNames) ->then(function (array $counts) use ($channels, $attributes) { - return [ - 'channels' => $channels->map(function (PresenceChannel $channel) use ($counts, $attributes) { - $info = new \stdClass; - if (in_array('user_count', $attributes)) { - $info->user_count = $counts[$channel->getChannelName()]; - } + $channels = $channels->map(function (PresenceChannel $channel) use ($counts, $attributes) { + $info = new stdClass; - return $info; - })->toArray() ?: new \stdClass, + if (in_array('user_count', $attributes)) { + $info->user_count = $counts[$channel->getChannelName()]; + } + + return $info; + })->toArray(); + + return [ + 'channels' => $channels ?: new stdClass, ]; }); } diff --git a/src/PubSub/Drivers/RedisClient.php b/src/PubSub/Drivers/RedisClient.php index 6d8aa28..cbec33a 100644 --- a/src/PubSub/Drivers/RedisClient.php +++ b/src/PubSub/Drivers/RedisClient.php @@ -2,6 +2,7 @@ namespace BeyondCode\LaravelWebSockets\PubSub\Drivers; +use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger; use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use Clue\React\Redis\Client; @@ -139,6 +140,8 @@ class RedisClient implements ReplicationInterface $this->subscribedChannels["$appId:$channel"]++; } + DashboardLogger::replicatorSubscribed($appId, $channel, $this->serverId); + return true; } @@ -161,9 +164,12 @@ class RedisClient implements ReplicationInterface // If we no longer have subscriptions to that channel, unsubscribe if ($this->subscribedChannels["$appId:$channel"] < 1) { $this->subscribeClient->__call('unsubscribe', ["$appId:$channel"]); + unset($this->subscribedChannels["$appId:$channel"]); } + DashboardLogger::replicatorUnsubscribed($appId, $channel, $this->serverId); + return true; }