Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/esm/sse.js>EventSource

A JavaScript extension package for building strong and modern applications.
Latest
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

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);
});

Constructors

new
EventSource(url, options?)

Properties

onerror
onmessage
onopen
readonly
readyState
readonly
url
readonly
withCredentials