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

  • Revision slug: Web/API/EventSource
  • Revision title: EventSource
  • Revision id: 834315
  • Created:
  • Creator: Jeremie
  • 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 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); {{ 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() }}

 

Revision Source

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

<p>The <code>EventSource</code> 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 event source.</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">Method overview</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <td><code>void <a href="#close()">close</a>();</code></td>
  </tr>
  <tr>
   <td><code>void <a href="#init()">init</a>(in nsIPrincipal principal, in nsIScriptContext scriptContext, in nsPIDOMWindow ownerWindow, in DOMString url);</code> {{ noscript_inline() }}</td>
  </tr>
 </tbody>
</table>

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

<table class="standard-table">
 <tbody>
  <tr>
   <td class="header">Attribute</td>
   <td class="header">Type</td>
   <td class="header">Description</td>
  </tr>
  <tr>
   <td><code>onerror</code></td>
   <td><code>{{ Interface("nsIDOMEventListener") }}</code></td>
   <td>A JavaScript function to call when an error occurs.</td>
  </tr>
  <tr>
   <td><code>onmessage</code></td>
   <td><code>{{ Interface("nsIDOMEventListener") }}</code></td>
   <td>A JavaScript function to call when an a message without an <code>event</code> field arrives.</td>
  </tr>
  <tr>
   <td><code>onopen</code></td>
   <td><code>{{ Interface("nsIDOMEventListener") }}</code></td>
   <td>A JavaScript function to call when the connection has opened.</td>
  </tr>
  <tr>
   <td><code>readyState</code></td>
   <td><code><a href="/en/long" title="en/long">long</a></code></td>
   <td>The state of the connection, must be one of <code>CONNECTING</code>, <code>OPEN</code>, or <code>CLOSED</code>. <strong>Read only.</strong></td>
  </tr>
  <tr>
   <td><code>url</code></td>
   <td>{{ domxref("DOMString") }}</td>
   <td><strong>Read only.</strong></td>
  </tr>
 </tbody>
</table>

<p>In addition to these exposed attributes, there are two internal attributes that are not exposed directly:</p>

<dl>
 <dt>reconnection time</dt>
 <dd>This is a time, in milliseconds, used to determine how long to wait after a failed attempt to connect before trying again.</dd>
 <dt>last event ID&nbsp;string</dt>
 <dd>This is initially the empty string; the server can send a message with the field name <code>id</code> to set this value.</dd>
</dl>

<h2 id="Constants" name="Constants">Constants</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <td class="header">Constant</td>
   <td class="header">Value</td>
   <td class="header">Description</td>
  </tr>
  <tr>
   <td><code>CONNECTING</code></td>
   <td><code>0</code></td>
   <td>The connection is being established.</td>
  </tr>
  <tr>
   <td><code>OPEN</code></td>
   <td><code>1</code></td>
   <td>The connection is open and dispatching events.</td>
  </tr>
  <tr>
   <td><code>CLOSED</code></td>
   <td><code>2</code></td>
   <td>The connection is not being established, has been closed or there was a fatal error.</td>
  </tr>
 </tbody>
</table>

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

<h3 id="close()" name="close()">close()</h3>

<p>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.</p>

<pre class="eval">
void close();
</pre>

<h6 id="Parameters" name="Parameters">Parameters</h6>

<p>None.</p>

<p>{{ method_noscript("init") }}</p>

<p>Initializes the object for use from C++ code with the principal, script context, and owner window that should be used.</p>

<pre class="eval">
void init(
  in nsIPrincipal principal,
  in nsIScriptContext scriptContext,
  in nsPIDOMWindow ownerWindow,
  in DOMString url
);
</pre>

<h6 id="Parameters" name="Parameters">Parameters</h6>

<dl>
 <dt><code>principal</code></dt>
 <dd>The principal to use for the request. This must not be <code>null</code>.</dd>
 <dt><code>scriptContext</code></dt>
 <dd>The script context to use for the request. May be <code>null</code>.</dd>
 <dt><code>ownerWindow</code></dt>
 <dd>The associated window for the request. May be <code>null</code>.</dd>
 <dt><code>url</code></dt>
 <dd>The <code>EventSource</code>'s URL. This must not be empty.</dd>
</dl>

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

<ul>
 <li>{{ spec("https://html.spec.whatwg.org/multipage/comms.html#the-eventsource-interface","Server-Sent Events: The EventSource Interface","CR") }}</li>
 <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>

<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>EventSource 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>EventSource support</td>
   <td>{{ CompatAndroid("4.4") }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{CompatUnknown()}}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>

<p>&nbsp;</p>
Revert to this revision