From 4ee9e9730fe515a0d10b5c4c25d40570641e9933 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Thu, 7 Nov 2024 12:05:37 +0100 Subject: [PATCH 1/3] 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/3] 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.'); From 7f3a3429404bef32e5884ac38552834fe032ade6 Mon Sep 17 00:00:00 2001 From: Ebenezer Don Date: Tue, 12 Nov 2024 16:44:20 +0000 Subject: [PATCH 3/3] Remove inaccurate info about leaving the URL parameter empty --- docs/references/account/create-token-magic-url.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/references/account/create-token-magic-url.md b/docs/references/account/create-token-magic-url.md index 6ebe4154b8..99ad6dba5e 100644 --- a/docs/references/account/create-token-magic-url.md +++ b/docs/references/account/create-token-magic-url.md @@ -1,3 +1,3 @@ -Sends the user an email with a secret key for creating a session. If the provided user ID has not been registered, a new user will be created. When the user clicks the link in the email, the user is redirected back to the URL you provided with the secret key and userId values attached to the URL query string. Use the query string parameters to submit a request to the [POST /v1/account/sessions/token](https://appwrite.io/docs/references/cloud/client-web/account#createSession) endpoint to complete the login process. The link sent to the user's email address is valid for 1 hour. If you are on a mobile device you can leave the URL parameter empty, so that the login completion will be handled by your Appwrite instance by default. +Sends the user an email with a secret key for creating a session. If the provided user ID has not been registered, a new user will be created. When the user clicks the link in the email, the user is redirected back to the URL you provided with the secret key and userId values attached to the URL query string. Use the query string parameters to submit a request to the [POST /v1/account/sessions/token](https://appwrite.io/docs/references/cloud/client-web/account#createSession) endpoint to complete the login process. The link sent to the user's email address is valid for 1 hour. A user is limited to 10 active sessions at a time by default. [Learn more about session limits](https://appwrite.io/docs/authentication-security#limits).