npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lndr/sse

v1.0.2

Published

Server-sent-events

Readme

@lndr/sse

A lightweight library for implementing Server-Sent Events (SSE) on the backend. This library provides an easy-to-use API for managing SSE connections and broadcasting events to connected clients.

Features

  • Simple singleton-based architecture
  • Automatic connection lifecycle management
  • Support for targeted and broadcast messaging
  • Framework-agnostic (works with any Node.js HTTP server)
  • TypeScript support with full type definitions
  • Automatic SSE header configuration

Installation

npm install @lndr/sse

Quick Start

import http from 'http';
import { Server, Hub } from '@lndr/sse';

const server = http.createServer((req, res) => {
	if (req.url === '/events') {
		// Create an SSE connection
		Server.createConnection(req, res);
		return;
	}

	// Broadcast an event to all connected clients
	Hub.emit('message', { text: 'Hello, World!' });
	res.end('Event sent');
});

server.listen(3000);

Server

The Server singleton manages all SSE connections.

Server.createConnection(req, res)

Creates a new SSE connection from an HTTP request/response pair.

import { Server } from '@lndr/sse';

// In your HTTP handler
Server.createConnection(req, res);

The connection ID is automatically determined from:

  1. The x-request-id header (if present)
  2. A randomly generated UUID v4 (fallback)

Hub

The Hub singleton provides a fluent API for emitting events.

Hub.emit(event, data?)

Broadcasts an event to all connected clients.

import { Hub } from '@lndr/sse';

Hub.emit('update', { status: 'complete' });

Hub.to(id).emit(event, data?)

Sends an event to a specific connection.

// Send to a specific client
Hub.to('user-123').emit('private-message', { text: 'Hello!' });

// Broadcast to all (equivalent to Hub.emit)
Hub.to('*').emit('announcement', { text: 'Welcome!' });

Connection

Each connection automatically configures the required SSE headers:

  • Content-Type: text/event-stream
  • Cache-Control: no-cache
  • Connection: keep-alive

Usage Examples

Basic Express.js Integration

import express from 'express';
import { Server, Hub } from '@lndr/sse';

const app = express();

// SSE endpoint
app.get('/events', (req, res) => {
	Server.createConnection(req, res);
});

// Trigger an event
app.post('/notify', (req, res) => {
	Hub.emit('notification', { message: 'New notification!' });

	res.json({ success: true });
});

app.listen(3000);

Targeted Messaging

import { Server, Hub } from '@lndr/sse';

// Client connects with a custom request ID
// GET /events with header: x-request-id: user-456
app.get('/events', (req, res) => {
	Server.createConnection(req, res);
});

// Send a message to a specific user
app.post('/message/:userId', (req, res) => {
	const { userId } = req.params;

	Hub.to(userId).emit('direct-message', {
		from: 'admin',
		text: 'Hello!'
	});

	res.json({ sent: true });
});

TypeScript with Custom Types

import { Server, Hub } from '@lndr/sse';

interface NotificationPayload {
	id: string;
	title: string;
	body: string;
	timestamp: number;
}

// Type-safe event emission
Hub.emit<NotificationPayload>('notification', {
	id: 'notif-1',
	title: 'New Message',
	body: 'You have a new message',
	timestamp: Date.now()
});

Client-Side Usage

Connect to your SSE endpoint using the browser's EventSource API:

const eventSource = new EventSource('/events');

// Listen for specific events
eventSource.addEventListener('notification', (event) => {
	const data = JSON.parse(event.data);
	console.log('Notification:', data);
});

// Listen for all messages
eventSource.onmessage = (event) => {
	console.log('Message:', event.data);
};

// Handle connection errors
eventSource.onerror = (error) => {
	console.error('SSE error:', error);
};

SSE Message Format

Events are sent in the standard SSE format:

id: <uuid>
event: <event-name>
data: <json-payload>

Example:

id: 550e8400-e29b-41d4-a716-446655440000
event: notification
data: {"message":"Hello, World!"}

Connection Lifecycle

  • Connections are automatically tracked when created via Server.createConnection()
  • When a client disconnects, the connection is automatically removed from the pool
  • Connection IDs can be specified via the x-request-id header for targeted messaging