Server-sent events

{{Short description|Server push technology}}

Server-Sent Events (SSE) is a server push technology enabling a client to receive automatic updates from a server via an HTTP connection, and describes how servers can initiate data transmission towards clients once an initial client connection has been established. They are commonly used to send message updates or continuous data streams to a browser client and designed to enhance native, cross-browser streaming through a JavaScript API called EventSource, through which a client requests a particular URL in order to receive an event stream. The EventSource API is standardized as part of HTML Living Standard{{Cite web |date=31 March 2022 |title=HTML Living Standard: 9.2 Server-sent events |url=https://html.spec.whatwg.org/multipage/server-sent-events.html |publisher=WHATWG}} by the WHATWG. The media type for SSE is text/event-stream.

All modern browsers support server-sent events: Firefox 6+, Google Chrome 6+, Opera 11.5+, Safari 5+, Microsoft Edge 79+.[https://caniuse.com/eventsource When can I use... Server-sent DOM events]

History

The SSE mechanism was first specified by Ian Hickson as part of the "WHATWG Web Applications 1.0" proposal starting in 2004.{{cite web |editor-last=Hickson |editor-first=Ian |date=2006-01-01 |df=dmy |title=Server-sent DOM events |department=Web Applications 1.0 |url=https://whatwg.org/specs/web-apps/2006-01-01/#scs-server-sent |website=WHATWG |access-date=2024-05-09}} In September 2006, the Opera web browser implemented the experimental technology in a feature called "Server-Sent Events".{{cite web |last=Bersvendsen |first=Arve |date=1 September 2006 |title=Event Streaming to Web Browsers |url=http://dev.opera.com/blog/event-streaming-to-web-browsers/ |website=dev.opera.com}}[http://www.html5rocks.com/en/tutorials/eventsource/basics/ Stream Updates with Server-Sent Events, Eric Bidelman, HTML5Rocks website.]

Example

var source = new EventSource('updates.cgi');

source.onmessage = function (event) {

alert(event.data);

};

See also

References