Skip to content

EspoCRM – Open Source CRM Application

websocket changes

Yuri Kuznetsovfa85d9e1326a
diff --git a/application/Espo/Core/WebSocket/Pusher.php b/application/Espo/Core/WebSocket/Pusher.php
index d681869964..ab78c0bcde 100644
--- a/application/Espo/Core/WebSocket/Pusher.php
+++ b/application/Espo/Core/WebSocket/Pusher.php
@@ -29,10 +29,12 @@
namespace Espo\Core\WebSocket;
+use Espo\Core\Utils\Json;
use GuzzleHttp\Psr7\Query;
use Psr\Http\Message\RequestInterface;
use Ratchet\ConnectionInterface;
+use Ratchet\Wamp\ServerProtocol as WAMP;
use Ratchet\Wamp\Topic;
use Ratchet\Wamp\WampConnection;
use Ratchet\Wamp\WampServerInterface;
@@ -408,6 +410,8 @@ class Pusher implements WampServerInterface
}
$this->subscribeUser($conn, $userId);
+
+ $this->sendWelcome($conn);
});
}
@@ -572,4 +576,15 @@ class Pusher implements WampServerInterface
$this->topicHash[$topicId] = $topic;
}
+
+ private function sendWelcome(ConnectionInterface $conn): void
+ {
+ /**
+ * @noinspection PhpPossiblePolymorphicInvocationInspection
+ * @phpstan-ignore property.notFound
+ */
+ $sessionId = $conn->WAMP->sessionId;
+
+ $conn->send(Json::encode([WAMP::MSG_WELCOME, $sessionId, 1]));
+ }
}
diff --git a/application/Espo/Core/WebSocket/Ratchet/EspoServerProtocol.php b/application/Espo/Core/WebSocket/Ratchet/EspoServerProtocol.php
new file mode 100644
index 0000000000..ac09dd71fb
--- /dev/null
+++ b/application/Espo/Core/WebSocket/Ratchet/EspoServerProtocol.php
@@ -0,0 +1,48 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\WebSocket\Ratchet;
+
+use Ratchet\ConnectionInterface;
+use Ratchet\Wamp\ServerProtocol;
+
+class EspoServerProtocol extends ServerProtocol
+{
+ /**
+ * @inheritDoc
+ * @phpstan-ignore missingType.return
+ */
+ public function onOpen(ConnectionInterface $conn)
+ {
+ $decor = new EspoWampConnection($conn);
+ $this->connections->attach($conn, $decor);
+
+ $this->_decorating->onOpen($decor);
+ }
+}
diff --git a/application/Espo/Core/WebSocket/Ratchet/EspoWampConnection.php b/application/Espo/Core/WebSocket/Ratchet/EspoWampConnection.php
new file mode 100644
index 0000000000..05910f3f9b
--- /dev/null
+++ b/application/Espo/Core/WebSocket/Ratchet/EspoWampConnection.php
@@ -0,0 +1,49 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\WebSocket\Ratchet;
+
+use Ratchet\ConnectionInterface;
+use Ratchet\Wamp\WampConnection;
+use stdClass;
+
+class EspoWampConnection extends WampConnection
+{
+ /**
+ * @noinspection PhpMissingParentConstructorInspection
+ */
+ public function __construct(ConnectionInterface $conn)
+ {
+ $this->wrappedConn = $conn;
+
+ $this->WAMP = new stdClass;
+ $this->WAMP->sessionId = str_replace('.', '', uniqid((string) mt_rand(), true));
+ $this->WAMP->prefixes = [];
+ }
+}
diff --git a/application/Espo/Core/WebSocket/Ratchet/EspoWampServer.php b/application/Espo/Core/WebSocket/Ratchet/EspoWampServer.php
new file mode 100644
index 0000000000..23b5218d65
--- /dev/null
+++ b/application/Espo/Core/WebSocket/Ratchet/EspoWampServer.php
@@ -0,0 +1,48 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\WebSocket\Ratchet;
+
+use Ratchet\Wamp\TopicManager;
+use Ratchet\Wamp\WampServer;
+use Ratchet\Wamp\WampServerInterface;
+
+/**
+ * Customized so that the welcome response is not sent on open. It will be sent after async access check.
+ */
+class EspoWampServer extends WampServer
+{
+ /**
+ * @noinspection PhpMissingParentConstructorInspection
+ */
+ public function __construct(WampServerInterface $app)
+ {
+ $this->wampProtocol = new EspoServerProtocol(new TopicManager($app));
+ }
+}
diff --git a/application/Espo/Core/WebSocket/ServerStarter.php b/application/Espo/Core/WebSocket/ServerStarter.php
index 421dcaf605..6e3b298fa5 100644
--- a/application/Espo/Core/WebSocket/ServerStarter.php
+++ b/application/Espo/Core/WebSocket/ServerStarter.php
@@ -38,7 +38,6 @@ use React\Socket\SecureServer as SocketSecureServer;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
-use Ratchet\Wamp\WampServer;
/**
* Starts a web-socket server.
@@ -90,7 +89,7 @@ class ServerStarter
$socketServer = new SocketSecureServer($socketServer, $loop, $sslParams);
}
- $wsServer = new WsServer(new WampServer($pusher));
+ $wsServer = new WsServer(new Ratchet\EspoWampServer($pusher));
$wsServer->enableKeepAlive($loop, 60);
new IoServer(