Revision 299739 of EventSource

  • Revision slug: Server-sent_events/EventSource
  • Revision title: EventSource
  • Revision id: 299739
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

Revision Content

The EventSource interface is used to manage server-sent events. 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() }}

Attributes

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("http://www.w3.org/TR/eventsource/#the-eventsource-interface","Server-Sent Events: The EventSource Interface","WD") }}
  • Using server-sent events

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
EventSource support 9 {{ CompatGeckoDesktop("6.0") }} {{ CompatUnknown() }} 11 5
Cross-Origin Resource Sharing
support
{{ CompatUnknown() }} {{ CompatGeckoDesktop("11.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
EventSource support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

 

Revision Source

<p>The <code>EventSource</code> interface is used to manage server-sent events. You can set the onmessage attribute to a JavaScript function to receive non-typed messages (that is, messages with no <code>event</code> field). You can also call <code>addEventListener()</code> to listen for events just like any other event source.</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">Attributes</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("http://www.w3.org/TR/eventsource/#the-eventsource-interface","Server-Sent Events: The EventSource Interface","WD") }}</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><a href="/En/HTTP_access_control" title="HTTP access control">Cross-Origin Resource Sharing</a><br />
          support</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoDesktop("11.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</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>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>&nbsp;</p>
Revert to this revision