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().

Aram Grigoryan
  • 740
  • 1
  • 6
  • 24
Royar
  • 611
  • 6
  • 21
  • Have you get a look in symfony profiler or in log files ? – Weenesta - Mathieu Dormeval Nov 07 '16 at 23:37
  • 1
    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 - http://joxi.ru/D2P0vdvcEvW823. And then try to push something. You must get something like this: http://joxi.ru/82QNvLviNK3QAd – Stephan Yamilov Nov 08 '16 at 09:27
  • @StephanYamilov The "--host 0.0.0.0" flag doesn't change anything. Still doesn't work. – Royar Nov 11 '16 at 16:27
  • @Royar did you ever figure this out? I'm having the exact same issues. – Laniax Mar 10 '17 at 20:32
  • 1
    @Laniax 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. – Royar Mar 11 '17 at 21:30
  • just as a comment, why using ajax in between if the client could directly push to socket? – john Smith Oct 17 '21 at 15:38
  • Also AFAIK you dont need to manually broadcast in onPush because it will be broadcasted to all subscribers anyway – john Smith Oct 17 '21 at 15:44

1 Answers1

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 }
Harmon Wood
  • 2,977
  • 1
  • 15
  • 12