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 927787 of EventSource

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

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.

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.

Methods

{{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.

Properties

{{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.

 

Specifications

 

{{ spec("https://html.spec.whatwg.org/multipage/comms.html#the-eventsource-interface","Server-Sent Events: The EventSource Interface","CR") }}

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>

<p><span style="line-height:1.5">You can set the <code>onmessage</code> attribute to a JavaScript function to receive non-typed messages (that is, messages with no </span><code style="font-size: 14px;">event</code><span style="line-height:1.5"> field). You can also call </span><code style="font-size: 14px;">addEventListener()</code><span style="line-height:1.5"> to listen for events just like any other <code><a href="/en-US/docs/Web/API/EventTarget">EventTarget</a></code>.</span></p>

<p>See <a href="/en/Server-sent_events/Using_server-sent_events" title="en/Server-sent events/Using server-sent events">Using server-sent events</a> for further details.</p>

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

<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="Attributes" name="Attributes">Properties</h2>

<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>

<p>&nbsp;</p>

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

<p>&nbsp;</p>

<p>{{ spec("https://html.spec.whatwg.org/multipage/comms.html#the-eventsource-interface","Server-Sent Events: The EventSource Interface","CR") }}</p>

<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" name="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