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

@spectrayan/react-sse

v2.1.0

Published

Idiomatic React hooks and context providers for Server-Sent Events (SSE), built on @spectrayan/sse-client.

Readme

⚛️ @spectrayan/react-sse

Idiomatic, production-ready React hooks for Server-Sent Events (SSE)

npm npm downloads React TypeScript License

Automatic lifecycle cleanup · Reconnect with jitter · Named events · Context pooling · SSR-safe


📦 Installation

npm install @spectrayan/react-sse @spectrayan/sse-client

Peer Dependencies

  • react: >=18.0.0 (Supports React 18 & 19)
  • @spectrayan/sse-client: ^2.0.0

🚀 Quick Start

1. Basic Stream with useSseStream

import React from 'react';
import { useSseStream } from '@spectrayan/react-sse';

interface Notification {
  id: string;
  message: string;
}

export function NotificationWidget() {
  const { data, status, error, reconnect, close } = useSseStream<Notification>(
    'https://api.example.com/sse/notifications',
    {
      reconnection: { initialDelayMs: 1000, maxDelayMs: 15000 },
    }
  );

  if (status === 'connecting') return <div>Connecting to live updates...</div>;
  if (status === 'error') return <div>Error: {error?.message} <button onClick={reconnect}>Retry</button></div>;

  return (
    <div>
      <h3>Live Alerts ({status})</h3>
      {data && <p>{data.message}</p>}
      <button onClick={close}>Disconnect</button>
    </div>
  );
}

2. Filtering Named Events with useSseEvent

When your SSE backend emits multiple named events on the same endpoint (e.g. event: trade, event: heartbeat):

import { useSseEvent } from '@spectrayan/react-sse';

export function TradeTicker() {
  const { data: trade } = useSseEvent<Trade>(
    'https://api.example.com/sse/market',
    'trade'
  );

  return <div>Latest Trade: {trade?.symbol} @ ${trade?.price}</div>;
}

3. Shared Context with SseProvider

Configure authentication tokens or global reconnection once at the top of your component tree:

import { SseProvider } from '@spectrayan/react-sse';

export function App() {
  return (
    <SseProvider
      config={{
        baseUrl: 'https://api.example.com',
        headers: async () => ({
          Authorization: `Bearer ${await getAuthToken()}`,
        }),
        reconnection: { jitterRatio: 0.25 },
      }}
    >
      <MainDashboard />
    </SseProvider>
  );
}

📡 API Reference

useSseStream<T>(url, options?)

Returns:

  • data: T | undefined — The latest parsed event data.
  • status: 'idle' | 'connecting' | 'open' | 'error' | 'closed' — Current connection state.
  • error: Error | null — Connection or parsing error if occurred.
  • reconnect: () => void — Manually forces a reconnection attempt.
  • close: () => void — Explicitly closes the stream.

Options:

  • enabled?: boolean (default: true) — Set to false to defer connecting.
  • initialData?: T — Initial state value before the first event arrives.
  • reconnection?: Partial<SseReconnectionConfig> — Override backoff multiplier, jitter, and delay caps.
  • headers?: Record<string, string> — Custom request headers.

📄 License

Distributed under the Apache License 2.0. See LICENSE for details.