EventSource Class

A connection to a server that sends events (https://html.spec.whatwg.org/multipage/server-sent-events.html#the-eventsource-interface): the response of a CORS request for a text/event-stream resource is interpreted as its bytes arrive (see DocumentResource.RemainingContentBytes), and each event is dispatched as a MessageEvent. When the response ends, the connection is reestablished after the reconnection time, with the Last-Event-ID header.

public sealed class EventSource : EventTarget

Remarks

The relevant global object of the event source is the current global object when it is created (the window or the worker of the script that creates it): its events are fired by tasks of its event loop, and the reconnection time is measured by the timers of that event loop (DomEventLoop.QueueTimer). An event source created without current global object cannot connect: its EventSource.ReadyState is EventSource.Closed.

An open connection that waits for events is not an asynchronous operation of its event loop: DomEventLoop.RunUntilIdleAsync does not wait for the next event. The event sources of a document are closed when the document is unloaded or destroyed, and the ones of a worker when it ends.

See also

Inheritance
EventTarget EventSource

Constructors

NameDescription
EventSource(string, EventSourceInit?)Creates an event source and starts to establish its connection (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource).

EventSource(string, EventSourceInit?)

EventSource.cs

Creates an event source and starts to establish its connection (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource).

public EventSource(string url, EventSourceInit? eventSourceInitDict = null)

Parameters

url string
The URL of the event stream, possibly relative to the API base URL of the relevant global object.
eventSourceInitDict EventSourceInit?
The options, or null.

Exceptions

DomException
A SyntaxError when the URL is not valid.

See also

Properties

NameDescription
OnErrorGets or sets the event handler of the error event.
OnMessageGets or sets the event handler of the message event.
OnOpenGets or sets the event handler of the open event.
ReadyStateGets the state of the connection (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-readystate): EventSource.Connecting, EventSource.Open or EventSource.Closed.
UrlGets the URL of the event stream, serialized (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-url).
WithCredentialsGets whether the credentials mode of the requests is "include": the cookies are also sent to another origin.

Gets or sets the event handler of the error event.

public Action<Event>? OnError { get; set; }

Gets or sets the event handler of the message event.

public Action<Event>? OnMessage { get; set; }

Gets or sets the event handler of the open event.

public Action<Event>? OnOpen { get; set; }

ReadyState

EventSource.cs

Gets the state of the connection (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-readystate): EventSource.Connecting, EventSource.Open or EventSource.Closed.

public ushort ReadyState { get; }

Gets the URL of the event stream, serialized (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-url).

public string Url { get; }

WithCredentials

EventSource.cs

Gets whether the credentials mode of the requests is "include": the cookies are also sent to another origin.

public bool WithCredentials { get; }

Methods

NameDescription
Close()Closes the connection (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-close): the fetch is aborted, the events that were received and not yet dispatched are dropped, and the connection is not reestablished.

Closes the connection (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-close): the fetch is aborted, the events that were received and not yet dispatched are dropped, and the connection is not reestablished.

public void Close()

Constants

NameDescription
ClosedThe state of an event source whose connection is not open and is not reestablished: it failed, or EventSource.Close was called.
ConnectingThe state of an event source whose connection has not yet been established, or was closed and is being reestablished.
OpenThe state of an event source whose connection is open: the events are dispatched as they are received.

Closed

The state of an event source whose connection is not open and is not reestablished: it failed, or EventSource.Close was called.

public const ushort Closed = 2

Connecting

The state of an event source whose connection has not yet been established, or was closed and is being reestablished.

public const ushort Connecting = 0

Open

The state of an event source whose connection is open: the events are dispatched as they are received.

public const ushort Open = 1