From 4ee9e9730fe515a0d10b5c4c25d40570641e9933 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Thu, 7 Nov 2024 12:05:37 +0100 Subject: [PATCH 1/2] feat: realtime ping pong --- .gitignore | 1 + app/realtime.php | 12 ++++++--- .../Realtime/RealtimeCustomClientTest.php | 26 +++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 5ae03e2a56..0d2b4b51ff 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ dev/yasd_init.php .phpunit.result.cache Makefile appwrite.json +.zed/ \ No newline at end of file diff --git a/app/realtime.php b/app/realtime.php index b8fdb2cf21..160b33d8d5 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -582,9 +582,15 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re } switch ($message['type']) { - /** - * This type is used to authenticate. - */ + case 'ping': + $server->send([$connection], json_encode([ + 'type' => 'pong' + ])); + + break; + /** + * This type is used to authenticate. + */ case 'authentication': if (!array_key_exists('session', $message['data'])) { throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Payload is not valid.'); diff --git a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php index c3372b98c5..2b3d874e7c 100644 --- a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php +++ b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php @@ -7,6 +7,7 @@ use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideClient; +use Tests\E2E\Services\Functions\FunctionsBase; use Utopia\CLI\Console; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; @@ -15,6 +16,7 @@ use WebSocket\ConnectionException; class RealtimeCustomClientTest extends Scope { + use FunctionsBase; use RealtimeBase; use ProjectCustom; use SideClient; @@ -110,6 +112,30 @@ class RealtimeCustomClientTest extends Scope $client->close(); } + public function testPingPong() + { + $client = $this->getWebsocket(['files'], [ + 'origin' => 'http://localhost' + ]); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('files', $response['data']['channels']); + + $client->send(\json_encode([ + 'type' => 'ping' + ])); + + $response = json_decode($client->receive(), true); + $this->assertEquals('pong', $response['type']); + + $client->close(); + } + public function testManualAuthentication() { $user = $this->getUser(); From f91f382baf010962d2399087d26a5a70d6c63673 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Thu, 7 Nov 2024 12:08:11 +0100 Subject: [PATCH 2/2] style: remove unnecessary comment --- app/realtime.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/realtime.php b/app/realtime.php index 160b33d8d5..d8137a7a5a 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -588,9 +588,6 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re ])); break; - /** - * This type is used to authenticate. - */ case 'authentication': if (!array_key_exists('session', $message['data'])) { throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Payload is not valid.');