SuSiE - Server-Sent Events with hapi
Above example under
/examples. Start with
This is a plugin that adds simple Server-Sent Events (aka EventSource) capabilities to hapi. It decorates the
toolkit with a new method
h.event(). You can send individual events as objects, or you can simply pass a stream and some options and SuSiE will make things work as you expect.
You probably already know this but install it with:
npm install --save susie
First load and register the plugin:
With event objects
In a route handler you can now call
h.event() to start an SSE response:
The first time you call
h.event(), appropriate HTTP response headers will be sent, along with your first event. Subsequent calls to
h.event() will send more events.
If any of your datum are objects, they will be stringified for you. Make sure to parse them again in the client, if you're expecting JSON.
With a readable stream
A really nice way to provide an EventSource is using a ReadableStream. This is really simple with SuSiE. Just call
Each chunk coming off the stream will be sent as an event. The content of the chunk will be the data parameter. You can provide an optional
event option and
id generator. By default the id will be the number of chunks received:
Object mode streams
If the stream is in
objectMode, each object that comes off the stream will be stringified and the resulting string will be used as the
data parameter. See example under
examples for example.
How do I finish a SSE stream for good?
In the SSE spec, it says that when the HTTP response ends, the browser will try to reconnect, sending another request to the endpoint. You may want this. Or you may really want to stop to the events being streamed altogether.
When you call
h.event(null) or your stream emits its
end event, the HTTP response will conclude. However, SuSiE will send one last event to the browser before it closes. You should listen for this
end event in your client code and close the EventSource, before the browser attempts to reconnect: