I need some help, sorry for my English =)
I have a API tornado request handler that received a JSON with a API method, then I parse the API method and start that specific child function method or child class method.
One of this methods is "getEvents", if the method in the received JSON "getEvents" then a first response should write and flash some API informations to the output buffer but the connection should be opened.
Then a thread is calling that collect event data from the application and when a new event is detected the thread should write and flush a JSON that include that event data to the output buffer.
Request -> API response (connection remains open) -> JSON MSG -> JSON MSG -> etc.
Is this possible to have a coroutine in a child function on a request handler and is this behavior possible to hold the connection open and send JSON MSG constantly?
Some code example would be great =)
Thank you so much.
BR Christoph
On the Tornado side, this is easy: just write your messages and await self.flush()
after each one:
async for i in some_queue:
self.write(i)
await self.flush()
But on the client side, things are trickier - most JSON parsers don't handle consecutive JSON objects very well, so you probably want some sort of explicit delimiter or other framing. If you want to support browser clients, you should use either websockets or server-sent events.