Skip to content

EventSource

The EventSource class provides an implementation of the Server-Sent Events (SSE) standard. SSE is a simple and efficient protocol to receive real-time updates over HTTP. Instead of continually polling the server for changes, clients can open a single connection and receive updates as they happen, making it a powerful tool for building real-time applications. Why Use EventSource:

  • Reduces the amount of redundant data sent over the network, since there's no need to repeatedly poll the server.
  • Simplifies the client-side logic, as the protocol handles reconnects and manages the connection state.
  • Offers a standardized way (via the W3C standard) to achieve real-time communication in web applications.

This class encapsulates the complexity of the SSE protocol, offering an easy-to-use API for both sending and receiving real-time updates.

Remarks:

How It Works:

  • A client makes a request to an SSE-supporting server, specifying that it expects the server to keep the connection open.
  • The server holds the connection and sends data whenever a new event occurs. Each event is sent as a block of text terminated by a pair of newline characters.
  • Clients process these events as they arrive.

Fields:

Uri Uri

Gets the URI of the remote endpoint.

States State

Gets the current state of the EventSource object.

TimeSpan ReconnectionTime

Gets or sets the duration to wait before attempting a reconnection. Defaults to 2 seconds.
The server can override this setting.

String LastEventId

Gets the ID of the last successfully received event.

Boolean IsClosed

Gets a value indicating whether the EventSource is in the Closed state.

LoggingContext LoggingContext

Gets the logging context for the current EventSource instance.

HTTPRequest InternalRequest

Gets the internal HTTPRequest object used by the EventSource for communication.

OnGeneralEventDelegate OnOpen

Called when successfully connected to the server.

OnMessageDelegate OnMessage

Called on every message received from the server.

OnErrorDelegate OnError

Called when an error occurs.

OnRetryDelegate OnRetry

Called when the EventSource will try to do a retry attempt. If this function returns with false, it will cancel the attempt.

OnCommentDelegate OnComment

This event is called for comments received from the server.

OnGeneralEventDelegate OnClosed

Called when the EventSource object closed.

OnStateChangedDelegate OnStateChanged

Called every time when the State property changed.

Methods:

Void Open()

Start to connect to the remote server.

Void Close()

Start to close the connection.

Void On(String, OnEventDelegate)

With this function an event handler can be subscribed for an event name.

Void Off(String)

With this function the event handler can be removed for the given event name.
The event is still will be sent by the server and processed by the client.

Comments