Skip to content

re-utils/live-sse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

live-sse

Emit events to SSE streams.

Usage:

// server.ts
import { sse } from 'live-sse';

const c = sse.channel();

setInterval(() => {
  // Shorthand for sse.send(c, 'event: update\ndata: ');
  sse.send(c, sse.startEvent('update'));

  // Update event data
  sse.send(c, 'Hello world');

  // Mark the end of the data section
  sse.send(c, sse.dataEnd);
}, 1000);

export default {
  // Create a Request => Response handler that streams the content
  fetch: sse.stream(c)
}

// client.ts
const events = new EventSource(serverUrl);

events.addEventListener('update', (event) => {
  // Print 'Hello world' every 1s
  console.log(event.data);
});

live-sse also provides an API optimized for bun:

import { sse } from 'live-sse/bun';

About

Do live updates with SSE

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published