@planigale/sse@0.2.8Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
latest
raaymax/planigaleServer-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




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
Add Package
deno add jsr:@planigale/sse
Import symbol
import * as sse from "@planigale/sse";
Import directly with a jsr specifier
import * as sse from "jsr:@planigale/sse";
Add Package
pnpm i jsr:@planigale/sse
pnpm dlx jsr add @planigale/sse
Import symbol
import * as sse from "@planigale/sse";
Add Package
yarn add jsr:@planigale/sse
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";