EventSource
The EventSource ↗ interface is a server-sent event API that allows a server to push events to a client. The EventSource object is used to receive server-sent events. It connects to a server over HTTP and receives events in a text-based format.
let eventSource = new EventSource(url, options);urlUSVString - The URL to which to connect.optionsEventSourceInit - An optional dictionary containing any optional settings.
By default, the EventSource will use the global fetch() function under the
covers to make requests. If you need to use a different fetch implementation as
provided by a Cloudflare Workers binding, you can pass the fetcher option:
export default { async fetch(req, env) { let eventSource = new EventSource(url, { fetcher: env.MYFETCHER }); // ... }};Note that the fetcher option is a Cloudflare Workers specific extension.
eventSource.urlUSVString read-only- The URL of the event source.
eventSource.readyStateUSVString read-only- The state of the connection.
eventSource.withCredentialsBoolean read-only- A Boolean indicating whether the
EventSourceobject was instantiated with cross-origin (CORS) credentials set (true), or not (false).
- A Boolean indicating whether the
eventSource.close()- Closes the connection.
eventSource.onopen- An event handler called when a connection is opened.
eventSource.onmessage- An event handler called when a message is received.
eventSource.onerror- An event handler called when an error occurs.
message- Fired when a message is received.
open- Fired when the connection is opened.
error- Fired when an error occurs.
-
EventSource.from(readableStreamReadableStream) : EventSource- This is a Cloudflare Workers specific extension that creates a new
EventSourceobject from an existingReadableStream. Such an instance does not initiate a new connection but instead attaches to the provided stream.
- This is a Cloudflare Workers specific extension that creates a new