{{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.
Constructor
- {{domxref("EventSource.EventSource", "EventSource()")}}
- Creates a new
EventSource
from the given {{domxref("USVString")}}.
Properties
This interface also inherits properties from its parent, {{domxref("EventTarget")}}.
- {{domxref("EventSource.onerror")}}
- Is an {{domxref("EventHandler")}} being called when an error occurs and the {{event("error")}} event is dispatched on this object.
- {{domxref("EventSource.onmessage")}}
- Is an {{domxref("EventHandler")}} being called when a {{event("message")}} event is received, that is when a message is coming from the source.
- {{domxref("EventSource.onopen")}}
- Is an {{domxref("EventHandler")}} being called when an {{event("open")}} event is received, that is when the connection was just opened.
- {{domxref("EventSource.readyState")}} {{readonlyinline}}
- An
unsigned short
representing the state of the connection. Possible values areCONNECTING
(0
),OPEN
(1
), orCLOSED
(2
). - {{domxref("EventSource.url")}} {{readonlyinline}}
- A {{domxref("DOMString")}} representing the URL of the source.
Methods
This interface also inherits methods from its parent, {{domxref("EventTarget")}}.
- {{domxref("EventSource.close()")}}
- Closes the connection, if any, and sets the
readyState
attribute toCLOSED
. If the connection is already closed, the method does nothing.
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', "comms.html#the-eventsource-interface", "EventSource")}} | {{Spec2('HTML WHATWG')}} |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic 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 |
---|---|---|---|---|---|
Basic support | {{ CompatAndroid("4.4") }} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |