Skip to content

re-utils/live-sse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

live-sse

Emit events to SSE streams.

Usage:

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

const chan = sse.channel();

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

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

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

export default {
  fetch: () => new Response(
    sse.toWebStream(chan),
    {
      headers: {
        'content-type': 'text/event-stream',
        'cache-control': 'no-cache'
      }
    }
  )
}

// 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