Search code examples
symfonywebsocketratchetautobahn

Websocket disconnects on subscribing


I'm building an application where I can see realtime changes within the logs This application is build with the Symfony v4.1. There is this bundle that has a Web Socket server and client based on Ratchet and Autobahn.js

I've setup all the requirements to make it work according to the documentation.

  • There's a Topic class:

  • pubsub routing is configured

  • The server runs

  • Client runs in javascript when page is loaded

The script to connect works fine, until I subscribe to a channel/topic. The connection is immediately closed on the client side, without the server detecting it. Does anyone know how to solve this? Also, I'm curious what this responsecode WS-1007 means.

Javascript:

var ws = WS.connect("ws://" + $websocket_host + ":" + $websocket_port);

ws.on("socket/connect", function(session) {
    if (window.$debug) {
        console.log("websocket connected");
    }

    console.log(session);

    session.subscribe("log/channel", function(uri, payload) {
        console.log(payload);
    });
});

ws.on("socket/disconnect", function(e) {
    if (window.$debug) {
        console.log("websocket disconnected [reason:" + e.reason + " code:" + e.code + "]");
    }
});

Javascript logs:

~ websocket connected
~ websocket disconnected [reason:Connection was closed properly [WS-1007: ] code:0]

Server logs:

14:15:39 DEBUG     [websocket] INSERT CLIENT 2926 ["user" => "s:37:"anon-19491835335b991f8bde43b229754494";"] []
14:15:39 INFO      [websocket] anon-19491835335b991f8bde43b229754494 connected ["connection_id" => 2926,"session_id" => "19491835335b991f8bde43b229754494","storage_id" => 2926] []
14:15:39 DEBUG     [websocket] GET CLIENT 2926 [] []
14:15:39 INFO      [websocket] anon-19491835335b991f8bde43b229754494 subscribe to log/channel [] []
14:15:39 DEBUG     [websocket] Matched route "shop4_log" [] []
14:15:39 DEBUG     [websocket] Matched route "shop4_log" [] []

Topic class:

namespace App\Service\WebSocket\Topic;

use Gos\Bundle\WebSocketBundle\Router\WampRequest;
use Gos\Bundle\WebSocketBundle\Topic\TopicInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\Topic;

class LogTopic implements TopicInterface
{
    public function onPublish(ConnectionInterface $connection, Topic $topic, WampRequest $request, $event, array $exclude, array $eligible)
    {
        $topic->broadcast(['msg' => $event]);
    }

    public function getName()
    {
        return "log_topic";
    }

    ....
}

pubsub.yaml

shop4_log:
    channel: log/channel
    handler:
        callback: "log_topic"

Solution

  • So, I've found the solution eventually, the topic needs to be tagged in your services configuration

    services.yaml:

    App\Service\Websocket\Topic\LogTopic:
        tags:
            - { name: gos_web_socket.topic }