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

react-use-sse

v0.0.4

Published

React hook for real-time updates with SSE. Plug in, Stream on. ⚡️

Readme

React SSE

React hook for real-time updates with SSE. Plug in, Stream on. ⚡️

Installation

npm install react-use-sse

Usage

This library provides a single hook, useSSE, which you can use to connect to a Server-Sent Events (SSE) endpoint.

The API is similar to useQuery from @tanstack/react-query, but it's designed specifically for SSE:

import React from 'react';
import { useSSE } from 'react-use-sse';

function App() {
  const { data, isPending, isError } = useSSE({
    url: 'https://server.com/stream',
  });

  if (isPending) {
    return <div>Loading...</div>;
  }

  if (isError) {
    return <div>Error occurred while fetching data.</div>;
  }

  return (
    <div>
      <h1>React SSE</h1>
      <p>Updated value from server: {data}</p>
    </div>
  );
}

Each time the server sends an update, the hook will re-render your component with the new data.

Transforming the Data

You can also pass a transform function to modify the data before it is returned, since the data received from the server is a string:

import React from 'react';
import { useSSE } from 'react-use-sse';

function App() {
  const { data } = useSSE<{ valueFromServer: number }>({
    url: 'https://server.com/stream',
    transform: (rawData: string) => JSON.parse(rawData),
  });

  return (
    <div>
      <h1>React SSE</h1>
      <p>Updated value from server: {data.valueFromServer}</p>
    </div>
  );
}

The data type will either be inferred from the transform function or can be explicitly defined in the hook call (as shown above).

Custom Events

SSE supports custom events. To use them, you can pass the custom event name using the event option:

useSSE({
  url: 'https://server.com/stream',
  event: 'custom-event',
});

Attaching Credentials

You can also attach the user's credentials by passing a withCredentials option to the hook call:

useSSE({
  url: 'https://server.com/stream',
  withCredentials: true,
});

Development

To run the development client and server use:

npm run dev

This will start the client on http://localhost:5173 and the server on http://localhost:8888.

There is no need to open the server URL in the browser, as the client will automatically connect to it (and because you'll be stuck in an infinite loop).