Server-Sent Events (SSE) allows a server to continuously send events to a connected browser over a persistent HTTP connection. WebForms Core provides SSE Event methods that allow an HTML event to establish an SSE connection to a server endpoint and receive WebForms Core Action Controls from that connection.
In this model, the HTML event is defined by the server through the WebForms class, while WebFormsJS manages the SSE connection and executes the Action Controls received from the server.
The execution flow can be represented as:
Important: WebForms Core is not dependent on any specific programming language, Backend, or web development ecosystem. SSE itself must therefore be implemented using the SSE capabilities provided by the Backend or server environment used by your application. In the following example, the server-side SSE endpoints are implemented with the CodeBehind Framework. WebForms Core only defines the client-side SSE event behavior and the format of the WebForms Core commands returned through the SSE connection.
The SetSSEEvent method assigns an SSE connection to an HTML event. When the specified event occurs, WebFormsJS connects to the specified SSE endpoint.
The basic signature is:
| Parameter | Description |
|---|---|
InputPlace |
The WPC expression identifying the HTML element on which the event is assigned. |
HtmlEvent |
The HTML event that starts the SSE connection. |
Path |
The Backend endpoint that provides the SSE response. |
ShouldReconnect |
Determines whether WebFormsJS should attempt to reconnect when the SSE connection is interrupted. The default value is true. |
ReconnectTryTimeout |
The interval in milliseconds used for a reconnection attempt. The default value is 3000. |
For example, the following event starts an SSE connection when the button is clicked:
In this example, WebFormsJS attempts to reconnect after an interrupted connection using the configured reconnection interval.
An overload of SetSSEEvent accepts an OutputPlace. This allows the SSE response to be associated with a specific output location.
The signature is:
When an OutputPlace is specified, the received output can be directed to that location according to the WebForms Core response processing rules.
The reconnection options can also be specified:
SetSSEEventListener provides the EventListener form of SSE Event assignment.
The basic signature is:
The difference between SetSSEEvent and SetSSEEventListener is the event mechanism used to trigger the SSE connection. The first uses HtmlEvent, while the second uses HtmlEventListener.
The listener-based method also supports an OutputPlace.
The signature is:
For example:
The RemoveSSEEvent method removes an SSE event assigned with SetSSEEvent from the specified HTML element.
The method signature is:
| Parameter | Description |
|---|---|
InputPlace |
The WPC expression identifying the HTML element from which the SSE event is removed. |
HtmlEvent |
The HTML event associated with the SSE event. |
For example, if an SSE connection was previously assigned to the click event of RandomButton, the following command removes that assignment:
This method removes the event assignment. It does not define a new SSE endpoint or create another connection.
The RemoveSSEEventListener method removes an SSE event assigned through SetSSEEventListener.
The method signature is:
| Parameter | Description |
|---|---|
InputPlace |
The WPC expression identifying the HTML element from which the SSE listener is removed. |
HtmlEventListener |
The event listener associated with the SSE event. |
For example:
This removes the listener-based SSE event assignment from the selected element.
The SSE Event API provides a corresponding removal method for each event assignment mechanism:
| Set Method | Remove Method |
|---|---|
SetSSEEvent |
RemoveSSEEvent |
SetSSEEventListener |
RemoveSSEEventListener |
For example, an SSE event can first be assigned to a button and later removed:
Similarly, a listener-based SSE event can be assigned and removed:
The removal methods identify the existing event assignment by the selected element and its event type. They do not require the SSE Path because the purpose of the removal command is to remove the event assignment itself.
The SSE endpoint can return WebForms Core Action Controls instead of sending application-specific data that must then be interpreted by custom JavaScript.
For example, the server can create a WebForms instance and generate a DOM update:
Here, SetText creates a WebForms Core Action Control. The Action Control is then converted to the line-based representation required for an SSE response.
ExportToLineBreak() is specifically provided for SSE responses. It converts the WebForms Core response into a single-line representation compatible with the SSE transmission format.
The following example shows a complete SSE endpoint implemented using CodeBehind. The Backend opens the SSE response and periodically sends WebForms Core commands to the connected browser.
The important point is that EnableSSE() and BroadcastSSE() belong to the Backend implementation in this example. They are not WebForms Core methods. They are used by CodeBehind to establish and maintain the server-side SSE response.
WebForms Core only needs the resulting SSE response to contain the appropriate WebForms Core command format. The browser-side WebFormsJS runtime receives the response and executes the commands.
SSE is particularly useful when the server needs to continuously send updates without requiring a new request for every update.
For example, the following endpoint sends the current server time every five seconds:
The browser does not need to repeatedly request the server for the time. After the SSE connection has been established, the server can continuously send new WebForms Core commands through the connection.
The following example assigns three SSE connections to three different HTML events. The first two use HtmlEvent, while the third uses HtmlEventListener.
The resulting Action Controls are returned to the browser as an HTML comment. WebFormsJS reads the commands and assigns the SSE behavior to the specified elements.
SSE does not change the fundamental WebForms Core execution model. It changes the transport used for delivering commands from the server to the browser.
Instead of a conventional request followed by a single response, the server can keep an SSE connection open and send multiple WebForms Core command responses over time.
This allows the same WebForms Core command protocol to be used for both immediate server responses and continuous server-to-browser updates.
SetSSEEvent assigns an SSE connection to an HTML event.SetSSEEventListener provides the listener-based form.ReconnectTryTimeout specifies the reconnection interval in milliseconds.OutputPlace.ExportToLineBreak() produces the WebForms Core response representation intended for SSE transmission.Because WebForms Core is independent of the Backend ecosystem, the same SSE Event mechanism can be used with any server environment capable of providing SSE. The Backend is responsible for maintaining the SSE connection and producing the stream, while WebFormsJS is responsible for receiving and executing the WebForms Core commands.