Refer to the documentation for EventSource for information on how to use the EventSource
API in your web application.
const sse = new EventSource("/sse");
sse.onmessage = (e) => {
console.log(e.data);
};
sse.addEventListener("custom-event", (e) => {
console.log(e.data);
});
The server supports cross-origin requests via the Access-Control-Allow-Origin
header. By default, it is set to *
to allow all origins.
Cross-Origin requests are configurable with the --cors-*
family of options.
NOTE: Access-Control-Allow-Credentials
cannot be set to true
if either of Access-Control-Allow-Origin
or Access-Control-Allow-Headers
is set to *
(any).
Last-Event-ID
The native browser EventSource
API does not support manually setting Last-Event-ID
when establishing a new connection. It will set it automatically when reconnecting after a connection loss. However, it is often useful to set it manually to resume from a specific message ID or to "catch-up" any messages missed since the last connection. For this use-case, the server supports setting Last-Event-ID
via the query parameter ?last_event_id=
.
const sse = new EventSource("/sse?last_event_id=some-id");
When both the Last-Event-ID
header and ?last_event_id=
query parameter are present, the header takes precedence.
The server itself does not store any message history. Message history and "catch-up" functionality must be implemented in the Lua script via the catchup(sub, last_event_id)
function.