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

@nayaabh/react-broadcast-channel

v0.1.4

Published

A React hook for broadcasting and subscribing to messages using the BroadcastChannel API.

Readme

React Broadcast Channel

A lightweight, intuitive, and scalable library for broadcasting messages between components, tabs, iframes, and windows. It empowers developers to share low-latency data and states effortlessly across different parts of an application or between multiple windows, using simple react hooks.

It uses BroadcastChannel API under the hood.

Key Features

  • Low-Latency Data Sharing: Share data instantly between components, tabs, iframes, and windows without polling or server-side communication.
  • Cross-Windows Messaging: Send messages between components, tabs, iframes, and windows seamlessly.
  • Effortless State Management: Manage state changes across your application with our intuitive useBroadcastState hook.

Demo

Demo GIF

Installation

npm install @nayaabh/react-broadcast-channel

Getting Started

useBroadcastState

This hook provides a stateful way to manage the current message received from the channel. It returns an array with two elements: the current message state object and a function to send a new message.

"use client";

import { useBroadcastState } from "@nayaabh/react-broadcast-channel";
import { useCallback, useEffect, useState } from "react";

const CHANNEL_NAME = "BroadcastChannel-001";

export const BroadcastPlayBox = () => {
  const [message, setMessage] = useBroadcastState<string>(CHANNEL_NAME);
  const [logs, setLogs] = useState<string[]>([]);

  const onPublish = useCallback((e: any) => {
    e.preventDefault();
    const data = new FormData(e.target);
    const text = data.get("message") as string;
    const dateTime = getTimestamp();
    // Send message to all connected clients
    setMessage(`${dateTime} - ${text}`);
  }, []);

  useEffect(() => {
    if (message) {
      setLogs((logs) => [message, ...logs]);
    }
  }, [message]);
  return (
    <>
      <form onSubmit={onPublish}>
        <label htmlFor="message">Enter your message:</label>
        <input name="message" />
        <button type="submit">Publish</button>
      </form>
      <ol>
        {logs.map((log) => (
          <li key={log}>
            <pre>{log}</pre>
          </li>
        ))}
      </ol>
    </>
  );
};

function getTimestamp() {
  const date = new Date();
  const year = date.getFullYear();
  const month = `${date.getMonth() + 1}`.padStart(2, "0");
  const day = `${date.getDate()}`.padStart(2, "0");
  const hour = `${date.getHours()}`.padStart(2, "0");
  const minute = `${date.getMinutes()}`.padStart(2, "0");
  const second = `${date.getSeconds()}`.padStart(2, "0");
  const millisecond = `${date.getMilliseconds()}`.padStart(2, "0");
  return `${year}-${month}-${day} ${hour}:${minute}:${second}.${millisecond}`;
}

useBroadcastChannel

This hook provides a way to post messages to the channel and handle incoming messages using callbacks. It takes a channel name and an optional callback function to handle incoming messages.

"use client";

import { useBroadcastChannel } from "@nayaabh/react-broadcast-channel";
import { useCallback, useState } from "react";

const CHANNEL_NAME = "BroadcastChannel-001";

export const BroadcastPlayBox = () => {
  const [logs, setLogs] = useState<string[]>([]);
  const appendLogs = (message: string | null) => {
    if (message) {
      setLogs((logs) => [message, ...logs]);
    }
  };
  const [postMessage] = useBroadcastChannel<string>(CHANNEL_NAME, appendLogs);

  const onPublish = useCallback((e: any) => {
    e.preventDefault();
    const data = new FormData(e.target);
    const text = data.get("message") as string;
    const log = `${getTimestamp()} - ${text}`;
    postMessage(log); // Send message to all connected clients
    setLogs((logs) => [log, ...logs]); // update local state
  }, []);

  return (
    <>
      <form onSubmit={onPublish}>
        <label htmlFor="message">Enter your message:</label>
        <input name="message" />
        <button type="submit">Publish</button>
      </form>
      <ol>
        {logs.map((log) => (
          <li key={log}>
            <pre>{log}</pre>
          </li>
        ))}
      </ol>
    </>
  );
};

function getTimestamp() {
  const date = new Date();
  const year = date.getFullYear();
  const month = `${date.getMonth() + 1}`.padStart(2, "0");
  const day = `${date.getDate()}`.padStart(2, "0");
  const hour = `${date.getHours()}`.padStart(2, "0");
  const minute = `${date.getMinutes()}`.padStart(2, "0");
  const second = `${date.getSeconds()}`.padStart(2, "0");
  const millisecond = `${date.getMilliseconds()}`.padStart(2, "0");
  return `${year}-${month}-${day} ${hour}:${minute}:${second}.${millisecond}`;
}

API Reference

useBroadcastChannel(channelName: string, onMessage: (message: T | null) => void = () => {}): BroadcastChannelProps<T>

  • Creates a new BroadcastChannel instance with the specified channel name.
  • The onMessage callback is called whenever a message is received from another client.
  • Returns: [postMessage, closeChannel]:
    • postMessage sends a message to all connected clients
    • closeChannel closes the channel.

useBroadcastState(channelName: string): BroadcastStateProps<T>

  • Provides a stateful hook for managing messages in a broadcast channel.
  • Returns: [message, sendMessage]
    • message is the current message
    • sendMessage is a function to send a new message.

References

📔 Documentation

Demo 🚀