Skip to main content
Home

Built and signed on GitHub Actions

Server-Sent Events (SSE): Source and Sink for handling both sides of communication

This package works with Deno, BrowsersIt is unknown whether this package works with Cloudflare Workers, Node.js, Bun
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
This package works with Browsers
JSR Score
76%
Published
2 months ago (0.2.8)

SSESource

The @planigale/sse library provides an easy and efficient way to handle Server-Sent Events (SSE) in JavaScript and TypeScript. It includes tools for both fetching and pushing events from/to the server.

  • Fetching Events: The SSESource class allows you to connect to an SSE endpoint, handle events individually or in a loop, and gracefully manage connection closures.
  • Pushing Events: The SSESink class facilitates sending events from the server to connected clients, making it simple to implement real-time updates in your application.

The library supports custom HTTP methods, headers, and request bodies, offering flexibility for various use cases.

Usage

Fetching events from the server

import { SSESource } from 'jsr:@planigale/sse';

const source = new SSESource('https://example.com/sse', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer token',
  },
  body: JSON.stringify({ key: 'value' }),
});

try {
  // Handling events one by one
  const { event, done } = await source.next();
  console.log('connection closed: ', done);
  if (!done) {
    console.log(event.data);
  }

  // Handling events in a loop
  for await (const event of source) {
    console.log(event.data);
  }
  console.log('connection closed');
} catch (e) {
  console.error(e);
}

Pushing events on the server

import { SSESink } from 'jsr:@planigale/sse';

Deno.serve((req) => {
  if (req.method === 'GET' && req.url === '/sse') {
    const sink = new SSESink();
    setInterval(() => {
      sink.sendMessage({ data: 'Hello, world!' });
    }, 1000);
    return sink.getResponse();
  }
  return Response.error();
});

License

MIT License

Copyright (c) 2024 Mateusz Russak

Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@planigale/sse

Import symbol

import * as sse from "@planigale/sse";
or

Import directly with a jsr specifier

import * as sse from "jsr:@planigale/sse";

Add Package

pnpm i jsr:@planigale/sse
or (using pnpm 10.8 or older)
pnpm dlx jsr add @planigale/sse

Import symbol

import * as sse from "@planigale/sse";

Add Package

yarn add jsr:@planigale/sse
or (using Yarn 4.8 or older)
yarn dlx jsr add @planigale/sse

Import symbol

import * as sse from "@planigale/sse";

Add Package

vlt install jsr:@planigale/sse

Import symbol

import * as sse from "@planigale/sse";

Add Package

npx jsr add @planigale/sse

Import symbol

import * as sse from "@planigale/sse";

Add Package

bunx jsr add @planigale/sse

Import symbol

import * as sse from "@planigale/sse";