The EventSource interface is web content's interface to server-sent events. An EventSource instance opens a persistent connection to an HTTP server, which sends events in text/event-stream format. The connection remains open until closed by calling EventSource.close().
Once the connection is opened, incoming messages from the server are delivered to your code in the form of message events.
Unlike WebSockets, server-sent events are unidirectional; that is, data messages are delivered in one direction, from the server to the client (such as a user's web browser). That makes them an excellent choice when there's no need to send data from the client to the server in message form. For example, EventSource is a useful approach for handling things like social media status updates, news feeds, or delivering data into a client-side storage mechanism like IndexedDB or web storage.
Constructor
EventSource()- Creates a new
EventSourceto handle receiving server-sent events from a specified URL, optionally in credentials mode.
Properties
This interface also inherits properties from its parent, EventTarget.
EventSource.readyStateRead only- A number representing the state of the connection. Possible values are
CONNECTING(0),OPEN(1), orCLOSED(2). EventSource.urlRead only- A
DOMStringrepresenting the URL of the source. EventSource.withCredentialsRead only- A
Booleanindicating whether theEventSourceobject was instantiated with cross-origin (CORS) credentials set (true), or not (false, the default).
Event handlers
EventSource.onerror- Is an
EventHandlercalled when an error occurs and theerrorevent is dispatched on anEventSourceobject. EventSource.onmessage- Is an
EventHandlercalled when amessageevent is received, that is when a message is coming from the source. EventSource.onopen- Is an
EventHandlercalled when anopenevent is received, that is when the connection was just opened.
Methods
This interface also inherits methods from its parent, EventTarget.
EventSource.close()- Closes the connection, if any, and sets the
readyStateattribute toCLOSED. If the connection is already closed, the method does nothing.
Examples
In this basic example, an EventSource is created to receive events from the server; a page with the name "sse.php" is responsible for generating the events.
var evtSource = new EventSource('sse.php');
var eventList = document.querySelector('ul');
evtSource.onmessage = function(e) {
var newElement = document.createElement("li");
newElement.textContent = "message: " + e.data;
eventList.appendChild(newElement);
}
Each received event causes our EventSource object's onmessage event handler to be run. It, in turn, creates a new <li> element and writes the message's data into it, then appends the new element to the list element already in the document.
Note: You can find a full example on GitHub — see Simple SSE demo using PHP.
Specifications
| Specification | Status | Comment |
|---|---|---|
| HTML Living Standard The definition of 'EventSource' in that specification. |
Living Standard |
Browser compatibility
| Desktop | Mobile | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Basic support | Chrome Full support 6 | Edge No support No | Firefox Full support 6 | IE No support No | Opera Full support Yes | Safari Full support 5 | WebView Android Full support Yes | Chrome Android Full support 18 | Edge Mobile No support No | Firefox Android Full support 45 | Opera Android Full support 12 | Safari iOS Full support 5 | Samsung Internet Android ? |
| Available in workers | Chrome Full support Yes | Edge No support No | Firefox Full support 53 | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile No support No | Firefox Android Full support 53 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android ? |
EventSource() constructor | Chrome Full support 9 | Edge No support No | Firefox Full support 6 | IE No support No | Opera Full support 11 | Safari Full support 5 | WebView Android Full support Yes | Chrome Android Full support 18 | Edge Mobile No support No | Firefox Android Full support 6 | Opera Android Full support 11 | Safari iOS Full support 5 | Samsung Internet Android ? |
close | Chrome Full support 6 | Edge No support No | Firefox Full support 6 | IE No support No | Opera Full support Yes | Safari Full support 5 | WebView Android Full support Yes | Chrome Android Full support 18 | Edge Mobile No support No | Firefox Android Full support 45 | Opera Android Full support 12 | Safari iOS Full support 5 | Samsung Internet Android ? |
onerror | Chrome Full support 6 | Edge No support No | Firefox Full support 6 | IE No support No | Opera Full support Yes | Safari Full support 5 | WebView Android Full support Yes | Chrome Android Full support 18 | Edge Mobile No support No | Firefox Android Full support 45 | Opera Android Full support 12 | Safari iOS Full support 5 | Samsung Internet Android ? |
onmessage | Chrome Full support 6 | Edge No support No | Firefox Full support 6 | IE No support No | Opera Full support Yes | Safari Full support 5 | WebView Android Full support Yes | Chrome Android Full support 18 | Edge Mobile No support No | Firefox Android Full support 45 | Opera Android Full support 12 | Safari iOS Full support 5 | Samsung Internet Android ? |
onopen | Chrome Full support 6 | Edge No support No | Firefox Full support 6 | IE No support No | Opera Full support Yes | Safari Full support 5 | WebView Android Full support Yes | Chrome Android Full support 18 | Edge Mobile No support No | Firefox Android Full support 45 | Opera Android Full support 12 | Safari iOS Full support 5 | Samsung Internet Android ? |
readyState | Chrome Full support 6 | Edge No support No | Firefox Full support 6 | IE No support No | Opera Full support Yes | Safari Full support 5 | WebView Android Full support Yes | Chrome Android Full support 18 | Edge Mobile No support No | Firefox Android Full support 45 | Opera Android Full support 12 | Safari iOS Full support 5 | Samsung Internet Android ? |
url | Chrome Full support 6 | Edge No support No | Firefox Full support 6 | IE No support No | Opera Full support Yes | Safari Full support 5 | WebView Android Full support Yes | Chrome Android Full support 18 | Edge Mobile No support No | Firefox Android Full support 45 | Opera Android Full support 12 | Safari iOS Full support 5 | Samsung Internet Android ? |
withCredentials | Chrome Full support 6 | Edge No support No | Firefox Full support 6 | IE No support No | Opera Full support Yes | Safari Full support 5 | WebView Android Full support Yes | Chrome Android Full support 18 | Edge Mobile No support No | Firefox Android Full support 45 | Opera Android Full support 12 | Safari iOS Full support 5 | Samsung Internet Android ? |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown