From 69738aed9a3c2d0eaff3001f07429cc7eaaba9a8 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 2 Sep 2024 18:14:40 +0300 Subject: [PATCH] realtime connection --- app/init.php | 9 +++++++ src/Appwrite/Messaging/Adapter/Realtime.php | 26 +++++++++++++++++---- src/Appwrite/Platform/Workers/Databases.php | 8 ------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/app/init.php b/app/init.php index d91483d7cb..66e6aee049 100644 --- a/app/init.php +++ b/app/init.php @@ -36,10 +36,12 @@ use Appwrite\Extend\Exception; use Appwrite\GraphQL\Promises\Adapter\Swoole; use Appwrite\GraphQL\Schema; use Appwrite\Hooks\Hooks; +use Appwrite\Messaging\Adapter\Realtime; use Appwrite\Network\Validator\Email; use Appwrite\Network\Validator\Origin; use Appwrite\OpenSSL\OpenSSL; use Appwrite\URL\URL as AppwriteURL; +use Appwrite\Utopia\Request; use MaxMind\Db\Reader; use PHPMailer\PHPMailer\PHPMailer; use Swoole\Database\PDOProxy; @@ -1457,10 +1459,17 @@ App::setResource('deviceForBuilds', function ($project, $connectionString) { return getDevice(APP_STORAGE_BUILDS.'/app-'.$project->getId(), $connectionString); }, ['project', 'connectionString']); +App::setResource('realtimeConnection', fn($pools) => $pools->get('pubsub')->pop()->getResource() +, ['pools']); + App::setResource('connectionString', function () { return System::getEnv('_APP_CONNECTIONS_STORAGE', ''); }); +Realtime::setRedis( + $register->get('pools')->get('pubsub')->pop()->getResource() +); + function getDevice(string $root, string $connectionString = ''): Device { diff --git a/src/Appwrite/Messaging/Adapter/Realtime.php b/src/Appwrite/Messaging/Adapter/Realtime.php index 55d8db2924..f2ab8c33cd 100644 --- a/src/Appwrite/Messaging/Adapter/Realtime.php +++ b/src/Appwrite/Messaging/Adapter/Realtime.php @@ -7,10 +7,12 @@ use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Role; +use Utopia\Pools\Connection; use Utopia\System\System; class Realtime extends Adapter { + /** * Connection Tree * @@ -36,6 +38,21 @@ class Realtime extends Adapter */ public array $subscriptions = []; + private static ?\Redis $redis = null; + + public static function setRedis($redis): void + { + self::$redis = $redis; + } + + /** + * @return \Redis|null + */ + public static function getRedis(): ?\Redis + { + return self::$redis; + } + /** * Adds a subscription. * @@ -124,24 +141,23 @@ class Realtime extends Adapter * Sends an event to the Realtime Server * @param string $projectId * @param array $payload - * @param string $event + * @param array $events * @param array $channels * @param array $roles * @param array $options * @return void + * @throws \RedisException */ public static function send(string $projectId, array $payload, array $events, array $channels, array $roles, array $options = []): void { if (empty($channels) || empty($roles) || empty($projectId)) { return; } - $permissionsChanged = array_key_exists('permissionsChanged', $options) && $options['permissionsChanged']; $userId = array_key_exists('userId', $options) ? $options['userId'] : null; - $redis = new \Redis(); //TODO: make this part of the constructor - $redis->connect(System::getEnv('_APP_REDIS_HOST', ''), System::getEnv('_APP_REDIS_PORT', '')); - $redis->publish('realtime', json_encode([ + + self::getRedis()->publish('realtime', json_encode([ 'project' => $projectId, 'roles' => $roles, 'permissionsChanged' => $permissionsChanged, diff --git a/src/Appwrite/Platform/Workers/Databases.php b/src/Appwrite/Platform/Workers/Databases.php index f2b3ca6a7b..56f5f012e8 100644 --- a/src/Appwrite/Platform/Workers/Databases.php +++ b/src/Appwrite/Platform/Workers/Databases.php @@ -195,14 +195,6 @@ class Databases extends Action ); } } finally { - var_dump([ - 'database'=> $database, - 'collection'=> $collection, - 'attribute'=> $attribute, - 'database'=> $database, - 'events'=> $events, - - ]); $this->trigger($database, $collection, $attribute, $project, $projectId, $events); }