Search code examples
pythonsocketswebsockettornadopython-asyncio

How to broadcast a message to tornado socket clients all at once?


I'm running a tornado app but I realized that everytime there is a new connection to the socket, it creates a new instance of the server instead of adding the new connection to self.connections. Because of this behaviour, I can't broadcast a message to all the connections at the same time. How can I make the app run using an existing instance?

import asyncio
import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.wsgi
import time


class EchoWebSocket(tornado.websocket.WebSocketHandler):

    def initialize(self, tornado_output_queue):
        self.connections = set()

    def open(self):
        print("WebSocket opened")
        self.connections.add(self)

    def on_message(self, message):
        for client in self.connections:
            await client.write_message(str(time.time()))

    def on_close(self):
        print("WebSocket closed")

    def check_origin(self, origin):
        return True


def make_app():
    "initializes the web server"
    return tornado.web.Application([
        (r"/websocket", EchoWebSocket)
    ])


if __name__ == "__main__":
    webapp = make_app()
    application = tornado.wsgi.WSGIContainer(webapp)
    webapp.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

I have read about tornado.ioloop.IOLoop.current Vs tornado.ioloop.IOLoop.instance (which I'm using) but the docs say .instance is just an alias of .current.


Solution

  • Tornado creates a new instance of the handler for every connection. To keep track of all the connected clients you have to create the connections object on the class:

    class EchoWebSocket(...):
        connections = set()