feat: enhance hasSubscriber method to support query filtering

This commit is contained in:
ArnabChatterjee20k
2026-01-23 00:51:14 +05:30
parent cbb5963b29
commit 09edebd6a3
2 changed files with 52 additions and 2 deletions
+18
View File
@@ -417,6 +417,24 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
/**
* Sending test message for SDK E2E tests every 5 seconds.
*/
if ($realtime->hasSubscriber('console', Role::guests()->toString(), 'tests', [Query::equal('type', ['tests'])->toString()])) {
$payload = ['response' => 'WS:/v1/realtime:passed', 'type' => 'tests'];
$event = [
'project' => 'console',
'roles' => [Role::guests()->toString()],
'data' => [
'events' => ['test.event'],
'channels' => ['tests'],
'timestamp' => DateTime::formatTz(DateTime::now()),
'payload' => $payload,
]
];
$server->send($realtime->getSubscribers($event), json_encode([
'type' => 'event',
'data' => $event['data']
]));
}
if ($realtime->hasSubscriber('console', Role::guests()->toString(), 'tests')) {
$payload = ['response' => 'WS:/v1/realtime:passed'];
+34 -2
View File
@@ -118,9 +118,10 @@ class Realtime extends MessagingAdapter
* @param string $projectId
* @param string $role
* @param string $channel
* @param array<string> $queries
* @return bool
*/
public function hasSubscriber(string $projectId, string $role, string $channel = ''): bool
public function hasSubscriber(string $projectId, string $role, string $channel = '', array $queries = []): bool
{
//TODO: look into moving it to an abstract class in the parent class
if (empty($channel)) {
@@ -128,9 +129,40 @@ class Realtime extends MessagingAdapter
&& array_key_exists($role, $this->subscriptions[$projectId]);
}
return array_key_exists($projectId, $this->subscriptions)
$hasBasicSubscription = array_key_exists($projectId, $this->subscriptions)
&& array_key_exists($role, $this->subscriptions[$projectId])
&& array_key_exists($channel, $this->subscriptions[$projectId][$role]);
if (!$hasBasicSubscription) {
return false;
}
if (empty($queries)) {
return true;
}
$queries = Realtime::convertQueries($queries);
$queryStrings = array_map(fn ($query) => $query->toString(), $queries);
foreach (array_keys($this->subscriptions[$projectId][$role][$channel]) as $connectionId) {
if (!isset($this->connections[$connectionId])) {
continue;
}
$connection = $this->connections[$connectionId];
if ($connection['projectId'] === $projectId && isset($connection['channels'][$channel])) {
$connectionQueries = $connection['queries'] ?? [];
if (empty($connectionQueries)) {
continue;
}
$connectionQueryStrings = array_map(fn ($query) => $query->toString(), $connectionQueries);
if (empty(array_diff($queryStrings, $connectionQueryStrings))) {
return true;
}
}
}
return false;
}
/**