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.


Creates a new EventSource to handle receiving server-sent events from a specified URL, optionally in credentials mode.


This interface also inherits properties from its parent, EventTarget.

EventSource.readyState Read only
A number representing the state of the connection. Possible values are CONNECTING (0), OPEN (1), or CLOSED (2).
EventSource.url Read only
A DOMString representing the URL of the source.
EventSource.withCredentials Read only
A Boolean indicating whether the EventSource object was instantiated with cross-origin (CORS) credentials set (true), or not (false, the default).

Event handlers

Is an EventHandler called when an error occurs and the error event is dispatched on an EventSource object.
Is an EventHandler called when a message event is received, that is when a message is coming from the source.
Is an EventHandler called when an open event is received, that is when the connection was just opened.


This interface also inherits methods from its parent, EventTarget.

Closes the connection, if any, and sets the readyState attribute to CLOSED. If the connection is already closed, the method does nothing.


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;

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.


Browser compatibilityUpdate compatibility data on GitHub

Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 6 No 6 No Yes 5
Available in workers Yes No 53 No Yes Yes
EventSource() constructor 9 No 6 No 11 5
close 6 No 6 No Yes 5
onerror 6 No 6 No Yes 5
onmessage 6 No 6 No Yes 5
onopen 6 No 6 No Yes 5
readyState 6 No 6 No Yes 5
url 6 No 6 No Yes 5
withCredentials 6 No 6 No Yes 5
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support Yes 18 No 45 12 5 ?
Available in workers Yes Yes No 53 Yes Yes ?
EventSource() constructor Yes 18 No 6 11 5 ?
close Yes 18 No 45 12 5 ?
onerror Yes 18 No 45 12 5 ?
onmessage Yes 18 No 45 12 5 ?
onopen Yes 18 No 45 12 5 ?
readyState Yes 18 No 45 12 5 ?
url Yes 18 No 45 12 5 ?
withCredentials Yes 18 No 45 12 5 ?

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.