Table of Contents

Class EventSource

Namespace
CSharpToJavaScript.APIs.JS
Assembly
CSharpToJavaScript.dll

The EventSource interface is web content's interface to server-sent events.

[Value("EventSource")]
public class EventSource : EventTarget
Inheritance
EventSource
Inherited Members

Remarks

An EventSource instance opens a persistent connection to an HTTP server, which sends events in text/event-stream format. The connection remains open until closed by calling Close().

Once the connection is opened, incoming messages from the server are delivered to your code in the form of events. If there is an event field in the incoming message, the triggered event is the same as the event field value. If no event field is present, then a generic EventSourcemessage event is fired.

Unlike WebSockets, server-sent events are unidirectional; that is, data messages are delivered in one direction, from the server to the client (such as a user's web browser). That makes them an excellent choice when there's no need to send data from the client to the server in message form. For example, EventSource is a useful approach for handling things like social media status updates, news feeds, or delivering data into a client-side storage mechanism like IndexedDB or web storage.

WARNING

When not used over HTTP/2, SSE suffers from a limitation to the maximum number of open connections, which can be specially painful when opening various tabs as the limit is per browser and set to a very low number (6). The issue has been marked as "Won't fix" in Chrome and Firefox. This limit is per browser + domain, so that means that you can open 6 SSE connections across all of the tabs to www.example1.com and another 6 SSE connections to www.example2.com. (from Stack Overflow). When using HTTP/2, the maximum number of simultaneous HTTP streams is negotiated between the server and the client (defaults to 100).

-Server-sent events
-Using server-sent events

See also on MDN

Constructors

EventSource()

public EventSource()

EventSource(string, EventSourceInit)

The EventSource()
constructor returns a newly-created EventSource, which represents a
remote resource.

public EventSource(string url, EventSourceInit eventSourceInitDict = null)

Parameters

url string
eventSourceInitDict EventSourceInit

Remarks

Fields

CLOSED

[Value("CLOSED")]
public const ushort CLOSED = 2

Field Value

ushort

CONNECTING

[Value("CONNECTING")]
public const ushort CONNECTING = 0

Field Value

ushort

OPEN

[Value("OPEN")]
public const ushort OPEN = 1

Field Value

ushort

Properties

Onerror

[Value("onerror")]
public EventHandlerNonNull Onerror { get; set; }

Property Value

EventHandlerNonNull

Onmessage

[Value("onmessage")]
public EventHandlerNonNull Onmessage { get; set; }

Property Value

EventHandlerNonNull

Onopen

[Value("onopen")]
public EventHandlerNonNull Onopen { get; set; }

Property Value

EventHandlerNonNull

ReadyState

The readyState read-only property of the
EventSource interface returns a number representing the state of the
connection.

[Value("readyState")]
public ushort ReadyState { get; }

Property Value

ushort

A number which is one of the three possible state constants defined on the EventSource interface:

Remarks

Url

The url read-only property of the
EventSource interface returns a string representing the
URL of the source.

[Value("url")]
public string Url { get; }

Property Value

string

A string representing the URL of the source.

Remarks

WithCredentials

The withCredentials read-only property of the
EventSource interface returns a boolean value indicating whether
the EventSource object was instantiated with CORS credentials set.

[Value("withCredentials")]
public bool WithCredentials { get; }

Property Value

bool

A boolean value indicating whether the EventSource object was
instantiated with CORS credentials set (true), or not (false,
the default).

Remarks

Methods

Close()

The close() method of the EventSource
interface closes the connection, if one is made, and sets the
ReadyState attribute to 2 (closed).

[Value("close")]
public GlobalObject.Undefined Close()

Returns

GlobalObject.Undefined

None (GlobalObject.Undefined).

Remarks

NOTE

If the connection is already closed, the method does nothing.

-EventSource

See also on MDN