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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-use-pusher

v0.5.0

Published

> Easy as [React hooks](https://reactjs.org/docs/hooks-intro.html) that integrate with the [`pusher-js`](https://github.com/pusher/pusher-js) library.

Downloads

48

Readme

react-use-pusher

Easy as React hooks that integrate with the pusher-js library.

Install

npm install react-use-pusher

Usage

You must wrap your app with a PusherProvider and pass it config props for pusher-js initialisation.

import React from 'react';
import { PusherProvider } from 'react-use-pusher';

const App = () => {
  <PusherProvider
    clientKey={process.env.CLIENT_KEY}
    cluster="eu"
    
    // you can set this to false if you're waiting on some state to load
    // and want to wait for it before the WS connection happens
    ready
    // if you're using presence channels
    // see: https://pusher.com/docs/channels/server_api/authorizing-users/#implementing-the-authorization-endpoint-for-a-presence-channel 
    channelAuthEndpoint="/api/auth"
    // TIP: if you're hosting your own websocket server via something like Soketi
    // you can pass additional pusher options for the websocket connection via the prop bellow
    // recommend you either memoise these options or
    // use a statically defined object outside the component if possible
      
    // options={{ wsHost: 'MY_HOST', wssPort: 1234, wsPort:1233, wsPath: '/my-path' }}
  >
    <Example />
  </PusherProvider>;
};

useChannel

Use this hook to subscribe to a channel.

// returns Channel instance.
const channel = useChannel('channel-name');

usePrivateChannel

Use this hook to subscribe to a private channel.

If you're using presence channels, you must provide an auth endpoint.

See the Pusher Docs for examples for how the endpoint is implemented.

// returns PrivateChannel instance.
const channel = usePrivateChannel('private-name');

usePresenceChannel

Presence channels allow you to see who else is connected

If you're using presence channels, you must provide an auth endpoint.

See the Pusher Docs for examples for how the endpoint is implemented.

const Example = () => {
  const { members, myID } = usePresenceChannel('presence-dashboard');

  return (
    <ul>
      {members
        // filter self from members
        .filter(({ id }) => id !== myID)
        // map them to a list
        .map(({ id, name }) => (
          <li key={id}>{name}</li>
        ))}
    </ul>
  );
};

useEvent

Bind to events on a channel with a callback.

const Example = () => {
  const [message, setMessages] = useState();
  const channel = useChannel('channel-name');
  useEvent(channel, 'event name', (data) => console.log(data));
};

Note: This will bind and unbind to the event on each render. You may want to memoise your callback with useCallback before passing it in.

useClientTrigger

Message other clients directly. use client events to push directly from the client:

import { useChannel, useClientTrigger } from 'react-use-pusher';

const Example = () => {
  const channel = useChannel('presence-dashboard');
  const trigger = useClientTrigger(channel);
  const handleClientEvent = () => {
    trigger('event name', eventData);
  };

  return <button onClick={handleClientEvent}>Fire</button>;
};

usePusher

Get access to the Pusher instance to do other things.

import { usePusher } from 'react-use-pusher';

const Example = () => {
  const { client } = usePusher();

  return null;
};

Authentication endpoint example

I'm using a NextJS api endpoint page as an example.

  npm install pusher
// pages/api/auth.ts

// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import Pusher from 'pusher';

type Modify<T, K extends keyof T, U> = Omit<T, K> & {
    [P in keyof Pick<T, K>]: U;
};

type AuthBody = {
    socket_id: string;
    channel_name: string;
    [key: string]: unknown;
};

const pusher = new Pusher({
    appId: process.env.APP_ID,
    key: process.env.APP_KEY,
    secret: process.env.APP_SECRET,
    cluster: 'eu',
});

export default function handler(
    req: Modify<NextApiRequest, 'body', AuthBody>,
    res: NextApiResponse,
) {
    const { socket_id, channel_name, ...rest } = req.body;

    const channelData = {
        // user_id is mandatory according to pusher docs
        user_id: 'the user id',
        user_info: {
            // name within user_info is mandatory
            name: 'the authenticated user name',
            // you can put whatever you like here. It'll appear in the presence channel member's data
            ...rest,
        },
    };
    res.status(200).json(pusher.authorizeChannel(socket_id, channel_name, channelData));
}