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

Fix presence channels double counting users #527

Merged
merged 3 commits into from
Sep 15, 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
8 changes: 4 additions & 4 deletions src/WebSockets/Channels/PresenceChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ protected function getChannelData(): array
{
return [
'presence' => [
'ids' => $this->getUserIds(),
'ids' => $userIds = $this->getUserIds(),
'hash' => $this->getHash(),
'count' => count($this->users),
'count' => count($userIds),
],
];
}

public function toArray(): array
{
return array_merge(parent::toArray(), [
'user_count' => count($this->users),
'user_count' => count($this->getUserIds()),
]);
}

Expand All @@ -83,7 +83,7 @@ protected function getUserIds(): array
return (string) $channelData->user_id;
}, $this->users);

return array_values($userIds);
return array_values(array_unique($userIds));
}

/**
Expand Down
63 changes: 44 additions & 19 deletions tests/Channels/PresenceChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BeyondCode\LaravelWebSockets\Tests\Channels;

use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature;
Expand Down Expand Up @@ -42,16 +43,7 @@ public function clients_with_valid_auth_signatures_can_join_presence_channels()
],
];

$signature = "{$connection->socketId}:presence-channel:".json_encode($channelData);

$message = new Message(json_encode([
'event' => 'pusher:subscribe',
'data' => [
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
'channel' => 'presence-channel',
'channel_data' => json_encode($channelData),
],
]));
$message = $this->getSignedMessage($connection, 'presence-channel', $channelData);

$this->pusherServer->onMessage($connection, $message);

Expand All @@ -71,21 +63,54 @@ public function clients_with_no_user_info_can_join_presence_channels()
'user_id' => 1,
];

$signature = "{$connection->socketId}:presence-channel:".json_encode($channelData);
$message = $this->getSignedMessage($connection, 'presence-channel', $channelData);

$message = new Message(json_encode([
$this->pusherServer->onMessage($connection, $message);

$connection->assertSentEvent('pusher_internal:subscription_succeeded', [
'channel' => 'presence-channel',
]);
}

/** @test */
public function multiple_clients_with_same_user_id_are_counted_once()
{
$this->pusherServer->onOpen($connection = $this->getWebSocketConnection());
$this->pusherServer->onOpen($connection2 = $this->getWebSocketConnection());

$channelName = 'presence-channel';
$channelData = [
'user_id' => $userId = 1,
];

$this->pusherServer->onMessage($connection, $this->getSignedMessage($connection, $channelName, $channelData));
$this->pusherServer->onMessage($connection2, $this->getSignedMessage($connection2, $channelName, $channelData));

$connection2->assertSentEvent('pusher_internal:subscription_succeeded', [
'channel' => $channelName,
'data' => json_encode([
'presence' => [
'ids' => [(string) $userId],
'hash' => [
(string) $userId => [],
],
'count' => 1,
],
]),
]);
}

private function getSignedMessage(Connection $connection, string $channelName, array $channelData): Message
{
$signature = "{$connection->socketId}:{$channelName}:".json_encode($channelData);

return new Message(json_encode([
'event' => 'pusher:subscribe',
'data' => [
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
'channel' => 'presence-channel',
'channel' => $channelName,
'channel_data' => json_encode($channelData),
],
]));

$this->pusherServer->onMessage($connection, $message);

$connection->assertSentEvent('pusher_internal:subscription_succeeded', [
'channel' => 'presence-channel',
]);
}
}