pythonwebsockettwistedautobahn

Autobahn twisted websocket server does not send messages to client unless it receives a message from a client


I'm running an autobahn/twisted python websocket server and I'm having a problem with sending messages to clients. It seems to only sends messages to clients when a client sends it a message. For some reason, If I want to force out messages to connected clients, the message gets stuck in a queue until any client sends me a message. At that point, all the queued messages go out to all clients.

I was able to reproduce this behavior with both python clients and javascript clients against this python autobahn/twisted websocket server.

Can anyone tell me how to properly send a message from the websocket server to all connected clients? My goal is to let clients subscribe to events, then the server just sends them event messages. But for some reason this above behavior isn't letting the server send clients messages unless they first send the server a message.

You can easily reproduce this using the code below: You only need one server and one client to reproduce it. I used two different clients to prove that the issue was with the server.

Autobahn/twisted python websocket server

Javascript websocket client

Python websocket client

Regardless of which websocket client I'm using, I'm seeing the same behavior. So clearly the websocket server is queueing messages for some reason.

Server logs, every 5 seconds on the dot it claims to be sending a message.

Python client shows the difference in time between when the server sent the message and when the python client received it. Notice they come in chunks and some have a massive delay.

Javascript client gets them in chunks of 4 only after it sends the server a new message.


Solution

  • Twisted APIs are not threadsafe. With the exception of reactor.callFromThread, you may only call them in the thread the reactor is running in.

    Try not using multithreading at all in your Twisted-based program, as a start. I don't see anything in your code that calls for multiple threads.