Skip to content
Closed
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
18 changes: 18 additions & 0 deletions src/Events/PresenceChannelSubscribe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Contracts\Connection;
use Illuminate\Foundation\Events\Dispatchable;
class PresenceChannelSubscribe

{
use Dispatchable;
public function __construct(
public string $channel,
public Connection $connection
) {}



}
18 changes: 18 additions & 0 deletions src/Events/PresenceChannelUnsubscribe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Contracts\Connection;
use Illuminate\Foundation\Events\Dispatchable;
class PresenceChannelUnsubscribe

{
use Dispatchable;
public function __construct(
public string $channel,
public Connection $connection
) {}



}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Laravel\Reverb\Protocols\Pusher\Channels\Concerns;

use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\PresenceChannelSubscribe;
use Laravel\Reverb\Events\PresenceChannelUnsubscribe;

trait InteractsWithPresenceChannels
{
Expand All @@ -25,6 +27,8 @@ public function subscribe(Connection $connection, ?string $auth = null, ?string

parent::subscribe($connection, $auth, $data);

PresenceChannelSubscribe::dispatch($this->name(),$connection);

parent::broadcastInternally(
[
'event' => 'pusher_internal:member_added',
Expand Down Expand Up @@ -52,6 +56,8 @@ public function unsubscribe(Connection $connection): void
return;
}

PresenceChannelUnsubscribe::dispatch($this->name(), $connection);

parent::broadcast(
[
'event' => 'pusher_internal:member_removed',
Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/Protocols/Pusher/Channels/PresenceChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Laravel\Reverb\Protocols\Pusher\Contracts\ChannelConnectionManager;
use Laravel\Reverb\Protocols\Pusher\Exceptions\ConnectionUnauthorized;
use Laravel\Reverb\Tests\FakeConnection;
use Illuminate\Support\Facades\Event;
use Laravel\Reverb\Events\PresenceChannelSubscribe;
use Laravel\Reverb\Events\PresenceChannelUnsubscribe;

beforeEach(function () {
$this->connection = new FakeConnection;
Expand Down Expand Up @@ -82,6 +85,58 @@
]);
});


it('dispatches PresenceChannelSubscribe event on first subscription', function () {
Event::fake([PresenceChannelSubscribe::class]);

$channel = new PresenceChannel('presence-test-channel');
$data = json_encode(['user_id' => 1, 'user_info' => ['name' => 'Joe']]);

$this->channelConnectionManager->shouldReceive('add')
->once()
->with($this->connection, ['user_id' => 1, 'user_info' => ['name' => 'Joe']]);

$this->channelConnectionManager->shouldReceive('all')
->andReturn([]);

$channel->subscribe(
$this->connection,
validAuth($this->connection->id(), 'presence-test-channel', $data),
$data
);

Event::assertDispatched(PresenceChannelSubscribe::class, function ($event) {
return $event->channel === 'presence-test-channel' &&
$event->connection === $this->connection;
});
});

it('dispatches PresenceChannelUnsubscribe event when last connection of user is removed', function () {
Event::fake([PresenceChannelUnsubscribe::class]);

$channel = new PresenceChannel('presence-test-channel');

$this->channelConnectionManager->shouldReceive('find')
->andReturn(new ChannelConnection($this->connection, [
'user_id' => 1,
'user_info' => ['name' => 'Joe'],
]));

$this->channelConnectionManager->shouldReceive('all')
->andReturn([]);

$this->channelConnectionManager->shouldReceive('remove')
->once()
->with($this->connection);

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

Event::assertDispatched(PresenceChannelUnsubscribe::class, function ($event) {
return $event->channel === 'presence-test-channel' &&
$event->connection === $this->connection;
});
});

it('sends notification of subscription', function () {
$channel = new PresenceChannel('presence-test-channel');

Expand Down