Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1071198 of EventSource

  • Revision slug: Web/API/EventSource
  • Revision title: EventSource
  • Revision id: 1071198
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment + constructor

Revision Content

{{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 are CONNECTING (0), OPEN (1), or CLOSED (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 to CLOSED. 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}}

See also

Revision Source

<p>{{APIRef("Websockets API")}}</p>

<p>The <strong><code>EventSource</code></strong> interface is used to receive server-sent events. It connects to a server over HTTP and receives events in <code>text/event-stream</code> format without closing the connection.</p>

<dl>
</dl>

<h2>Constructor</h2>

<dl>
 <dt>{{domxref("EventSource.EventSource", "EventSource()"}}</dt>
 <dd>Creates a new <code>EventSource</code> from the given {{domxref("USVString")}}.</dd>
</dl>

<h2 id="Properties">Properties</h2>

<p><em>This interface also inherits properties from its parent, {{domxref("EventTarget")}}.</em></p>

<dl>
 <dt>{{domxref("EventSource.onerror")}}</dt>
 <dd>Is an {{domxref("EventHandler")}} being called when an error occurs and the {{event("error")}} event is dispatched on this object.</dd>
 <dt>{{domxref("EventSource.onmessage")}}</dt>
 <dd>Is an {{domxref("EventHandler")}} being called when a {{event("message")}} event is received, that is when a message is coming from the source.</dd>
 <dt>{{domxref("EventSource.onopen")}}</dt>
 <dd>Is an {{domxref("EventHandler")}} being called when an {{event("open")}} event is received, that is when the connection was just opened.</dd>
 <dt>{{domxref("EventSource.readyState")}} {{readonlyinline}}</dt>
 <dd>An <code>unsigned short</code> representing the state of the connection. Possible values are <code>CONNECTING</code> (<code>0</code>), <code>OPEN</code> (<code>1</code>), or <code>CLOSED</code> (<code>2</code>).</dd>
 <dt>{{domxref("EventSource.url")}} {{readonlyinline}}</dt>
 <dd>A {{domxref("DOMString")}} representing the URL of the source.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<p><em>This interface also inherits methods from its parent, {{domxref("EventTarget")}}.</em></p>

<dl>
 <dt>{{domxref("EventSource.close()")}}</dt>
 <dd>Closes the connection, if any, and sets the <code>readyState</code> attribute to <code>CLOSED</code>. If the connection is already closed, the method does nothing.</dd>
</dl>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', "comms.html#the-eventsource-interface", "EventSource")}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<ul>
</ul>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>&nbsp;Basic support</td>
   <td>9</td>
   <td>{{ CompatGeckoDesktop("6.0") }}</td>
   <td>{{CompatUnknown}}</td>
   <td>11</td>
   <td>5</td>
  </tr>
  <tr>
   <td>CORS&nbsp;support</td>
   <td>26</td>
   <td>{{ CompatGeckoDesktop("11.0") }}</td>
   <td>{{CompatUnknown}}</td>
   <td>12</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatAndroid("4.4") }}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en/Server-sent_events/Using_server-sent_events" title="en/Server-sent events/Using server-sent events">Using server-sent events</a></li>
</ul>
Revert to this revision