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

fibersse-react

v0.1.0

Published

React hooks for fibersse — replace polling with SSE invalidation in one line

Readme

@fibersse/react

React hooks for fibersse — replace polling with SSE cache invalidation in one line.

Install

npm install @fibersse/react

Peer dependency: React 18+

Quick Start

import { useSSEInvalidation } from '@fibersse/react';
import { useQueryClient } from '@tanstack/react-query';

function App() {
  const queryClient = useQueryClient();

  const { connected } = useSSEInvalidation({
    topics: ['orders', 'products', 'dashboard'],
    onInvalidate: (resource, action, id) => {
      queryClient.invalidateQueries({ queryKey: [resource] });
      if (id) queryClient.invalidateQueries({ queryKey: [resource, id] });
    },
    onProgress: (id, current, total, pct) => {
      console.log(`Import ${id}: ${pct}%`);
    },
    onComplete: (id, status) => {
      if (status === 'completed') toast.success('Done!');
    },
  });

  return <span>{connected ? 'Live' : 'Connecting...'}</span>;
}

That's it. No setInterval. No polling. Your queries refetch only when data actually changes.

Hooks

useSSEInvalidation — High-Level (Recommended)

The primary hook for replacing polling. Connects to a fibersse hub and routes events to your cache layer.

const { connected, connectionId, disconnect, reconnect } = useSSEInvalidation({
  // Required
  topics: ['orders', 'dashboard'],

  // Cache invalidation (the main thing)
  onInvalidate: (resource, action, resourceId, hint) => {
    queryClient.invalidateQueries({ queryKey: [resource] });
  },

  // Batch invalidation (multiple resources in one event)
  onBatch: (events) => {
    events.forEach(e => queryClient.invalidateQueries({ queryKey: [e.resource] }));
  },

  // Progress tracking (coalesced — 1000 updates → ~15 events)
  onProgress: (resourceId, current, total, pct, hint) => {
    setProgress(pct);
  },

  // Completion signals
  onComplete: (resourceId, status, hint) => {
    if (status === 'completed') refetchEverything();
  },

  // Generic refresh signals
  onSignal: () => {
    queryClient.invalidateQueries();
  },

  // Optional config
  url: '/api/v1/events/stream',       // SSE endpoint (default)
  ticketUrl: '/api/sse/ticket',        // Ticket endpoint (default)
  enabled: isAuthenticated,             // Connect only when ready
  visibilityAware: true,                // Disconnect on hidden tab (default)
  maxReconnectAttempts: 8,              // Exponential backoff
});

useSSE — Low-Level

Full control over event handling. Use when you need custom event types beyond invalidation.

const { connected } = useSSE({
  topics: ['notifications', 'live'],
  onEvent: {
    notification: (data) => showToast(data),
    live_visitors: (data) => setVisitors(data),
    intelligence: (data) => setSnapshot(data),
  },
  onConnect: ({ connection_id, topics }) => {
    console.log('Connected:', connection_id);
  },
  onServerShutdown: () => {
    console.log('Server draining, will reconnect...');
  },
});

With SWR

import { useSWRConfig } from 'swr';

const { mutate } = useSWRConfig();

useSSEInvalidation({
  topics: ['orders'],
  onInvalidate: (resource) => mutate(`/api/${resource}`),
});

Features

  • Ticket authentication — POST for a one-time ticket, then connect with it (EventSource can't send headers)
  • Exponential backoff — 3s → 6s → 12s → ... → 60s cap, with jitter
  • Visibility-aware — disconnects when tab is hidden, reconnects when visible
  • Type-safe — full TypeScript types for all event payloads
  • Zero runtime dependencies — only React as a peer dependency
  • Framework agnostic — works with TanStack Query, SWR, Zustand, Redux, or plain setState

Auth Flow

1. Client calls POST /api/sse/ticket (with JWT cookie/header)
2. Server returns { ticket: "one-time-token", topics: [...] }
3. Client opens EventSource at /events?ticket=TOKEN&topics=orders,dashboard
4. On disconnect/error → exponential backoff → get new ticket → reconnect

License

MIT — Vinod Morya