Add EventStreamHttpResponse for Server-Sent Events.

Review Request #13280 — Created Sept. 20, 2023 and submitted — Latest diff uploaded

Information

Djblets
release-4.x

Reviewers

This introduces djblets.http.responses.EventStreamHttpResponse, which
can stream events and data to a client by way of the Server-Sent Events
(SSE) standard.

This works by taking a generator or callable that yields dictionaries of
message data following the spec, allowing for app-specified IDs,
text-based payload data, event names, and retry intervals (for
disconnects) to be streamed to the client.

The response class takes charge of serializing this information for the
client, and can also support resuming of streamed events by intercepting
the SSE Last-Event-ID header and passing it to an event stream
callable.

It's important for consumers of this class to be mindful of how it's
used. Browsers have a limit of 6 concurrent HTTP/1.1 requests to the
same domain across all tabs, and a long-running SSE stream will take one
of those slots, potentially starving out other requests. This is far
less of a problem with HTTP/2.

Unit tests pass.

Tested with a makeshift EventSource implementation in JavaScript,
testing that events and all fields were successfully coming in and
processing as per spec.

Tested with curl and with GZip middleware turned on and off.

Commits

Files