added delete of presence on disconnect

This commit is contained in:
ArnabChatterjee20k
2026-04-15 13:38:34 +05:30
parent 69bc935517
commit 2ce39768c3
2 changed files with 28 additions and 1 deletions
+25
View File
@@ -1166,6 +1166,8 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re
$presence->removeAttribute('hostname');
$realtime->connections[$connection]['presences'] = array_merge($realtime->connections[$connection]['presences'] ?? [], [$presence->getId()]);
$responsePayload = json_encode([
'type' => 'response',
'data' => [
@@ -1228,6 +1230,29 @@ $server->onClose(function (int $connection) use ($realtime, $stats, $register) {
$register->get('telemetry.connectionCounter')->add(-1);
$projectId = $realtime->connections[$connection]['projectId'];
$presenceIds = $realtime->connections[$connection]['presences'] ?? [];
if (
!empty($presenceIds)
&& $projectId !== 'console'
) {
/** @var string[] $presenceIds */
$presenceIds = \array_values(\array_unique(\array_filter($presenceIds, fn (mixed $id): bool => \is_string($id) && $id !== '')));
if (!empty($presenceIds)) {
$consoleDB = getConsoleDB();
$project = $consoleDB->getAuthorization()->skip(fn () => $consoleDB->getDocument('projects', $projectId));
if (!$project->isEmpty()) {
$dbForProject = getProjectDB($project);
$dbForProject->deleteDocuments('presenceLogs', [
Query::equal('$id', $presenceIds),
], onError: function (Throwable $th) {
// Swallow errors to avoid breaking disconnect cleanup
});
}
}
}
triggerStats([
METRIC_REALTIME_CONNECTIONS => -1,
+3 -1
View File
@@ -22,6 +22,7 @@ class Realtime extends MessagingAdapter
* 'roles' -> [ROLE_x, ROLE_Y]
* 'userId' -> [USER_ID]
* 'channels' -> [CHANNEL_NAME_X, CHANNEL_NAME_Y, CHANNEL_NAME_Z]
* 'presences' -> [PRESENCE_ID_1, PRESENCE_ID_2, ...]
*/
public array $connections = [];
@@ -120,7 +121,8 @@ class Realtime extends MessagingAdapter
'projectId' => $projectId,
'roles' => $roles,
'userId' => $userId ?? ($this->connections[$identifier]['userId'] ?? ''),
'channels' => $channels
'channels' => $channels,
'presences' => $this->connections[$identifier]['presences'] ?? []
];
}