Merge pull request #12210 from appwrite/realtime-messsage-dispatchers

Realtime messsage dispatchers (presence api)
This commit is contained in:
Jake Barnby authored and GitHub committed 2026-05-07 19:12:56 +12:00
commit 0f67fd7618
18 files changed
+772 -390

No files matched your search

+56 -378
View File
@@ -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);
@@ -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: [
@@ -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: [
@@ -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: [
@@ -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: [
@@ -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: [
@@ -0,0 +1,142 @@
<?php
namespace Appwrite\Realtime\Message;
use Appwrite\Extend\Exception;
use Utopia\DI\Container;
use Utopia\Platform\Action;
class Dispatcher
{
public const LABEL_MESSAGE_TYPE = 'messageType';
public const LABEL_PAYLOAD_SHAPE = 'payloadShape';
public const LABEL_REQUIRES_PROJECT = 'requiresProjectContext';
public const PAYLOAD_SHAPE_OBJECT = 'object';
public const PAYLOAD_SHAPE_LIST = 'list';
private const REQUIRED_PARAM_ERROR_FORMAT = 'Payload is not valid. %s is required';
/**
* @var array<string, Action>
*/
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<string, Action>
*/
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<mixed> $message decoded inbound websocket frame: `['type' => ..., 'data' => ...]`.
* @return array<string, mixed>|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<int, mixed>
*/
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;
}
}
@@ -0,0 +1,120 @@
<?php
namespace Appwrite\Realtime\Message\Handlers;
use Appwrite\Extend\Exception;
use Appwrite\Messaging\Adapter\Realtime;
use Appwrite\Realtime\Message\Dispatcher;
use Appwrite\Utopia\Database\Documents\User;
use Appwrite\Utopia\Response;
use Utopia\Auth\Hashes\Sha;
use Utopia\Auth\Proofs\Token;
use Utopia\Auth\Store;
use Utopia\Database\Database;
use Utopia\Database\Query;
use Utopia\Platform\Action;
use Utopia\Registry\Registry;
use Utopia\Span\Span;
use Utopia\Validator\Text;
class Authentication extends Action
{
public function __construct()
{
$this
->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<string, mixed>
*/
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),
],
];
}
}
@@ -0,0 +1,28 @@
<?php
namespace Appwrite\Realtime\Message\Handlers;
use Appwrite\Realtime\Message\Dispatcher;
use Utopia\Platform\Action;
class Ping extends Action
{
public function __construct()
{
$this
->desc('Reply to client heartbeat')
->label(Dispatcher::LABEL_MESSAGE_TYPE, 'ping')
->label(Dispatcher::LABEL_REQUIRES_PROJECT, false)
->callback($this->action(...));
}
/**
* @return array<string, mixed>
*/
public function action(): array
{
return [
'type' => 'pong',
];
}
}
@@ -0,0 +1,116 @@
<?php
namespace Appwrite\Realtime\Message\Handlers;
use Appwrite\Databases\PresenceState;
use Appwrite\Extend\Exception;
use Appwrite\Messaging\Adapter\Realtime;
use Appwrite\Realtime\Message\Dispatcher;
use Appwrite\Utopia\Database\Documents\User;
use Closure;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Validator\Authorization;
use Utopia\Platform\Action;
use Utopia\Validator\ArrayList;
use Utopia\Validator\JSON;
use Utopia\Validator\Text;
class Presence extends Action
{
public function __construct()
{
$this
->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<int, string>|null $permissions
* @param Closure(int, string): void $triggerPresenceUsage
* @param Closure(?Document, User, string, Document): void $triggerPresenceEvent
* @return array<string, mixed>
*/
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(),
],
];
}
}
@@ -0,0 +1,109 @@
<?php
namespace Appwrite\Realtime\Message\Handlers;
use Appwrite\Extend\Exception;
use Appwrite\Messaging\Adapter\Realtime;
use Appwrite\Realtime\Message\Dispatcher;
use Appwrite\Realtime\Message\Validators\SubscribePayload as SubscribePayloadValidator;
use Utopia\Database\Exception\Query as QueryException;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Role;
use Utopia\Platform\Action;
use Utopia\Registry\Registry;
use Utopia\Span\Span;
class Subscribe extends Action
{
public function __construct()
{
$this
->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<int, array{channels: array<int, string>, queries?: array<int, string>, subscriptionId?: string}> $items
* @return array<string, mixed>
*/
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),
],
];
}
}
@@ -0,0 +1,74 @@
<?php
namespace Appwrite\Realtime\Message\Handlers;
use Appwrite\Messaging\Adapter\Realtime;
use Appwrite\Realtime\Message\Dispatcher;
use Appwrite\Realtime\Message\Validators\UnsubscribePayload as UnsubscribePayloadValidator;
use Utopia\Platform\Action;
use Utopia\Registry\Registry;
use Utopia\Span\Span;
class Unsubscribe extends Action
{
public function __construct()
{
$this
->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<int, array{subscriptionId: string}> $items
* @return array<string, mixed>
*/
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,
],
];
}
}
@@ -0,0 +1,69 @@
<?php
namespace Appwrite\Realtime\Message\Validators;
use Appwrite\Utopia\Database\Validator\CustomId;
use Utopia\Validator;
class SubscribePayload extends Validator
{
protected string $description = 'Payload is not valid.';
public function getDescription(): string
{
return $this->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;
}
}
@@ -0,0 +1,47 @@
<?php
namespace Appwrite\Realtime\Message\Validators;
use Utopia\Validator;
class UnsubscribePayload extends Validator
{
protected string $description = 'Payload is not valid.';
public function getDescription(): string
{
return $this->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;
}
}
+1 -2
View File
@@ -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']);
@@ -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'
@@ -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',
@@ -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',