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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-hooks-sse

v2.1.0

Published

React Hook for SSE

Downloads

5,775

Readme

React Hooks SSE

npm version Build Status

Installation

yarn add react-hooks-sse

Usage

import React from 'react';
import { useSSE, SSEProvider } from 'react-hooks-sse';

const Comments = () => {
  const state = useSSE('comments', {
    count: null
  });

  return state.count ? state.count : '...';
};

const App = () => (
  <SSEProvider endpoint="https://sse.example.com">
    <h1>Subscribe & update to SSE event</h1>
    <Comments />
  </SSEProvider>
);

Checkout the example on the project

API

SSEProvider

The provider manages subscriptions to the SSE server. You can subscribe multiple times to the same event or on different events. The source is lazy, it is created only when one of the hooks is called. The source is destroyed when no more hooks are registered. It is automatically re-created when a new hook is added.

Usage

import React from 'react';
import { SSEProvider } from 'react-hooks-sse';

const App = () => (
  <SSEProvider endpoint="https://sse.example.com">
    {/* ... */}
  </SSEProvider>
);

endpoint: string

The value is required when source is omitted.

The SSE endpoint to target. It uses the default source EventSource.

import React from 'react';
import { SSEProvider } from 'react-hooks-sse';

const App = () => (
  <SSEProvider endpoint="https://sse.example.com">
    {/* ... */}
  </SSEProvider>
);

source: () => Source

The value is required when endpoint is omitted.

You can provide custom source to the provider. The main use cases are:

Here is the interface that a source has to implement:

interface Event {
  data: any;
}

interface Listener {
  (event: Event): void;
}

interface Source {
  addEventListener(name: string, listener: Listener): void;
  removeEventListener(name: string, listener: Listener): void;
  close(): void;
}

The source is lazy, it is created only when a hook is added. That's why we provide a function to create a source not a source directly.

import React from 'react';
import { SSEProvider } from 'react-hooks-sse';
import { createCustomSource } from 'custom-source';

const App = () => (
  <SSEProvider source={() => createCustomSource()}>
    {/* ... */}
  </SSEProvider>
);

useSSE<S, T>(eventName: string, initialState: S, options?: Options<S, T>)

The component that uses the hook must be scoped under a SSEProvider to have access to the source. Once the hook is created none of the options can be updated (at the moment). You have to unmout/remount the component to update the options.

Usage

const state = useSSE('comments', {
  count: null
});

eventName: string

The name of the event that you want to listen.

const state = useSSE('comments', {
  count: null
});

initialState: S

The initial state to use on the first render.

const state = useSSE('comments', {
  count: null
});

options?: Options<S, T>

The options to control how the data is consumed from the source.

type Action<T> = { event: Event; data: T };
type StateReducer<S, T> = (state: S, changes: Action<T>) => S;
type Parser<T> = (data: any) => T;

export type Options<S, T = S> = {
  stateReducer?: StateReducer<S, T>;
  parser?: Parser<T>;
};

options.stateReducer?: <S, T>(state: S, changes: Action<T>) => S

The reducer to control how the state should be updated.

type Action<T> = {
  // event is provided by the source
  event: Event;
  // data is provided by the parser
  data: T;
};

const state = useSSE<S, T>(
  'comments',
  {
    count: null,
  },
  {
    stateReducer(state: S, action: Action<T>) {
      return changes.data;
    },
  }
);

options.parser?: <T>(data: any) => T

The parser to control how the event from the server is provided to the reducer.

const state = useSSE<S, T>(
  'comments',
  {
    count: null,
  },
  {
    parser(input: any): T {
      return JSON.parse(input);
    },
  }
);

Run example

yarn start:server
yarn start:example

Run the build

yarn build

Run the test

yarn test