The problem wasn't actually with DEFLATE, zlib, or any actual framing, but with the EventSource
client. Embarrassing.
The Server-Sent Events specification defines three events on the EventSource
object, open
, error
, and message
. message
fires for all messages emitted on the stream. Any other events subscribed to with addEventListener
serve as sugar for filtering by server-specified event types, using the event
syntax, e.g.
event: <event-name>
data: ...
In other words, changing this line in client.html
:
es.addEventListener('data', console.log);
to
es.addEventListener('message', console.log);
Will cause the web browser to correctly log all the "hi" messages from the server as they are flushed.
Re: compression, the code in the question does generate a valid DEFLATE stream, so it's all good on that part.
I think it could also be adapted to a gzip
stream with some header changes, but the only real change of gzip over DEFLATE is the integrity check at the end of the stream, as mentioned In Mark Adler's answer. In my case, I don't have a stream end, so I can never send a checksum anyway, negating any benefits of gzip.