diff --git a/app/realtime.php b/app/realtime.php index 3221f83e95..a0572c70ff 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -10,6 +10,12 @@ use Appwrite\Extend\Exception as AppwriteException; use Appwrite\Messaging\Adapter\Realtime; use Appwrite\Network\Validator\Origin; use Appwrite\PubSub\Adapter\Pool as PubSubPool; +use Appwrite\Realtime\Message\Dispatcher as MessageDispatcher; +use Appwrite\Realtime\Message\Handlers\Authentication as AuthenticationHandler; +use Appwrite\Realtime\Message\Handlers\Ping as PingHandler; +use Appwrite\Realtime\Message\Handlers\Presence as PresenceHandler; +use Appwrite\Realtime\Message\Handlers\Subscribe as SubscribeHandler; +use Appwrite\Realtime\Message\Handlers\Unsubscribe as UnsubscribeHandler; use Appwrite\Usage\Context; use Appwrite\Utopia\Database\Documents\User; use Appwrite\Utopia\Request; @@ -22,9 +28,6 @@ use Swoole\Table; use Swoole\Timer; use Utopia\Abuse\Abuse; use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimitRedis; -use Utopia\Auth\Hashes\Sha; -use Utopia\Auth\Proofs\Token; -use Utopia\Auth\Store; use Utopia\Cache\Adapter\Pool as CachePool; use Utopia\Cache\Adapter\Sharding; use Utopia\Cache\Cache; @@ -406,7 +409,6 @@ $container->set('pools', function ($register) { if (!$container->has('queueForEvents')) { $container->set('queueForEvents', function ($pools) { - var_dump("ce"); return new QueueEvent(new BrokerPool( publisher: $pools->get('publisher') )); @@ -422,6 +424,13 @@ if (!$container->has('queueForRealtime')) { $realtime = getRealtime(); $presenceState = new PresenceState(); +$messageDispatcher = (new MessageDispatcher()) + ->register(new PingHandler()) + ->register(new AuthenticationHandler()) + ->register(new SubscribeHandler()) + ->register(new UnsubscribeHandler()) + ->register(new PresenceHandler()); + /** * Table for statistics across all workers. */ @@ -1113,15 +1122,12 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server, } }); -$server->onMessage(function (int $connection, string $message) use ($server, $realtime, $containerId, $register, $presenceState) { +$server->onMessage(function (int $connection, string $message) use ($container, $server, $realtime, $containerId, $register, $presenceState, $messageDispatcher) { $project = null; $authorization = null; $projectId = $realtime->connections[$connection]['projectId'] ?? null; $rawSize = \strlen($message); $messageType = 'invalid'; - $subscriptionDelta = 0; - $subscriptionsRequested = 0; - $subscriptionsRemoved = 0; $outboundBytes = 0; $responseCode = 200; $success = false; @@ -1130,6 +1136,13 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re Span::add('realtime.connectionId', $connection); Span::add('realtime.inboundBytes', $rawSize); Span::add('realtime.containerId', $containerId); + // Defaults so an exception thrown mid-handler still leaves these attrs on the span. + // Handlers (Subscribe/Unsubscribe/Authentication) override with real values via Span::add + // when their work succeeds. + Span::add('realtime.subscriptionDelta', 0); + Span::add('realtime.subscriptionsRequested', 0); + Span::add('realtime.subscriptionsRemoved', 0); + Span::add('realtime.subscribe.subscriptionsCount', 0); try { $response = new Response(new SwooleResponse()); @@ -1181,6 +1194,7 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re } // Record realtime inbound bytes for this project + // not making this a part of the dispatcher as we need to get the inbound bytes as well even if we dont enter the dispatcher if ($project !== null && !$project->isEmpty()) { triggerStats([ METRIC_REALTIME_INBOUND => $rawSize, @@ -1199,378 +1213,46 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Message type is not valid.'); } - // Ping does not require project context; other messages do (e.g. after unsubscribe during auth) - if (empty($projectId) && ($message['type'] ?? '') !== 'ping') { - throw new Exception(Exception::REALTIME_POLICY_VIOLATION, 'Missing project context. Reconnect to the project first.'); - } + // Child of the global container: per-message values like $connection and $project + // live on this scope so concurrent message coroutines don't clobber each other, + // while globally-registered services (pools, queueForRealtime, ...) remain reachable + // via the parent. + $messageContainer = new Container($container); + $messageContainer->set('connectionId', fn () => $connection); + $messageContainer->set('server', fn () => $server); + $messageContainer->set('realtime', fn () => $realtime); + $messageContainer->set('register', fn () => $register); + $messageContainer->set('response', fn () => $response); + $messageContainer->set('presenceState', fn () => $presenceState); + $messageContainer->set('database', fn () => $database); + $messageContainer->set('authorization', fn () => $authorization); + $messageContainer->set('project', fn () => $project); + $messageContainer->set('projectId', fn () => $projectId); + // Wrap the global helpers as first-class callables so handlers receive them via + // injection rather than reaching into the global namespace. Keeps PresenceHandler + // unit-testable without bootstrapping app/realtime.php. + $messageContainer->set('triggerPresenceUsage', fn () => triggerPresenceUsage(...)); + $messageContainer->set('triggerPresenceEvent', fn () => triggerPresenceEvent(...)); - switch ($message['type']) { - case 'ping': - $pongPayloadJson = json_encode([ - 'type' => 'pong' - ]); + $responsePayload = $messageDispatcher->dispatch($messageContainer, $message); - $server->send([$connection], $pongPayloadJson); - $outboundBytes += \strlen($pongPayloadJson); - - if ($project !== null && !$project->isEmpty()) { - $pongOutboundBytes = \strlen($pongPayloadJson); - - if ($pongOutboundBytes > 0) { - triggerStats([ - METRIC_REALTIME_OUTBOUND => $pongOutboundBytes, - ], $project->getId()); - } - } - - break; - case 'authentication': - if (!array_key_exists('session', $message['data'])) { - throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Payload is not valid.'); - } - - $store = new Store(); - - $store->decode($message['data']['session']); - - /** @var User $user */ - $user = $database->getDocument('users', $store->getProperty('id', '')); - - /** - * TODO: - * Moving forward, we should try to use our dependency injection container - * to inject the proof for token. - * This way we will have one source of truth for the proof for token. - */ - $proofForToken = new Token(); - $proofForToken->setHash(new Sha()); - - if ( - empty($user->getId()) // Check a document has been found in the DB - || !$user->sessionVerify($store->getProperty('secret', ''), $proofForToken) // Validate user has valid login token - ) { - // cookie not valid - throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Session is not valid.'); - } - - $roles = $user->getRoles($database->getAuthorization()); - - $authorization = $realtime->connections[$connection]['authorization'] ?? null; - $projectId = $realtime->connections[$connection]['projectId'] ?? null; - // Capture the pre-auth userId so we can rebind any account channels - // that were stored under it (e.g. guest who subscribed to `account` - // and now authenticates). unsubscribe() below clears the connection - // entry, so we must read it first. - $previousUserId = $realtime->connections[$connection]['userId'] ?? ''; - - $subscriptionsBefore = \count($realtime->getSubscriptionMetadata($connection)); - $meta = $realtime->getSubscriptionMetadata($connection); - - $realtime->unsubscribe($connection); - - if (!empty($projectId)) { - foreach ($meta as $subscriptionId => $subscription) { - $queries = Query::parseQueries($subscription['queries'] ?? []); - $channels = Realtime::rebindAccountChannels( - $subscription['channels'] ?? [], - $previousUserId, - $user->getId() - ); - - $realtime->subscribe( - $projectId, - $connection, - $subscriptionId, - $roles, - $channels, - $queries, - $user->getId() - ); - } - } - - if ($authorization !== null) { - $realtime->connections[$connection]['authorization'] = $authorization; - } - - $subscriptionsAfter = \count($realtime->getSubscriptionMetadata($connection)); - $subscriptionDelta = $subscriptionsAfter - $subscriptionsBefore; - if ($subscriptionDelta !== 0) { - $register->get('telemetry.workerSubscriptionCounter')->add($subscriptionDelta, $register->get('telemetry.workerAttributes')); - } - - $user = $response->output($user, Response::MODEL_ACCOUNT); - - $authResponsePayloadJson = json_encode([ - 'type' => 'response', - 'data' => [ - 'to' => 'authentication', - 'success' => true, - 'user' => $user - ] - ]); - - $server->send([$connection], $authResponsePayloadJson); - $outboundBytes += \strlen($authResponsePayloadJson); - - if ($project !== null && !$project->isEmpty()) { - $authOutboundBytes = \strlen($authResponsePayloadJson); - - if ($authOutboundBytes > 0) { - triggerStats([ - METRIC_REALTIME_OUTBOUND => $authOutboundBytes, - ], $project->getId()); - } - } - - break; - - case 'subscribe': - /** - * Message based upsertion of a subscription - * If subscriptionId is given then it will match subId of the connection and update the subscription with channels and queries - * If non-existing subid is given or not given a new subid will be generated - * Similar to what we have now -> two subscribe() block with same channels and queries still two different subscriptions - * - * structure of the payload -> array of maps - * 'data' : [subscriptionId:"" , channels:[] , queries:[]] - */ - if (!is_array($message['data']) || !array_is_list($message['data'])) { - throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Payload is not valid.'); - } - - $roles = $realtime->connections[$connection]['roles'] ?? [Role::guests()->toString()]; - $userId = $realtime->connections[$connection]['userId'] ?? ''; - - // bulk validation + parsing before subscribing - $parsedPayloads = []; - $subscriptionsBefore = \count($realtime->getSubscriptionMetadata($connection)); - foreach ($message['data'] as $payload) { - if (!\is_array($payload)) { - throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Each subscribe payload must be an object.'); - } - if (!array_key_exists('channels', $payload)) { - throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'channels is not present in payload.'); - } - if (!is_array($payload['channels']) || !array_is_list($payload['channels'])) { - throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'channels is not a valid array.'); - } - // registering the queries if not present and check in the same payload later on - if (!array_key_exists('queries', $payload)) { - $payload['queries'] = []; - } - if (!is_array($payload['queries']) || !array_is_list($payload['queries'])) { - throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'queries is not a valid array.'); - } - - $subscriptionId = \array_key_exists('subscriptionId', $payload) - ? $payload['subscriptionId'] - : ID::unique(); - - try { - $convertedQueries = Realtime::convertQueries($payload['queries']); - } catch (QueryException $e) { - throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Invalid query: ' . $e->getMessage()); - } - - $convertedChannels = \array_keys(Realtime::convertChannels($payload['channels'], $userId)); - - $parsedPayloads[] = [ - 'subscriptionId' => $subscriptionId, - 'channels' => $payload['channels'], - 'convertedChannels' => $convertedChannels, - 'queries' => $convertedQueries, - ]; - } - - foreach ($parsedPayloads as $parsedPayload) { - $subscriptionId = $parsedPayload['subscriptionId']; - $channels = $parsedPayload['convertedChannels']; - $queries = $parsedPayload['queries']; - $realtime->subscribe($projectId, $connection, $subscriptionId, $roles, $channels, $queries); - } - $subscriptionsAfter = \count($realtime->getSubscriptionMetadata($connection)); - $subscriptionDelta = $subscriptionsAfter - $subscriptionsBefore; - $subscriptionsRequested = \count($parsedPayloads); - if ($subscriptionDelta !== 0) { - $register->get('telemetry.workerSubscriptionCounter')->add($subscriptionDelta, $register->get('telemetry.workerAttributes')); - } - - $responsePayload = json_encode([ - 'type' => 'response', - 'data' => [ - 'to' => 'subscribe', - 'success' => true, - 'subscriptions' => \array_map(function (array $parsedPayload) { - return [ - 'subscriptionId' => $parsedPayload['subscriptionId'], - 'channels' => $parsedPayload['convertedChannels'], - 'queries' => \array_map(fn ($q) => $q->toString(), $parsedPayload['queries']), - ]; - }, $parsedPayloads), - ] - ]); - - $server->send([$connection], $responsePayload); - $outboundBytes += \strlen($responsePayload); - - if ($project !== null && !$project->isEmpty()) { - $subscribeOutboundBytes = \strlen($responsePayload); - - if ($subscribeOutboundBytes > 0) { - triggerStats([ - METRIC_REALTIME_OUTBOUND => $subscribeOutboundBytes, - ], $project->getId()); - } - } - - break; - - case 'unsubscribe': - if (!\is_array($message['data']) || !\array_is_list($message['data'])) { - throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Payload is not valid.'); - } - - $subscriptionsBefore = \count($realtime->getSubscriptionMetadata($connection)); - - // Validate every payload before executing any removal so an invalid entry - // later in the batch does not leave earlier entries half-applied on the server. - $validatedIds = []; - foreach ($message['data'] as $payload) { - if ( - !\is_array($payload) - || !\array_key_exists('subscriptionId', $payload) - || !\is_string($payload['subscriptionId']) - || $payload['subscriptionId'] === '' - ) { - throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Each unsubscribe payload must include a non-empty subscriptionId.'); - } - $validatedIds[] = $payload['subscriptionId']; - } - - $unsubscribeResults = []; - foreach ($validatedIds as $subscriptionId) { - $wasRemoved = $realtime->unsubscribeSubscription($connection, $subscriptionId); - $unsubscribeResults[] = [ - 'subscriptionId' => $subscriptionId, - 'removed' => $wasRemoved, - ]; - } - $subscriptionsAfter = \count($realtime->getSubscriptionMetadata($connection)); - $subscriptionDelta = $subscriptionsAfter - $subscriptionsBefore; - $subscriptionsRequested = \count($validatedIds); - $subscriptionsRemoved = \count(\array_filter($unsubscribeResults, fn (array $item) => $item['removed'])); - if ($subscriptionDelta !== 0) { - $register->get('telemetry.workerSubscriptionCounter')->add($subscriptionDelta, $register->get('telemetry.workerAttributes')); - } - - $unsubscribeResponsePayload = json_encode([ - 'type' => 'response', - 'data' => [ - 'to' => 'unsubscribe', - 'success' => true, - 'subscriptions' => $unsubscribeResults, - ], - ]); - - $server->send([$connection], $unsubscribeResponsePayload); - $outboundBytes += \strlen($unsubscribeResponsePayload); - - if ($project !== null && !$project->isEmpty()) { - $unsubscribeOutboundBytes = \strlen($unsubscribeResponsePayload); - - if ($unsubscribeOutboundBytes > 0) { - triggerStats([ - METRIC_REALTIME_OUTBOUND => $unsubscribeOutboundBytes, - ], $project->getId()); - } - } - - break; - - case 'presence': - $userId = $realtime->connections[$connection]['userId']; - if (empty($userId)) { - throw new Exception(Exception::USER_UNAUTHORIZED, 'User must be authorized'); - } - /** @var User $user */ - $user = $database->getDocument('users', $userId); - - if ($user->isEmpty()) { - throw new Exception(Exception::USER_NOT_FOUND, params:[$userId]); - } - - if (!is_array($message['data'])) { - throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Payload is not valid.'); - } - - if (!array_key_exists('status', $message['data'])) { - throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Payload is not valid. status must be provided'); - } - - if (array_key_exists('permissions', $message['data']) && !\is_array($message['data']['permissions'])) { - throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Payload is not valid. permissions must be an array.'); - } - - $presenceData = [ - 'userInternalId' => $user->getSequence(), - 'userId' => $user->getId(), - 'source' => 'realtime', - 'status' => $message['data']['status'] - ]; - - // TODO: add the created presence id to the connections connection id array for deletion at the end(bulk delete) - - if (array_key_exists('metadata', $message['data'])) { - $presenceData['metadata'] = $message['data']['metadata']; - } - $presenceDocument = new Document($presenceData); - $presenceState->setPermissions($presenceDocument, $message['data']['permissions'] ?? null, $user, $authorization); - - $presenceId = $message['data']['presenceId'] ?? 'unique()'; - $presence = $presenceState->upsertForUser( - $database, - $presenceDocument, - $presenceId, - (string) $user->getSequence(), - function () use ($project): void { - if ($project !== null && !$project->isEmpty()) { - triggerPresenceUsage(1, $project->getId()); - } - } + if ($responsePayload !== null) { + $responseJson = json_encode($responsePayload); + if ($responseJson === false) { + throw new \RuntimeException( + 'Failed to encode realtime response payload: ' . json_last_error_msg() ); + } - $presence->removeAttribute('hostname'); + $server->send([$connection], $responseJson); + $bytes = \strlen($responseJson); + $outboundBytes += $bytes; - $realtime->connections[$connection]['presences'] = array_merge($realtime->connections[$connection]['presences'] ?? [], [$presence->getId()]); - - $responsePayload = json_encode([ - 'type' => 'response', - 'data' => [ - 'to' => 'presence', - 'presence' => $presence->getArrayCopy() - ] - ]); - - $server->send([$connection], $responsePayload); - - if ($project !== null && !$project->isEmpty()) { - $subscribeOutboundBytes = \strlen($responsePayload); - - if ($subscribeOutboundBytes > 0) { - triggerStats([ - METRIC_REALTIME_OUTBOUND => $subscribeOutboundBytes, - ], $project->getId()); - } - } - - triggerPresenceEvent($project, $user, 'presences.[presenceId].upsert', $presence); - - break; - - default: - throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Message type is not valid.'); + if ($project !== null && !$project->isEmpty()) { + triggerStats([METRIC_REALTIME_OUTBOUND => $bytes], $project->getId()); + } } + $success = true; } catch (Throwable $th) { logError($th, 'realtimeMessage', project: $project, authorization: $authorization); @@ -1606,10 +1288,6 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re } finally { Span::add('realtime.success', $success); Span::add('realtime.responseCode', $responseCode); - Span::add('realtime.subscriptionDelta', $subscriptionDelta); - Span::add('realtime.subscriptionsRequested', $subscriptionsRequested); - Span::add('realtime.subscriptionsRemoved', $subscriptionsRemoved); - Span::add('realtime.subscribe.subscriptionsCount', $subscriptionsRequested); Span::add('realtime.outboundBytes', $outboundBytes); Span::add('realtime.projectId', $project?->getId() ?? $projectId); Span::add('realtime.userId', $realtime->connections[$connection]['userId'] ?? null); diff --git a/src/Appwrite/Platform/Modules/Presences/HTTP/Delete.php b/src/Appwrite/Platform/Modules/Presences/HTTP/Delete.php index 6963bda7d2..2d3950645f 100644 --- a/src/Appwrite/Platform/Modules/Presences/HTTP/Delete.php +++ b/src/Appwrite/Platform/Modules/Presences/HTTP/Delete.php @@ -39,7 +39,7 @@ class Delete extends PlatformAction ->label('sdk', new Method( namespace: 'presences', group: 'presences', - name: 'deletePresence', + name: 'delete', description: 'Delete a presence log by its unique ID.', auth: [AuthType::ADMIN, AuthType::KEY, AuthType::SESSION, AuthType::JWT], responses: [ diff --git a/src/Appwrite/Platform/Modules/Presences/HTTP/Get.php b/src/Appwrite/Platform/Modules/Presences/HTTP/Get.php index 058cb2fa6f..6c6f02e1ab 100644 --- a/src/Appwrite/Platform/Modules/Presences/HTTP/Get.php +++ b/src/Appwrite/Platform/Modules/Presences/HTTP/Get.php @@ -33,7 +33,7 @@ class Get extends PlatformAction ->label('sdk', new Method( namespace: 'presences', group: 'presences', - name: 'getPresence', + name: 'get', description: 'Get a presence log by its unique ID.', auth: [AuthType::ADMIN, AuthType::KEY, AuthType::SESSION, AuthType::JWT], responses: [ diff --git a/src/Appwrite/Platform/Modules/Presences/HTTP/Update.php b/src/Appwrite/Platform/Modules/Presences/HTTP/Update.php index 336264730d..f59e85c0ed 100644 --- a/src/Appwrite/Platform/Modules/Presences/HTTP/Update.php +++ b/src/Appwrite/Platform/Modules/Presences/HTTP/Update.php @@ -50,7 +50,7 @@ class Update extends PlatformAction new Method( namespace: 'presences', group: 'presences', - name: 'updatePresence', + name: 'update', description: 'Update a presence log by its unique ID.', auth: [AuthType::SESSION], responses: [ diff --git a/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php b/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php index 0be1c58402..65a184a805 100644 --- a/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php +++ b/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php @@ -52,7 +52,7 @@ class Upsert extends PlatformAction new Method( namespace: 'presences', group: 'presences', - name: 'upsertPresence', + name: 'upsert', description: 'Create or update a presence log by its unique ID.', auth: [AuthType::SESSION], responses: [ @@ -73,7 +73,7 @@ class Upsert extends PlatformAction new Method( namespace: 'presences', group: 'presences', - name: 'upsertPresence', + name: 'upsert', description: 'Create or update a presence log by its unique ID.', auth: [AuthType::KEY, AuthType::JWT, AuthType::ADMIN], responses: [ diff --git a/src/Appwrite/Platform/Modules/Presences/HTTP/XList.php b/src/Appwrite/Platform/Modules/Presences/HTTP/XList.php index 94710715cc..19883181a5 100644 --- a/src/Appwrite/Platform/Modules/Presences/HTTP/XList.php +++ b/src/Appwrite/Platform/Modules/Presences/HTTP/XList.php @@ -43,7 +43,7 @@ class XList extends PlatformAction ->label('sdk', new Method( namespace: 'presences', group: 'presences', - name: 'listPresences', + name: 'list', description: 'List presence logs.', auth: [AuthType::ADMIN, AuthType::KEY, AuthType::SESSION, AuthType::JWT], responses: [ diff --git a/src/Appwrite/Realtime/Message/Dispatcher.php b/src/Appwrite/Realtime/Message/Dispatcher.php new file mode 100644 index 0000000000..ad766309dd --- /dev/null +++ b/src/Appwrite/Realtime/Message/Dispatcher.php @@ -0,0 +1,142 @@ + + */ + private array $handlers = []; + + public function register(Action $handler): self + { + $labels = $handler->getLabels(); + $type = $labels[self::LABEL_MESSAGE_TYPE] + ?? throw new \LogicException('Realtime message handler is missing the messageType label.'); + + $this->handlers[$type] = $handler; + return $this; + } + + /** + * @return array + */ + public function getHandlers(): array + { + return $this->handlers; + } + + /** + * Routes a parsed websocket message to the handler that registered for its `type`, + * runs param validation + dependency injection, and returns whatever the handler returns. + * Errors propagate so the caller can render them as websocket error frames. + * + * @param Container $container per-message container resolving 'connection', 'project', + * 'projectId' and any handler-declared injections. + * @param array $message decoded inbound websocket frame: `['type' => ..., 'data' => ...]`. + * @return array|null the handler's response payload (already shaped for the + * wire), or null when the handler chooses not to reply. + */ + public function dispatch(Container $container, array $message): ?array + { + $type = $message['type'] ?? ''; + if (!\is_string($type) || !isset($this->handlers[$type])) { + throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Message type is not valid.'); + } + + $handler = $this->handlers[$type]; + $labels = $handler->getLabels(); + + $requiresProject = $labels[self::LABEL_REQUIRES_PROJECT] ?? true; + if ($requiresProject && empty($container->get('projectId'))) { + throw new Exception( + Exception::REALTIME_POLICY_VIOLATION, + 'Missing project context. Reconnect to the project first.' + ); + } + + $shape = $labels[self::LABEL_PAYLOAD_SHAPE] ?? self::PAYLOAD_SHAPE_OBJECT; + $dataPresent = \array_key_exists('data', $message); + $data = $dataPresent ? $message['data'] : null; + + $args = $this->resolveArgs($handler, $data, $shape, $container); + + return ($handler->getCallback())(...$args); + } + + /** + * Resolves the ordered argument list for the handler callback by walking the action's + * declared option sequence. Params come from the inbound `data` (for object shape) or + * the entire data value (for list shape). Injections come from the per-message container. + * + * @return array + */ + private function resolveArgs( + Action $handler, + mixed $data, + string $shape, + Container $container, + ): array { + $values = []; + $dataPresent = $data !== null; + foreach ($handler->getParams() as $key => $param) { + if ($shape === self::PAYLOAD_SHAPE_LIST) { + // The whole `data` field is the value of this single param. `present` reflects + // whether the inbound message actually contained the `data` key. + $present = $dataPresent; + $value = $dataPresent ? $data : $param['default']; + } else { + $present = \is_array($data) && \array_key_exists($key, $data); + $value = $present ? $data[$key] : $param['default']; + } + + if (!$present && !$param['optional']) { + throw new Exception( + Exception::REALTIME_MESSAGE_FORMAT_INVALID, + \sprintf(self::REQUIRED_PARAM_ERROR_FORMAT, $key), + ); + } + + if ($present && !($param['skipValidation'] ?? false)) { + $validator = $param['validator']; + if (\is_callable($validator) && !($validator instanceof \Utopia\Validator)) { + $validator = $validator(); + } + if (!$validator->isValid($value)) { + throw new Exception( + Exception::REALTIME_MESSAGE_FORMAT_INVALID, + \sprintf('%s: %s', $key, $validator->getDescription()) + ); + } + } + + $values[$key] = $value; + } + + $ordered = []; + foreach ($handler->getOptions() as $optionKey => $option) { + if (($option['type'] ?? '') === 'param') { + $name = \substr($optionKey, \strlen('param:')); + $ordered[] = $values[$name] ?? null; + } else { + $ordered[] = $container->get($option['name']); + } + } + + return $ordered; + } +} diff --git a/src/Appwrite/Realtime/Message/Handlers/Authentication.php b/src/Appwrite/Realtime/Message/Handlers/Authentication.php new file mode 100644 index 0000000000..f738d84a23 --- /dev/null +++ b/src/Appwrite/Realtime/Message/Handlers/Authentication.php @@ -0,0 +1,120 @@ +desc('Authenticate the connection with a session token') + ->label(Dispatcher::LABEL_MESSAGE_TYPE, 'authentication') + ->param('session', '', new Text(2048), 'Encoded session token') + ->inject('connectionId') + ->inject('realtime') + ->inject('database') + ->inject('register') + ->inject('response') + ->callback($this->action(...)); + } + + /** + * @return array + */ + public function action( + string $session, + int $connectionId, + Realtime $realtime, + Database $database, + Registry $register, + Response $response, + ): array { + $store = new Store(); + $store->decode($session); + + /** @var User $user */ + $user = $database->getDocument('users', $store->getProperty('id', '')); + + // TODO: move proof construction to the DI container so there's one source of truth. + $proofForToken = new Token(); + $proofForToken->setHash(new Sha()); + + if ( + empty($user->getId()) + || !$user->sessionVerify($store->getProperty('secret', ''), $proofForToken) + ) { + throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Session is not valid.'); + } + + $roles = $user->getRoles($database->getAuthorization()); + + $authorization = $realtime->connections[$connectionId]['authorization'] ?? null; + $projectId = $realtime->connections[$connectionId]['projectId'] ?? null; + // Capture the pre-auth userId before unsubscribe() clears the connection entry, + // so we can rebind any account channels that were stored under it. + $previousUserId = $realtime->connections[$connectionId]['userId'] ?? ''; + + $subscriptionsBefore = \count($realtime->getSubscriptionMetadata($connectionId)); + $meta = $realtime->getSubscriptionMetadata($connectionId); + + $realtime->unsubscribe($connectionId); + + if (!empty($projectId)) { + foreach ($meta as $subscriptionId => $subscription) { + $queries = Query::parseQueries($subscription['queries'] ?? []); + $channels = Realtime::rebindAccountChannels( + $subscription['channels'] ?? [], + $previousUserId, + $user->getId(), + ); + + $realtime->subscribe( + $projectId, + $connectionId, + $subscriptionId, + $roles, + $channels, + $queries, + $user->getId(), + ); + } + } + + if ($authorization !== null) { + $realtime->connections[$connectionId]['authorization'] = $authorization; + } + + $subscriptionsAfter = \count($realtime->getSubscriptionMetadata($connectionId)); + $subscriptionDelta = $subscriptionsAfter - $subscriptionsBefore; + if ($subscriptionDelta !== 0) { + $register->get('telemetry.workerSubscriptionCounter') + ->add($subscriptionDelta, $register->get('telemetry.workerAttributes')); + } + + Span::add('realtime.subscriptionDelta', $subscriptionDelta); + + return [ + 'type' => 'response', + 'data' => [ + 'to' => 'authentication', + 'success' => true, + 'user' => $response->output($user, Response::MODEL_ACCOUNT), + ], + ]; + } +} diff --git a/src/Appwrite/Realtime/Message/Handlers/Ping.php b/src/Appwrite/Realtime/Message/Handlers/Ping.php new file mode 100644 index 0000000000..4bfcfa7060 --- /dev/null +++ b/src/Appwrite/Realtime/Message/Handlers/Ping.php @@ -0,0 +1,28 @@ +desc('Reply to client heartbeat') + ->label(Dispatcher::LABEL_MESSAGE_TYPE, 'ping') + ->label(Dispatcher::LABEL_REQUIRES_PROJECT, false) + ->callback($this->action(...)); + } + + /** + * @return array + */ + public function action(): array + { + return [ + 'type' => 'pong', + ]; + } +} diff --git a/src/Appwrite/Realtime/Message/Handlers/Presence.php b/src/Appwrite/Realtime/Message/Handlers/Presence.php new file mode 100644 index 0000000000..fbab6ec70e --- /dev/null +++ b/src/Appwrite/Realtime/Message/Handlers/Presence.php @@ -0,0 +1,116 @@ +desc('Upsert a presence document for the authenticated user') + ->label(Dispatcher::LABEL_MESSAGE_TYPE, 'presence') + ->param('status', '', new Text(2048), 'Presence status') + ->param('presenceId', 'unique()', new Text(36), 'Presence document ID', true) + ->param('metadata', null, new JSON(), 'Optional metadata payload', true, [], true) + ->param('permissions', null, new ArrayList(new Text(2048)), 'Optional permissions list', true) + ->inject('connectionId') + ->inject('realtime') + ->inject('database') + ->inject('authorization') + ->inject('presenceState') + ->inject('project') + ->inject('triggerPresenceUsage') + ->inject('triggerPresenceEvent') + ->callback($this->action(...)); + } + + /** + * @param array|null $permissions + * @param Closure(int, string): void $triggerPresenceUsage + * @param Closure(?Document, User, string, Document): void $triggerPresenceEvent + * @return array + */ + public function action( + string $status, + string $presenceId, + mixed $metadata, + ?array $permissions, + int $connectionId, + Realtime $realtime, + Database $database, + Authorization $authorization, + PresenceState $presenceState, + ?Document $project, + Closure $triggerPresenceUsage, + Closure $triggerPresenceEvent, + ): array { + if ($project === null || $project->isEmpty()) { + throw new Exception(Exception::REALTIME_POLICY_VIOLATION, 'Presence requires a project context.'); + } + + $userId = $realtime->connections[$connectionId]['userId'] ?? ''; + if (empty($userId)) { + throw new Exception(Exception::USER_UNAUTHORIZED, 'User must be authorized'); + } + + /** @var User $user */ + $user = $database->getDocument('users', $userId); + if ($user->isEmpty()) { + throw new Exception(Exception::USER_NOT_FOUND, params: [$userId]); + } + + $presenceData = [ + 'userInternalId' => $user->getSequence(), + 'userId' => $user->getId(), + 'source' => 'realtime', + 'status' => $status, + ]; + if ($metadata !== null) { + $presenceData['metadata'] = $metadata; + } + + $presenceDocument = new Document($presenceData); + $presenceState->setPermissions($presenceDocument, $permissions, $user, $authorization); + + $presence = $presenceState->upsertForUser( + $database, + $presenceDocument, + $presenceId, + (string) $user->getSequence(), + function () use ($project, $triggerPresenceUsage): void { + $triggerPresenceUsage(1, $project->getId()); + }, + ); + + $presence->removeAttribute('hostname'); + + $realtime->connections[$connectionId]['presences'] = \array_merge( + $realtime->connections[$connectionId]['presences'] ?? [], + [$presence->getId()], + ); + + $triggerPresenceEvent($project, $user, 'presences.[presenceId].upsert', $presence); + + return [ + 'type' => 'response', + 'data' => [ + 'to' => 'presence', + 'presence' => $presence->getArrayCopy(), + ], + ]; + } +} diff --git a/src/Appwrite/Realtime/Message/Handlers/Subscribe.php b/src/Appwrite/Realtime/Message/Handlers/Subscribe.php new file mode 100644 index 0000000000..76a4e5d3b8 --- /dev/null +++ b/src/Appwrite/Realtime/Message/Handlers/Subscribe.php @@ -0,0 +1,109 @@ +desc('Bulk subscribe to realtime channels') + ->label(Dispatcher::LABEL_MESSAGE_TYPE, 'subscribe') + ->label(Dispatcher::LABEL_PAYLOAD_SHAPE, Dispatcher::PAYLOAD_SHAPE_LIST) + ->param('items', null, fn () => new SubscribePayloadValidator(), 'Subscriptions to add') + ->inject('connectionId') + ->inject('realtime') + ->inject('register') + ->inject('projectId') + ->callback($this->action(...)); + } + + /** + * @param array, queries?: array, subscriptionId?: string}> $items + * @return array + */ + public function action( + array $items, + int $connectionId, + Realtime $realtime, + Registry $register, + ?string $projectId, + ): array { + $roles = $realtime->connections[$connectionId]['roles'] ?? [Role::guests()->toString()]; + $userId = $realtime->connections[$connectionId]['userId'] ?? ''; + + $parsedPayloads = []; + $subscriptionsBefore = \count($realtime->getSubscriptionMetadata($connectionId)); + + foreach ($items as $payload) { + $subscriptionId = \array_key_exists('subscriptionId', $payload) + ? $payload['subscriptionId'] + : ID::unique(); + + $queries = $payload['queries'] ?? []; + + try { + $convertedQueries = Realtime::convertQueries($queries); + } catch (QueryException $e) { + throw new Exception(Exception::REALTIME_MESSAGE_FORMAT_INVALID, 'Invalid query: ' . $e->getMessage()); + } + + $convertedChannels = \array_keys(Realtime::convertChannels($payload['channels'], $userId)); + + $parsedPayloads[] = [ + 'subscriptionId' => $subscriptionId, + 'channels' => $payload['channels'], + 'convertedChannels' => $convertedChannels, + 'queries' => $convertedQueries, + ]; + } + + foreach ($parsedPayloads as $parsedPayload) { + $realtime->subscribe( + $projectId, + $connectionId, + $parsedPayload['subscriptionId'], + $roles, + $parsedPayload['convertedChannels'], + $parsedPayload['queries'], + ); + } + + $subscriptionsAfter = \count($realtime->getSubscriptionMetadata($connectionId)); + $subscriptionDelta = $subscriptionsAfter - $subscriptionsBefore; + $subscriptionsRequested = \count($parsedPayloads); + + if ($subscriptionDelta !== 0) { + $register->get('telemetry.workerSubscriptionCounter') + ->add($subscriptionDelta, $register->get('telemetry.workerAttributes')); + } + + Span::add('realtime.subscriptionDelta', $subscriptionDelta); + Span::add('realtime.subscriptionsRequested', $subscriptionsRequested); + Span::add('realtime.subscribe.subscriptionsCount', $subscriptionsRequested); + + return [ + 'type' => 'response', + 'data' => [ + 'to' => 'subscribe', + 'success' => true, + 'subscriptions' => \array_map(static fn (array $parsed): array => [ + 'subscriptionId' => $parsed['subscriptionId'], + 'channels' => $parsed['convertedChannels'], + 'queries' => \array_map(static fn ($q) => $q->toString(), $parsed['queries']), + ], $parsedPayloads), + ], + ]; + } +} diff --git a/src/Appwrite/Realtime/Message/Handlers/Unsubscribe.php b/src/Appwrite/Realtime/Message/Handlers/Unsubscribe.php new file mode 100644 index 0000000000..1e23a40a7f --- /dev/null +++ b/src/Appwrite/Realtime/Message/Handlers/Unsubscribe.php @@ -0,0 +1,74 @@ +desc('Bulk remove subscriptions by id') + ->label(Dispatcher::LABEL_MESSAGE_TYPE, 'unsubscribe') + ->label(Dispatcher::LABEL_PAYLOAD_SHAPE, Dispatcher::PAYLOAD_SHAPE_LIST) + ->param('items', null, fn () => new UnsubscribePayloadValidator(), 'Subscriptions to remove') + ->inject('connectionId') + ->inject('realtime') + ->inject('register') + ->callback($this->action(...)); + } + + /** + * @param array $items + * @return array + */ + public function action( + array $items, + int $connectionId, + Realtime $realtime, + Registry $register, + ): array { + $subscriptionsBefore = \count($realtime->getSubscriptionMetadata($connectionId)); + + $unsubscribeResults = []; + foreach ($items as $payload) { + $subscriptionId = $payload['subscriptionId']; + $unsubscribeResults[] = [ + 'subscriptionId' => $subscriptionId, + 'removed' => $realtime->unsubscribeSubscription($connectionId, $subscriptionId), + ]; + } + + $subscriptionsAfter = \count($realtime->getSubscriptionMetadata($connectionId)); + $subscriptionDelta = $subscriptionsAfter - $subscriptionsBefore; + $subscriptionsRequested = \count($items); + $subscriptionsRemoved = \count(\array_filter( + $unsubscribeResults, + static fn (array $item): bool => $item['removed'] + )); + + if ($subscriptionDelta !== 0) { + $register->get('telemetry.workerSubscriptionCounter') + ->add($subscriptionDelta, $register->get('telemetry.workerAttributes')); + } + + Span::add('realtime.subscriptionDelta', $subscriptionDelta); + Span::add('realtime.subscriptionsRequested', $subscriptionsRequested); + Span::add('realtime.subscriptionsRemoved', $subscriptionsRemoved); + + return [ + 'type' => 'response', + 'data' => [ + 'to' => 'unsubscribe', + 'success' => true, + 'subscriptions' => $unsubscribeResults, + ], + ]; + } +} diff --git a/src/Appwrite/Realtime/Message/Validators/SubscribePayload.php b/src/Appwrite/Realtime/Message/Validators/SubscribePayload.php new file mode 100644 index 0000000000..3e7e5cec10 --- /dev/null +++ b/src/Appwrite/Realtime/Message/Validators/SubscribePayload.php @@ -0,0 +1,69 @@ +description; + } + + public function isArray(): bool + { + return true; + } + + public function getType(): string + { + return self::TYPE_ARRAY; + } + + public function isValid(mixed $value): bool + { + if (!\is_array($value) || !\array_is_list($value)) { + $this->description = 'Payload is not valid.'; + return false; + } + + $customId = new CustomId(); + + foreach ($value as $payload) { + if (!\is_array($payload)) { + $this->description = 'Each subscribe payload must be an object.'; + return false; + } + if (\array_key_exists('subscriptionId', $payload) && !$customId->isValid($payload['subscriptionId'])) { + $this->description = 'subscriptionId is not a valid id.'; + return false; + } + if (!\array_key_exists('channels', $payload)) { + $this->description = 'channels is not present in payload.'; + return false; + } + if (!\is_array($payload['channels']) || !\array_is_list($payload['channels'])) { + $this->description = 'channels is not a valid array.'; + return false; + } + foreach ($payload['channels'] as $channel) { + if (!\is_string($channel)) { + $this->description = 'channels must contain only strings.'; + return false; + } + } + if (\array_key_exists('queries', $payload) + && (!\is_array($payload['queries']) || !\array_is_list($payload['queries'])) + ) { + $this->description = 'queries is not a valid array.'; + return false; + } + } + + return true; + } +} diff --git a/src/Appwrite/Realtime/Message/Validators/UnsubscribePayload.php b/src/Appwrite/Realtime/Message/Validators/UnsubscribePayload.php new file mode 100644 index 0000000000..fca065dec8 --- /dev/null +++ b/src/Appwrite/Realtime/Message/Validators/UnsubscribePayload.php @@ -0,0 +1,47 @@ +description; + } + + public function isArray(): bool + { + return true; + } + + public function getType(): string + { + return self::TYPE_ARRAY; + } + + public function isValid(mixed $value): bool + { + if (!\is_array($value) || !\array_is_list($value)) { + $this->description = 'Payload is not valid.'; + return false; + } + + foreach ($value as $payload) { + if ( + !\is_array($payload) + || !\array_key_exists('subscriptionId', $payload) + || !\is_string($payload['subscriptionId']) + || $payload['subscriptionId'] === '' + ) { + $this->description = 'Each unsubscribe payload must include a non-empty subscriptionId.'; + return false; + } + } + + return true; + } +} diff --git a/tests/e2e/Services/GraphQL/PresenceTest.php b/tests/e2e/Services/GraphQL/PresenceTest.php index 077c0aa902..6087b19bde 100644 --- a/tests/e2e/Services/GraphQL/PresenceTest.php +++ b/tests/e2e/Services/GraphQL/PresenceTest.php @@ -21,7 +21,7 @@ class PresenceTest extends Scope $payload = [ 'query' => <<<'GQL' - mutation upsertPresence($presenceId: String!, $userId: String!, $status: String!, $metadata: Json) { + mutation upsert($presenceId: String!, $userId: String!, $status: String!, $metadata: Json) { presencesUpsert(presenceId: $presenceId, userId: $userId, status: $status, metadata: $metadata) { _id userId @@ -45,7 +45,6 @@ class PresenceTest extends Scope 'x-appwrite-project' => $projectId, 'x-appwrite-key' => $apiKey, ], $payload); - $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals('online', $response['body']['data']['presencesUpsert']['status']); $this->assertEquals('graphql', $response['body']['data']['presencesUpsert']['source']); diff --git a/tests/e2e/Services/Presence/PresenceRealtimeClientTest.php b/tests/e2e/Services/Presence/PresenceRealtimeClientTest.php index e03dba110d..ce5267b10a 100644 --- a/tests/e2e/Services/Presence/PresenceRealtimeClientTest.php +++ b/tests/e2e/Services/Presence/PresenceRealtimeClientTest.php @@ -382,7 +382,7 @@ class PresenceRealtimeClientTest extends Scope ], ])); $missingStatus = $this->receiveErrorMessage($client); - $this->assertStringContainsString('status must be provided', $missingStatus['data']['message'] ?? ''); + $this->assertStringContainsString('Payload is not valid. status is required', $missingStatus['data']['message'] ?? ''); $this->assertQuietFor( $client, fn (array $frame): bool => ($frame['type'] ?? null) === 'event' @@ -398,7 +398,7 @@ class PresenceRealtimeClientTest extends Scope ], ])); $invalidPermissions = $this->receiveErrorMessage($client); - $this->assertStringContainsString('permissions must be an array', $invalidPermissions['data']['message'] ?? ''); + $this->assertStringContainsString('permissions: Value must a valid array', $invalidPermissions['data']['message'] ?? ''); $this->assertQuietFor( $client, fn (array $frame): bool => ($frame['type'] ?? null) === 'event' diff --git a/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php b/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php index 3da00898c9..feebe8839e 100644 --- a/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php +++ b/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php @@ -257,7 +257,7 @@ class RealtimeConsoleClientTest extends Scope $this->assertEquals('error', $response['type']); $this->assertNotEmpty($response['data']); $this->assertEquals(1003, $response['data']['code']); - $this->assertEquals('Payload is not valid.', $response['data']['message']); + $this->assertEquals('Payload is not valid. session is required', $response['data']['message']); $client->send(\json_encode([ 'type' => 'unknown', diff --git a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php index 813ef70ff0..436de5b1d0 100644 --- a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php +++ b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php @@ -239,7 +239,7 @@ class RealtimeCustomClientTest extends Scope $this->assertEquals('error', $response['type']); $this->assertNotEmpty($response['data']); $this->assertEquals(1003, $response['data']['code']); - $this->assertEquals('Payload is not valid.', $response['data']['message']); + $this->assertEquals('Payload is not valid. session is required', $response['data']['message']); $client->send(\json_encode([ 'type' => 'unknown',