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

@zocket/react

v0.1.0

Published

Type-safe React hooks for Zocket WebSockets

Readme

@zocket/react

Type-safe React hooks for Zocket WebSockets. Build real-time applications with end-to-end type safety.

Installation

bun add @zocket/react @zocket/core zod

Quick Start

1. Define your router (server-side)

import { z } from "zod";
import { zocket } from "@zocket/core";

const zo = zocket.create();

const appRouter = {
  posts: {
    create: zo.message.incoming({
      payload: z.object({ title: z.string() }),
    }),
    created: zo.message.outgoing({
      payload: z.object({
        id: z.string(),
        title: z.string(),
      }),
    }),
  },
  users: {
    joined: zo.message.outgoing({
      payload: z.object({ name: z.string() }),
    }),
  },
};

export type AppRouter = typeof appRouter;

2. Wrap your app with ZocketProvider

import { ZocketProvider } from "@zocket/react";
import type { AppRouter } from "./server";

function App() {
  return (
    <ZocketProvider<AppRouter>
      url="ws://localhost:3000"
      onOpen={() => console.log("Connected!")}
      onClose={() => console.log("Disconnected")}
    >
      <YourApp />
    </ZocketProvider>
  );
}

3. Use type-safe hooks in any component

import { useZocket } from "@zocket/react";
import type { AppRouter } from "./server";

function ChatComponent() {
  const { client, useEvent } = useZocket<AppRouter>();
  const [messages, setMessages] = useState([]);

  useEvent(client.on.posts.created, (data) => {
    setMessages((prev) => [...prev, data]);
  });

  useEvent(client.on.users.joined, (data) => {
    console.log(`${data.name} joined!`);
  });

  const sendMessage = () => {
    client.send.posts.create({ title: "Hello!" });
  };

  return (
    <div>
      <button onClick={sendMessage}>Send</button>
      {messages.map((msg) => (
        <div key={msg.id}>{msg.title}</div>
      ))}
    </div>
  );
}

API Reference

<ZocketProvider>

Provider component that creates and manages a WebSocket connection.

Props:

  • url: string - WebSocket URL to connect to
  • children: ReactNode - React children
  • maxReconnectionDelay?: number - Max delay between reconnection attempts (default: 10000ms)
  • minReconnectionDelay?: number - Min delay between reconnection attempts (default: 1000ms)
  • reconnectionDelayGrowFactor?: number - Growth factor for reconnection delay (default: 1.3)
  • maxRetries?: number - Maximum reconnection attempts (default: Infinity)
  • debug?: boolean - Enable debug logging (default: false)
  • headers?: Record<string, string> - Custom headers for connection
  • onOpen?: () => void - Callback when connection opens
  • onClose?: () => void - Callback when connection closes

useZocket<TRouter>()

Hook to access the Zocket client and event listener.

Returns:

  • client: ZocketClient<TRouter> - The Zocket client instance for sending messages
  • useEvent: (event, handler) => void - Hook for type-safe event listening

useEvent(subscribeFn, handler)

Type-safe event listener hook (returned from useZocket).

Parameters:

  • subscribeFn: (callback) => UnsubscribeFn - Subscription function from client.on (e.g., client.on.posts.created)
  • handler: (payload) => void - Event handler (payload is fully typed and automatically inferred)

Features:

  • Automatically subscribes on mount
  • Automatically unsubscribes on unmount
  • Full TypeScript inference for event payloads based on router definition
  • Compile-time type safety - catches invalid event names and incorrect payload types

Features

  • Type Safety - End-to-end type safety from server to client
  • Auto Reconnection - Built-in reconnection with exponential backoff
  • Auto Cleanup - Event listeners are automatically cleaned up
  • React Idiomatic - Follows React patterns and best practices
  • Zero Config - Works out of the box with sensible defaults

License

MIT