Skip to content

Commit

Permalink
store connection data
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Nov 20, 2023
1 parent 9470b39 commit b56ac88
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/Channels/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ public function broadcast(Application $app, array $payload, Connection $except =
{
collect($this->connections())
->each(function ($connection) use ($payload, $except) {
if ($except && $except->identifier() === $connection->identifier()) {
if ($except && $except->identifier() === $connection->connection()->identifier()) {
return;
}

if (isset($payload['except']) && $payload['except'] === $connection->identifier()) {
if (isset($payload['except']) && $payload['except'] === $connection->connection()->identifier()) {
return;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Contracts/ChannelConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface ChannelConnectionManager
/**
* Add a connection.
*/
public function add(Connection $connection): void;
public function add(Connection $connection, array $data): void;

/**
* Remove a connection.
Expand All @@ -16,6 +16,7 @@ public function remove(Connection $connection): void;

/**
* Get all the connections.
* @return array<string, \Laravel\Reverb\Servers\Reverb\ChannelConnection>
*/
public function all(): array;

Expand Down
7 changes: 4 additions & 3 deletions src/Managers/ArrayChannelConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@

use Laravel\Reverb\Contracts\ChannelConnectionManager;
use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Servers\Reverb\ChannelConnection;

class ArrayChannelConnectionManager implements ChannelConnectionManager
{
/**
* Connection store.
*
* @var array<string, array<string, \Laravel\Reverb\Connection>>
* @var array<string, \Laravel\Reverb\Servers\Reverb\ChannelConnection>
*/
protected $connections = [];

/**
* Add a connection.
*/
public function add(Connection $connection): void
public function add(Connection $connection, array $data): void
{
$this->connections[$connection->identifier()] = $connection;
$this->connections[$connection->identifier()] = new ChannelConnection($connection, $data);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/Servers/Reverb/ChannelConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Laravel\Reverb\Servers\Reverb;

use Laravel\Reverb\Contracts\Connection;

class ChannelConnection
{
public function __construct(protected Connection $connection, protected array $data = [])
{
//
}

/**
* Get the underlying connection.
*/
public function connection(): Connection
{
return $this->connection;
}

/**
* Get the connection data.
*/
public function data(): array
{
return $this->data;
}

/**
* Call the method on the connection.
*/
public function __call(string $method, array $parameters): mixed
{
return $this->connection->{$method}(...$parameters);
}
}
7 changes: 5 additions & 2 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Laravel\Reverb\Contracts\Connection as ReverbConnection;
use Laravel\Reverb\Contracts\ConnectionManager;
use Laravel\Reverb\Managers\Connections;
use Laravel\Reverb\Servers\Reverb\ChannelConnection;
use Laravel\Reverb\Tests\Connection;
use Laravel\Reverb\Tests\SerializableConnection;
use Laravel\Reverb\Tests\TestCase;
Expand All @@ -24,9 +25,11 @@
function connections(int $count = 1, $serializable = false): array
{
return Collection::make(range(1, $count))->map(function () use ($serializable) {
return $serializable
return new ChannelConnection(
$serializable
? new SerializableConnection(Uuid::uuid4())
: new Connection(Uuid::uuid4());
: new Connection(Uuid::uuid4())
);
})->all();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Channels/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
->once()
->andReturn($connections = connections(3));

$channel->broadcast(app(ApplicationProvider::class)->findByKey('pusher-key'), ['foo' => 'bar'], $connections[0]);
$channel->broadcast(app(ApplicationProvider::class)->findByKey('pusher-key'), ['foo' => 'bar'], $connections[0]->connection());

$connections[0]->assertNothingSent();
collect(array_slice($connections, -2))->each(fn ($connection) => $connection->assertSent(['foo' => 'bar']));
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/ClientEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Laravel\Reverb\ClientEvent;
use Laravel\Reverb\Contracts\ChannelConnectionManager;
use Laravel\Reverb\Servers\Reverb\ChannelConnection;
use Laravel\Reverb\Tests\Connection;

beforeEach(function () {
Expand Down Expand Up @@ -33,7 +34,7 @@
it('does not forward a message to itself', function () {
$this->channelConnectionManager->shouldReceive('all')
->once()
->andReturn([$this->connection]);
->andReturn([new ChannelConnection($this->connection)]);

ClientEvent::handle(
$this->connection, [
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Jobs/PingInactiveConnectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
->andReturn($connections);

$connections = collect($connections)->each(function ($connection) use ($channel) {
$channel->subscribe($connection);
$channel->subscribe($connection->connection());
$connection->setLastSeenAt(now()->subMinutes(10));
});

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Jobs/PruneStaleConnectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
->andReturn($connections);

collect($connections)->each(function ($connection) use ($channel) {
$channel->subscribe($connection);
$channel->subscribe($connection->connection());
$connection->setLastSeenAt(now()->subMinutes(10));
$connection->setHasBeenPinged();

$this->channelManager->shouldReceive('unsubscribeFromAll')
->once()
->with($connection);
->with($connection->connection());
});

(new PruneStaleConnections)->handle(
Expand Down
22 changes: 11 additions & 11 deletions tests/Unit/Managers/ChannelMangerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@

it('can subscribe to a channel', function () {
collect(connections(5))
->each(fn ($connection) => $this->channel->subscribe($connection));
->each(fn ($connection) => $this->channel->subscribe($connection->connection()));

expect($this->channel->connections())->toHaveCount(5);
});

it('can unsubscribe from a channel', function () {
$connections = collect(connections(5))
->each(fn ($connection) => $this->channel->subscribe($connection));
->each(fn ($connection) => $this->channel->subscribe($connection->connection()));

$this->channel->unsubscribe($connections->first());
$this->channel->unsubscribe($connections->first()->connection());

expect($this->channel->connections())->toHaveCount(4);
});
Expand All @@ -42,10 +42,10 @@

it('can get all connections subscribed to a channel', function () {
$connections = collect(connections(5))
->each(fn ($connection) => $this->channel->subscribe($connection));
->each(fn ($connection) => $this->channel->subscribe($connection->connection()));

$connections->each(fn ($connection) => expect($connection->identifier())
->toBeIn(collect($this->channel->connections())->pluck('identifier')->all()));
->toBeIn(array_keys($this->channel->connections())));
});

it('can unsubscribe a connection for all channels', function () {
Expand Down Expand Up @@ -83,19 +83,19 @@
$connections = collect($connections)->split(3);

$connections->first()->each(function ($connection) use ($channelOne, $channelTwo, $channelThree) {
$channelOne->subscribe($connection);
$channelTwo->subscribe($connection);
$channelThree->subscribe($connection);
$channelOne->subscribe($connection->connection());
$channelTwo->subscribe($connection->connection());
$channelThree->subscribe($connection->connection());
});

$connections->get(1)->each(function ($connection) use ($channelTwo, $channelThree) {
$channelTwo->subscribe($connection);
$channelTwo->subscribe($connection->connection());

$channelThree->subscribe($connection);
$channelThree->subscribe($connection->connection());
});

$connections->last()->each(function ($connection) use ($channelThree) {
$channelThree->subscribe($connection);
$channelThree->subscribe($connection->connection());
});

expect($channelOne->connections())->toHaveCount(4);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Managers/ConnectionMangerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function () {

it('can get all connections', function () {
$connections = collect(connections(10));
$connections->each(fn ($connection) => $this->connectionManager->save($connection));
$connections->each(fn ($connection) => $this->connectionManager->save($connection->connection()));

expect($this->connectionManager->all())
->toHaveCount(10);
Expand Down

0 comments on commit b56ac88

Please sign in to comment.