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-pusher-client

v1.0.2

Published

Lightweight React hooks for Pusher (with client SDK pattern)

Readme

📡 react-pusher-client

npm version License Build Status GitHub stars GitHub Repo

A lightweight React/Next.js wrapper for Pusher that simplifies client-side usage with hooks, connection state tracking, and auto-channel binding.


✨ Features

  • ✅ Simple Pusher client initialization
  • 🔄 Track connection state with hooks
  • 🧠 Auto-bind to channels/events
  • 🧪 Works seamlessly in React or Next.js apps
  • 🧩 Ideal for real-time dashboards, chat, notifications
  • 🔧 Full control over Pusher client and custom hooks:

📦 Installation

npm install react-pusher-client pusher-js

🏁 Getting Started

  1. Initialize Pusher Client 🔑

The first step is to initialize Pusher by providing your Pusher Key and optionally configuring the options (typically in a top-level component).

import { initPusher } from 'react-pusher-client';
// Initialize Pusher in your app
initPusher(process.env.NEXT_PUBLIC_PUSHER_KEY!, {
  cluster: 'your-cluster',
});
  1. Using Hooks 📌

After initializing Pusher, you can use the provided hooks to interact with Pusher Channels, monitor connection states, and handle events.

API Reference 📚

initPusher(key: string, options?: Pusher.Options) 🔧

Description: Initializes the Pusher client with the provided key and optional configuration. This function must be called once at the entry point of your app, before using any other hook.

Parameters:

key: The Pusher key obtained from your Pusher Dashboard.

options (optional): Additional configuration options for Pusher (e.g., cluster, encrypted).

Usage:

import { initPusher } from 'react-pusher-client';

// Initialize the Pusher client
initPusher('your-pusher-key', {
  cluster: 'your-cluster',
});

usePusherEvent(channelName: string, eventName: string): T | null 🔄

Description: A hook that listens for events on a specified Pusher channel and returns the event data.

Parameters:

channelName: The name of the channel to subscribe to.

eventName: The name of the event to listen for.

Returns:

The event data (T) received from Pusher, or null if no data is received.

Usage:

import { usePusherEvent } from 'react-pusher-client';

const message = usePusherEvent<{ user: string; message: string }>('chat', 'new-message');

return (
  <div>
    {message ? (
      <p>
        <strong>{message.user}:</strong> {message.message}
      </p>
    ) : (
      <p>Waiting for messages...</p>
    )}
  </div>
);

usePusherConnection(): string 🌐

Description: A hook that tracks the Pusher connection state. The connection state can be one of the following:

connecting

connected

disconnected

reconnecting

failed

Returns:

The current connection state as a string.

Usage:

import { usePusherConnection } from 'react-pusher-client';

const connectionStatus = usePusherConnection();

return <p>Connection Status: {connectionStatus}</p>;

usePresenceChannel(channelName: string): PresenceMember[] 🧑‍🤝‍🧑

Description: A hook that listens for updates on a Presence Channel and returns the list of online members. Presence channels are used to track users in real-time.


Parameters:

channelName: The name of the presence channel to subscribe to.

Returns:

An array of presence members. Each member has an id and other custom properties.

# Usage:

```typescript
import { usePresenceChannel } from 'react-pusher-client';

const onlineUsers = usePresenceChannel('chat');

return (
  <div>
    <h3>Online Users:</h3>
    <ul>
      {onlineUsers.map((user) => (
        <li key={user.id}>{user.id}</li>
      ))}
    </ul>
  </div>
);

Custom Hook Example:

If you want full control over the Pusher client, you can directly access it via getPusher():

import { useEffect, useState } from 'react';
import { getPusher } from './lib/pusherClient';

const useCustomChannel = (channelName: string) => {
  const [messages, setMessages] = useState<any[]>([]);

  useEffect(() => {
    const pusher = getPusher();
    const channel = pusher!.subscribe(channelName);

    const handleNewMessage = (message: any) => {
      setMessages((prevMessages) => [...prevMessages, message]);
    };

    channel.bind('new-message', handleNewMessage);

    return () => {
      channel.unbind('new-message', handleNewMessage);
      pusher!.unsubscribe(channelName);
    };
  }, [channelName]);

  return messages;
};

Environment Variables 🌍

You should set the following environment variables in your .env.local (or .env) file:

NEXT_PUBLIC_PUSHER_KEY=your-pusher-key
NEXT_PUBLIC_PUSHER_CLUSTER=your-cluster

This will make sure the Pusher key and cluster are accessible throughout your app, especially during development and production builds.

Best Practices 💡

  • Single Initialization: Always call initPusher at the entry point of your app (e.g., in _app.tsx for Next.js apps) to ensure that the Pusher client is initialized only once.
  • Efficient Event Listening: Use the usePusherEvent hook for event-based updates and the usePusherConnection hook to track connection status. Avoid subscribing to unnecessary channels or events to minimize unnecessary re-renders and optimize performance.
  • Presence Channel Management: Use usePresenceChannel to track the state of users in real-time. Make sure to unsubscribe from channels when they are no longer needed to avoid memory leaks.

Troubleshooting ⚠️

If you encounter any issues, please check the following:

Ensure that you have correctly set your Pusher key and cluster in your environment variables. Ensure that you are only calling initPusher once in your app. Verify that the channelName and eventName you are using are correct.

Contributing 🤝

We welcome contributions to make pusher-react better! If you have ideas, bugs, or features you'd like to add:

  • Fork the repo
  • Create your feature branch: git checkout -b feat/your-feature
  • Commit your changes: git commit -m 'Add amazing feature'
  • Push to the branch: git push origin feat/your-feature
  • Open a pull request ✅

GitHub Repository: github.com/devnickjr/pusher-react

License 📄

This project is licensed under the MIT License - see the LICENSE file for details.