Search code examples
webwebsockettornado

Tornado: dynamically sending a websocket message to a javascript page?


I have a simple python process on a server A, which retrieves data from a server B.

Once the data is collected, I want to be able to send a websocket message to a javascript page, on a server C. Schematically:

A ---send data---> B -----send websocket message ---> C

How do I "dynamically" send a new websocket message to the server C? Is it possible to do so, or do I have to first receive a message from the Javascript page and then send a back response?

I'm using tornado, but I don't know how I'm supposed to instantiate the websocket.WebSocketHandler and call the write_message() function. In every example I see, there is no explicit instantiation of this class, e.g.:

app = web.Application([ ('/ws', WebSocketHandler, ), ]) # the class is just declared

How can I do this? Tornado looks really complex to me, as a newcomer to web development.

Thanks


Solution

  • The websocket connection must always be initiated by the browser. It doesn't have to send the first message; it could just open the connection and sit there waiting for a message from the server, but the browser must always start the process (because the browser knows how to talk to the server, but the server has no way to talk to the browser except in response to the browser's requests)