Streaming responses?


#1

Hi there,

I’m starting to poke around at a project where clients will be best served by listening on a Server Sent Events stream for changes, rather than fetching state using response bodies directly. Is it practical to implement this kind of service in API Star?

I do see that it supports asyncio, which is promising (doing this with threads would be hell), but it’s not clear to me if there’s any support whatsoever for postponing sending the response, or sending part of the response, after the handler method returns.

To get a better sense of how this might work, consider the following examples, with two clients ALICE and BOB, interacting with the same server:

A: GET /events HTTP/1.1
A: Accept: application/event-stream
A:
S: 200 Okay
S: Content-Type: application/event-stream
S:

At this point, Alice’s connection goes idle.

B: POST /some/action HTTP/1.1
B: Content-Type: application/json
B:
B: {…}
S: 202 Accepted

At this point, Alice’s connection wakes up:

S: event: some-event-tag
S: data: {…}
S:

And then goes back to sleep. This pattern continues, with changes being delivered to Alice as they happen, until Alice’s client disconnects or until the server opts to terminate the stream.

The use case for this is, as you might guess, gaming. While I don’t have particularly strong latency requirements, I do need to avoid polling, and I’d strongly prefer to avoid one thread per request for obvious reasons.