v0.8.0
Better SSE Version 0.8.0
npm install better-sse@latest
This release adds significant performance improvements as well as adding an intermediary data buffer to each session, support for HTTP/2 and the ability to overwrite the default response headers.
Changes
⚠ BREAKING: Session
s will now buffer data before sending it to the client
The Session
class has been updated to buffer all data until it is explicitly flushed to the client.
This significantly improves performance due to less calls to the underlying res.write
method, as well as reducing overall bandwidth usage by sending all data at once (and thus less TCP ACKs being sent back and forth).
If you only use the helper methods push
, iterate
and stream
this change should be transparent to you. More advanced users using the individual field-writing methods event
, data
, id
, retry
or comment
should keep in mind that the client will now no longer receive that data until it is sent explicitly with the flush
method.
The dispatch
method will now also only write a newline to the buffer, instead of writing a newline and flushing the data to the client.
// Before
session.event().data().id().retry().comment().dispatch();
// After
session.event().data().id().retry().comment().dispatch().flush(); // All data is sent once over the network
// Or to match the exact previous behaviour, flush after each field write
session.event().flush().data().flush().id().flush().retry().flush().comment().flush().dispatch().flush();
⚠ BREAKING: Event ID format guarantee removed
Event IDs are no longer guaranteed to be 8-character alphanumeric strings, but are still guaranteed to be cryptographically pseudo random.
This is due to a performance bottleneck being caused by Node's random byte generation, which has been replaced with a faster mechanism.
⚠ BREAKING: Session
id
method no longer accepts null
The Session#id
method no longer accepts null
as an argument. Simply pass no arguments instead to wipe the ID.
⚠ BREAKING: Channel registration methods short-circuit on already-registered sessions
The Channel#register
and Channel#deregister
now short-circuit and thus do not emit events when the session is already registered or not registered, respectively.
Performance improvements
This release brings significant performance improvements when using both individual sessions or multiple sessions with channels.
v0.7.1:
Benchmarking "Push events with channels".
better-sse x 11,421 ops/sec ±6.10% (75 runs sampled)
v0.8.0
Benchmarking "Push events with channels".
better-sse x 44,702 ops/sec ±6.08% (81 runs sampled)
That's almost a 4x performance improvement!
Feel free to try the benchmarks yourself in the examples
directory.
Session
default headers can now be overridden
Headers you pass in the header
option will now overwrite the values of the default headers they are the same as, instead of being ignored.
// Before
const session = await createSession({ headers: { Connection: "something-else" } });
// "Connection: keep-alive" is sent
// After
const session = await createSession({ headers: { Connection: "something-else" } });
// "Connection: something-else" is sent
HTTP/2 Support
HTTP/2 support is now available via the Node HTTP/2 compatibility API.
Note that this is only adding support for the compatibility version of the Node HTTP/2 implementation, not the direct core API, as that would be a more challenging task most likely requiring breaking changes to the package API. Support for the core API is not currently planned.
const server = http2.createServer().listen();
server.on("request", (req, res) => {
const session = new Session(req, res);
});
Full Changelog
Added
- Added an internal data buffer to Session that buffers written data internally until it is flushed to the client using the new
Session#flush
method. - Added the
Pragma
,X-Accel-Buffering
headers and add additional values to theCache-Control
default header to further disable all forms of caching. - Added support for supplying the Node HTTP/2 compatibility API
Http2ServerRequest
andHttp2ServerResponse
objects to theSession
constructorreq
andres
parameters, respectively.
Changed
- Update the
Session#event
,Session#data
,Session#id
,Session#retry
andSession#comment
methods to write to the internal data buffer instead of sending the field data over the wire immediately. - Update the
Session#dispatch
method to only write a newline (and to the internal data buffer) and not flush the written data to the client. - Update the
Channel#broadcast
method to generate its own custom event ID and thus add it as an additional argument to itsbroadcast
event callback function. - Update the
Channel#register
andChannel#deregister
to not do anything if the channel is already registered or deregistered, respectively. - Update the
Session
constructor optionsheader
field to overwrite conflicting default headers instead of being ignored. - Update auto-generated event IDs to be guaranteed to be a cryptographically unique string instead of a pseudorandomly generated string of eight characters.
Fixed
- Fixed the Channel
session-disconnected
being fired after instead of before the session is deregistered.
Removed
- Removed the ability to pass
null
toSession#id
. Give no arguments at all instead.