Name

EventSource — a Comet connection to an HTTP server

Inherits from

EventTarget

Synopsis

An EventSource represents a long-lived HTTP connection through which a Web server can “push” textual messages. To use these “Server Sent Events”, pass the server URL to the EventSource() constructor and then register a message event handler on the resulting Event Source object.

Server Sent Events are new, and at the time of this writing, are not supported in all browsers.

Constructor

new EventSource(string url)

Creates a new EventSource object connected to the web server at the specified url. url is interpreted relative to the URL of the containing document.

Constants

These constants define the possible values of the readyState property.

unsigned short CONNECTING = 0

The connection is being set up, or the connection closed and the EventSource is re-connecting.

unsigned short OPEN = 1

The connection is open and events are being dispatched.

unsigned short CLOSED = 2

The connection was closed, either because close() was called or a fatal error occurred and it is not possible to reconnect.

Properties

readonly unsigned short readyState

The state of the connection. The constants above define the possible values.

readonly string url

The absolute URL to which the EventSource is connected.

Methods

void close()

This method closes the connection. Once this method is called, the EventSource object can no longer be used. If you need to connect again, create a new EventSource.

Event Handlers

Network communication is asynchronous, ...

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.