class EventSource
extends EventTarget
import { EventSource } from "https://deno.land/x/ayonli_jsext@v0.9.72/esm/sse.js";
This is an implementation of the EventSource API that serves as a polyfill in environments that do not have native support, such as Node.js.
NOTE: This API depends on the Fetch API and Web Streams API, in Node.js, it requires Node.js v18.0 or above.
Examples
Example 1
Example 1
import { EventSource } from "@ayonli/jsext/sse";
globalThis.EventSource ??= EventSource;
const events = new EventSource("http://localhost:3000");
events.addEventListener("open", () => {
console.log("The connection is open.");
});
events.addEventListener("error", (ev) => {
console.error("An error occurred:", ev.error);
});
events.addEventListener("message", (ev) => {
console.log("Received message from the server:", ev.data);
});
events.addEventListener("my-event", (ev) => {
console.log("Received custom event from the server:", ev.data);
});
Methods
addEventListener()
event,
listener,
options?,
close()
Closes the connection.
connect()
removeEventListener()
type,
listener,
options?,