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 event source.
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); Native code only! |
Properties
| Attribute | Type | Description |
onerror |
|
A JavaScript function to call when an error occurs. |
onmessage |
|
A JavaScript function to call when an a message without an event field arrives. |
onopen |
|
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 |
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
idto 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.
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
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| EventSource support | 9 | 6.0 (6.0) | ? | 11 | 5 |
| CORS support | 26 | 11.0 (11.0) | ? | 12 | ? |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| EventSource support | 4.4 | ? | ? | ? | ? |