Symfony2 WebSocketBundle - ZMQ Push not working
Asked Answered
Y

1

19

I'm using Symfony2 to build a simple chat application. I decided to use the GeniusesOfSymfony/WebSocketBundle for my WebSocket, which is powered by Ratchet :

https://github.com/GeniusesOfSymfony/WebSocketBundle

I managed to get the chat working using PubSub, but I want to use the push integration instead: I want the client to send the message via AJAX to my Symfony2 controller, which in turn should push the message to all clients of the WebSocket.

I followed this documentation page:

https://github.com/GeniusesOfSymfony/WebSocketBundle/blob/master/Resources/docs/Pusher.md

I've tried using both ZMQ and Websocket Pusher.

With ZMQ, when I run the websocket, I get the cmd notification:

ZMQ transport listening on 127.0.0.1:5555

However, pushing messages doesn't work:

$pusher = $this->container->get("gos_web_socket.zmq.pusher");
//push(data, route_name, route_arguments)
$pusher->push(["type" => "newMessage", "text" => $request->request->get("msg")], "chat_topic");

This is the onPush method in my ChatTopic class:

class ChatTopic implements TopicInterface, PushableTopicInterface {

    public function onPush(Topic $topic, WampRequest $request, $data, $provider) {
        $topic->broadcast($data);
    }
}

The onPush method never gets called. Also, the pusher events never get fired. There doesn't appear to be an exception in the code.

With WebSocket Pusher, I'm not even able to get the service running. There is no notification in the cmd like with ZMQ, and using netstat command I was not able to detect that it's listening on port 1337. When I try to push to it, I get the exception :

Could not open socket. Reason: No connection could be made because the target machine actively refused it

Probably because there is no service listening on port 1337.

P.S. - I am on Windows 10 and using WAMP server. I've successfully installed the ZMQ extension on WAMP, as indicated in the phpinfo().

Yellow answered 1/11, 2016 at 23:15 Comment(7)
Have you get a look in symfony profiler or in log files ?Hartwell
Hello! Can u try to launch a server with this command php app/console gos:websocket:server --host 0.0.0.0. You must get something like this - joxi.ru/D2P0vdvcEvW823. And then try to push something. You must get something like this: joxi.ru/82QNvLviNK3QAdSatire
@StephanYamilov The "--host 0.0.0.0" flag doesn't change anything. Still doesn't work.Yellow
@Yellow did you ever figure this out? I'm having the exact same issues.Surfacetoair
@Surfacetoair Sadly I did not. The chat application was merely for me to learn using websocket for a multiplayer card game I was planning to make. I ended up switching to ASP.NET MVC for that project, using SignalR for the websocket. It worked out pretty well and the project is already complete. Personally, I would not suggest opting for PHP if you really need websocket.Yellow
just as a comment, why using ajax in between if the client could directly push to socket?Cartouche
Also AFAIK you dont need to manually broadcast in onPush because it will be broadcasted to all subscribers anywayCartouche
M
1

The documentation you quoted, pusher.md, is not complete. You must also register your topic as a service and tag it as an available topic for the config.

It would be best to follow the TopicSetup.md. However, here is the missing highlight.

The new ChatTopic class can be registered in the Symfony Services AppBundle/Resources/config/services.yml and tagged like so:

services:
    app.chat_topic_service:
        class: App\AppBundle\Topic\ChatTopic
        tags:
            - { name: gos_web_socket.topic }
Mahala answered 18/12, 2018 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.