Skip to content

Commit

Permalink
Added impl. for decoding socketIdObject inside broadcast method, updated
Browse files Browse the repository at this point in the history
tests for the same
  • Loading branch information
sacOO7 committed Sep 23, 2024
1 parent 7c2eedb commit fdac7b0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
35 changes: 26 additions & 9 deletions src/AblyBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ably\LaravelBroadcaster;

use _PHPStan_cb8f9103f\Nette\Neon\Exception;
use Ably\AblyRest;
use Ably\Exceptions\AblyException;
use Ably\Models\Message as AblyMessage;
Expand Down Expand Up @@ -177,10 +178,11 @@ public function validAuthenticationResponse($request, $result)
*/
public function broadcast($channels, $event, $payload = [])
{
$socketIdObject = Utils::decodeSocketId(Arr::pull($payload, 'socket'));
try {
foreach ($this->formatChannels($channels) as $channel) {
$this->ably->channels->get($channel)->publish(
$this->buildAblyMessage($event, $payload)
$this->buildAblyMessage($event, $payload, $socketIdObject)
);
}
} catch (AblyException $e) {
Expand Down Expand Up @@ -310,19 +312,34 @@ public function formatChannels($channels)
/**
* Build an Ably message object for broadcasting.
*
* @param string $event
* @param array $payload
* @return \Ably\Models\Message
* @param string $event
* @param array $payload
* @param array|null $socketIdObject
* @return AblyMessage
* @throws Exception
*/
protected function buildAblyMessage($event, $payload = [])
protected function buildAblyMessage($event, $payload = [], $socketIdObject = null)
{
$socket = Arr::pull($payload, 'socket');

return tap(new AblyMessage, function ($message) use ($event, $payload, $socket) {
$message = tap(new AblyMessage, function ($message) use ($event, $payload, $socketIdObject) {
$message->name = $event;
$message->data = $payload;
$message->connectionKey = $socket;
});

if ($socketIdObject) {
$connectionKey_key = 'connectionKey';
if (array_key_exists($connectionKey_key, $socketIdObject)) {
$message->connectionKey = $socketIdObject[$connectionKey_key];
} else {
throw new Exception(Utils::missingKeyErrorForSocketId($connectionKey_key));
}
$clientId_key = 'clientId';
if (array_key_exists($clientId_key, $socketIdObject)) {
$message->clientId = $socketIdObject[$clientId_key];
} else {
throw new Exception(Utils::missingKeyErrorForSocketId($clientId_key));
}
}
return $message;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Ably\LaravelBroadcaster;

use Illuminate\Broadcasting\BroadcastException;

class Utils
{
// JWT related PHP utility functions
Expand Down Expand Up @@ -62,4 +64,16 @@ public static function base64urlEncode($str)
{
return rtrim(strtr(base64_encode($str), '+/', '-_'), '=');
}

public static function decodeSocketId($socketId) {
if ($socketId) {
return json_decode(base64_decode($socketId), true);
}
return null;
}

public static function missingKeyErrorForSocketId($keyName) {
return $keyName." not present in socketId, please make sure to send base64 encoded json with "
."connectionKey and clientId as keys. clientId is null if connection is not identified.";
}
}
24 changes: 19 additions & 5 deletions tests/AblyBroadcasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,23 +343,37 @@ public function testLaravelAblyAgentHeader()

public function testPayloadShouldNotIncludeSocketKey()
{

// self::assertArrayNotHasKey('socket', $message->data);

}
public function testBuildMessageUsingProvidedSocketIdObject()
{
$broadcaster = m::mock(AblyBroadcasterExposed::class, [$this->ably, []])->makePartial();

$payload = [
'foo' => 'bar',
'socket' => null
'chat' => 'hello there'
];

$socketIdObject = [
'connectionKey' => 'key',
'clientId' => 'id'
];

$message = $broadcaster->buildAblyMessage('testEvent', $payload);
self::assertArrayNotHasKey('socket', $message->data);
$message = $broadcaster->buildAblyMessage('testEvent', $payload, $socketIdObject);
self::assertEquals('key', $message->connectionKey);
self::assertEquals('id', $message->clientId);
self::assertEquals('testEvent', $message->name);
self::assertEquals($payload, $message->data);
}
}

class AblyBroadcasterExposed extends AblyBroadcaster
{
public function buildAblyMessage($event, $payload = [])
public function buildAblyMessage($event, $payload = [], $socketIdObject = null)
{
return parent::buildAblyMessage($event, $payload);
return parent::buildAblyMessage($event, $payload, $socketIdObject);
}
}

Expand Down

0 comments on commit fdac7b0

Please sign in to comment.