class EventSource
Supported only in the v8 engine. Introduced in Max v9.2.0.
A Server-Sent Events client.
URLs must be absolute http:// or https:// URLs. Successful connections require 200 OK and a content-type beginning with text/event-stream. On failure the client dispatches error and reconnects (default delay 3000 ms, overridable by the stream's retry: field).
Example
const source = new EventSource("http://127.0.0.1:8093/sse/basic");
source.onopen = () => post("sse open\n");
source.onmessage = (event) => {
post(event.data, "\n");
source.close();
};
source.onerror = () => post("sse error\n");
Index
Constructors
Properties
Methods
new EventSource(url, options)
new EventSource(url: string, options?: { withCredentials?: boolean });
Constructs a new instance of the EventSource class
| Parameter | Type | Description |
|---|---|---|
| url | string | an absolute http:// or https:// URL. |
| optional options | object | withCredentials controls cross-origin cookie use.optional options.withCredentials boolean |
addEventListener(type, listener)
Registers a listener. Use a custom type to receive named event: messages from the stream.
addEventListener(
type: string,
listener: (event: EventSourceMessageEvent) => void,
): void;
| Name | Type | Description |
|---|---|---|
| type | string | |
| listener | (event: EventSourceMessageEvent) => void |
close()
Closes the connection permanently (no reconnect).
close(): void;
CLOSED number read-only
Closed state constant.
CONNECTING number read-only
Connecting state constant.
onerror ((event: { type: string }) => void) | null
Called on error (before an automatic reconnect).
onmessage ((event: EventSourceMessageEvent) => void) | null
Called when a default (unnamed) message arrives.
onopen ((event: { type: string }) => void) | null
Called when the connection opens.
OPEN number read-only
Open state constant.
readyState number read-only
The connection state.
removeEventListener(type, listener)
Removes an event listener.
removeEventListener(
type: string,
listener: (event: EventSourceMessageEvent) => void,
): void;
| Name | Type | Description |
|---|---|---|
| type | string | |
| listener | (event: EventSourceMessageEvent) => void |
url string read-only
The stream URL.
withCredentials boolean read-only
Whether cross-origin credentials are used.