{{APIRef("Websockets API")}}
The EventSource
interface is used to receive server-sent events. It connects to a server over HTTP and receives events in text/event-stream
format without closing the connection.
You can set the onmessage
attribute to a JavaScript function to receive non-typed messages (that is, messages with no event
field). You can also call addEventListener()
to listen for events just like any other EventTarget
.
See Using server-sent events for further details.
Method overview
void close(); |
void init(in nsIPrincipal principal, in nsIScriptContext scriptContext, in nsPIDOMWindow ownerWindow, in DOMString url); {{ noscript_inline() }} |
Properties
Attribute | Type | Description |
onerror |
{{ Interface("nsIDOMEventListener") }} |
A JavaScript function to call when an error occurs. |
onmessage |
{{ Interface("nsIDOMEventListener") }} |
A JavaScript function to call when an a message without an event field arrives. |
onopen |
{{ Interface("nsIDOMEventListener") }} |
A JavaScript function to call when the connection has opened. |
readyState |
long |
The state of the connection, must be one of CONNECTING , OPEN , or CLOSED . Read only. |
url |
{{ domxref("DOMString") }} | Read only. |
In addition to these exposed attributes, there are two internal attributes that are not exposed directly:
- reconnection time
- This is a time, in milliseconds, used to determine how long to wait after a failed attempt to connect before trying again.
- last event ID string
- This is initially the empty string; the server can send a message with the field name
id
to set this value.
Constants
Constant | Value | Description |
CONNECTING |
0 |
The connection is being established. |
OPEN |
1 |
The connection is open and dispatching events. |
CLOSED |
2 |
The connection is not being established, has been closed or there was a fatal error. |
Methods
close()
Closes the connection, if any, and sets the readyState
attribute to CLOSED
. If the connection is already closed, the method does nothing.
void close();
Parameters
None.
{{ method_noscript("init") }}
Initializes the object for use from C++ code with the principal, script context, and owner window that should be used.
void init( in nsIPrincipal principal, in nsIScriptContext scriptContext, in nsPIDOMWindow ownerWindow, in DOMString url );
Parameters
principal
- The principal to use for the request. This must not be
null
. scriptContext
- The script context to use for the request. May be
null
. ownerWindow
- The associated window for the request. May be
null
. url
- The
EventSource
's URL. This must not be empty.
See also
- {{ spec("https://html.spec.whatwg.org/multipage/comms.html#the-eventsource-interface","Server-Sent Events: The EventSource Interface","CR") }}
- Using server-sent events
Browser compatibility
{{ CompatibilityTable() }}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
EventSource support | 9 | {{ CompatGeckoDesktop("6.0") }} | {{ CompatUnknown() }} | 11 | 5 |
CORS support | 26 | {{ CompatGeckoDesktop("11.0") }} | {{ CompatUnknown() }} | 12 | {{ CompatUnknown() }} |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
EventSource support | {{ CompatAndroid("4.4") }} | {{ CompatUnknown() }} | {{CompatUnknown()}} | {{ CompatUnknown() }} | {{ CompatUnknown() }} |