-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for Pusher cache channels (#25)
* adds cache channels * send empty cache message * stub private and presence cache channels * wip * Fix code styling
- Loading branch information
Showing
18 changed files
with
706 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb\Channels; | ||
|
||
use Laravel\Reverb\Contracts\Connection; | ||
|
||
class CacheChannel extends Channel | ||
{ | ||
/** | ||
* Data from last event triggered. | ||
*/ | ||
protected ?array $payload = null; | ||
|
||
/** | ||
* Send a message to all connections subscribed to the channel. | ||
*/ | ||
public function broadcast(array $payload, Connection $except = null): void | ||
{ | ||
$this->payload = $payload; | ||
|
||
parent::broadcast($payload, $except); | ||
} | ||
|
||
/** | ||
* Broadcast a message triggered from an internal source. | ||
*/ | ||
public function broadcastInternally(array $payload, Connection $except = null): void | ||
{ | ||
parent::broadcast($payload, $except); | ||
} | ||
|
||
/** | ||
* Determine if the channel has a cached payload. | ||
*/ | ||
public function hasCachedPayload(): bool | ||
{ | ||
return $this->payload !== null; | ||
} | ||
|
||
/** | ||
* Get the cached payload. | ||
*/ | ||
public function cachedPayload(): ?array | ||
{ | ||
return $this->payload; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb\Channels\Concerns; | ||
|
||
use Laravel\Reverb\Contracts\Connection; | ||
|
||
trait InteractsWithPresenceChannels | ||
{ | ||
use InteractsWithPrivateChannels; | ||
|
||
/** | ||
* Subscribe to the given channel. | ||
*/ | ||
public function subscribe(Connection $connection, string $auth = null, string $data = null): void | ||
{ | ||
$this->verify($connection, $auth, $data); | ||
|
||
parent::subscribe($connection, $auth, $data); | ||
|
||
parent::broadcastInternally( | ||
[ | ||
'event' => 'pusher_internal:member_added', | ||
'data' => $data ? json_decode($data, true) : [], | ||
'channel' => $this->name(), | ||
], | ||
$connection | ||
); | ||
} | ||
|
||
/** | ||
* Unsubscribe from the given channel. | ||
*/ | ||
public function unsubscribe(Connection $connection): void | ||
{ | ||
if (! $subscription = $this->connections->find($connection)) { | ||
parent::unsubscribe($connection); | ||
|
||
return; | ||
} | ||
|
||
if ($userId = $subscription->data('user_id')) { | ||
parent::broadcast( | ||
[ | ||
'event' => 'pusher_internal:member_removed', | ||
'data' => ['user_id' => $userId], | ||
'channel' => $this->name(), | ||
], | ||
$connection | ||
); | ||
} | ||
|
||
parent::unsubscribe($connection); | ||
} | ||
|
||
/** | ||
* Get the data associated with the channel. | ||
*/ | ||
public function data(): array | ||
{ | ||
$connections = collect($this->connections->all()) | ||
->map(fn ($connection) => $connection->data()); | ||
|
||
if ($connections->contains(fn ($connection) => ! isset($connection['user_id']))) { | ||
return [ | ||
'presence' => [ | ||
'count' => 0, | ||
'ids' => [], | ||
'hash' => [], | ||
], | ||
]; | ||
} | ||
|
||
return [ | ||
'presence' => [ | ||
'count' => $connections->count() ?? 0, | ||
'ids' => $connections->map(fn ($connection) => $connection['user_id'])->all(), | ||
'hash' => $connections->keyBy('user_id')->map->user_info->toArray(), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb\Channels\Concerns; | ||
|
||
use Illuminate\Support\Str; | ||
use Laravel\Reverb\Contracts\Connection; | ||
use Laravel\Reverb\Exceptions\ConnectionUnauthorized; | ||
|
||
trait InteractsWithPrivateChannels | ||
{ | ||
/** | ||
* Subscribe to the given channel. | ||
*/ | ||
public function subscribe(Connection $connection, string $auth = null, string $data = null): void | ||
{ | ||
$this->verify($connection, $auth, $data); | ||
|
||
parent::subscribe($connection, $auth, $data); | ||
} | ||
|
||
/** | ||
* Deteremine whether the given auth token is valid. | ||
*/ | ||
protected function verify(Connection $connection, string $auth, string $data = null): bool | ||
{ | ||
$signature = "{$connection->id()}:{$this->name()}"; | ||
|
||
if ($data) { | ||
$signature .= ":{$data}"; | ||
} | ||
|
||
if (! hash_equals( | ||
hash_hmac( | ||
'sha256', | ||
$signature, | ||
$connection->app()->secret(), | ||
), | ||
Str::after($auth, ':') | ||
)) { | ||
throw new ConnectionUnauthorized; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb\Channels; | ||
|
||
use Laravel\Reverb\Channels\Concerns\InteractsWithPresenceChannels; | ||
|
||
class PresenceCacheChannel extends CacheChannel | ||
{ | ||
use InteractsWithPresenceChannels; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb\Channels; | ||
|
||
use Laravel\Reverb\Channels\Concerns\InteractsWithPrivateChannels; | ||
|
||
class PrivateCacheChannel extends CacheChannel | ||
{ | ||
use InteractsWithPrivateChannels; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.