0

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

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. Does that mean that I will have to implement some sort of a heartbeat mechanism, so that the client periodically polls the server for new data?
The client doesn't need to poll, it just needs to create a websocket connection and leave it open (and reopen it if it fails). Then the server will be able to push things to it.
Ok, but this was then the purpose of my initial question: there does not seem to be a way to invoke tornado write_message() function explicitly, after the instantiation of the WebSocketHandler class...how is the server supposed to write a message to the client explicitly, once the connection is opened, and not necessarily when a client message arrived? Thanks
In your open() callback, you can save a reference to the WebSocketHandler object and call write_message on it later.
It is instantiated for you when the client connects. See github.com/tornadoweb/tornado/blob/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.