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

@gnani.ai/web-voice-hook

v1.0.5

Published

React hook for real-time bidirectional WebSocket voice (STT/TTS)

Readme

Web Voice Hook by Gnani.AI

A React hook for real-time bidirectional WebSocket voice: capture microphone → server (STT), play TTS ← server. Published as @gnani.ai/web-voice-hook.

We welcome community contributions — edge cases, cleaner code, and improvements. See Contributing below.


Installation

In your application project:

pnpm add @gnani.ai/web-voice-hook

Or with npm:

npm install @gnani.ai/web-voice-hook

Quick start

1. Use the hook (no extra setup)

The Audio Worklet is embedded in the package and loaded at runtime via a Blob URL. You don't need to copy or host any worklet file — just use the hook with the default options. If you prefer to serve the worklet yourself (e.g. custom path or base path), copy src/audio-processor.js from this repo into your app's public folder and pass workletPath in the hook options.

2. Use the hook

Option A — Direct use (you build the WebSocket URL yourself):

import { useWebSocketAudio as useWebSocketAudioHook } from '@gnani.ai/web-voice-hook';
import type { IUseWebVoiceOptions } from '@gnani.ai/web-voice-hook';

const {
  isConnected,
  isPlaying,
  connect,
  disconnect,
  reconnect,
  startRecording,
  stopRecording,
  isRecording,
} = useWebSocketAudioHook({
  websocketUrl: 'wss://your-api/voice',
  visualizerOptions: { elementId: 'visualizer-canvas', color: '#9E77ED' },
  events: {
    onOpen: () => console.log('Connected'),
    onClose: (source) => console.log('Closed', source),
    onException: (err) => console.error(err),
  },
  logger: { info: console.log, error: console.error },
} as IUseWebVoiceOptions);

Sample Screenshot

Screenshot or diagram

Option B — Wrapper hook (build URL from app config/auth and pass through):

import { useWebSocketAudio as useWebSocketAudioHook } from '@gnani.ai/web-voice-hook';
import type { IUseWebVoiceOptions } from '@gnani.ai/web-voice-hook';
import { useMemo } from 'react';
import { v4 } from 'uuid';

// Example: your app builds websocketUrl from agentId, auth, etc.
export const useWebSocketAudio = ({
  agentId,
  onClose,
  onConversationIdGenerated,
  visualizerColor,
  accessToken
}) => {
  const callId = useMemo(() => v4(), []);

  const websocketUrl = useMemo(() => {
    if (!agentId) return undefined;
    return buildVoiceWebSocketUrl(agentId, callId, accessToken);
  }, [agentId, callId, accessToken]);

  onConversationIdGenerated?.(callId);

  const hook = useWebSocketAudioHook({
    websocketUrl,
    conversationId: callId,
    visualizerOptions: { elementId: 'visualizer-canvas', color: visualizerColor },
    events: {
      onOpen: () => console.log('WebSocket connected'),
      onClose: (source) => {
        console.log('WebSocket closed', source);
        onClose?.(source);
      },
      onException: (error) => {
        console.error('WebSocket error', error);
        Sentry.captureException(error); // optional
      },
    },
    logger: { info: console.log, error: console.error },
  } as IUseWebVoiceOptions);

  return {
    ...hook,
    conversationId: callId,
  };
};

Then in your UI, call your wrapper (e.g. useWebSocketAudio({ variant, agentId, onClose, ... })) and use connect, disconnect, startRecording, stopRecording, etc.


API

Hook: useWebSocketAudio(options)

Options (IUseWebVoiceOptions):

| Option | Type | Required | Description | |--------|------|----------|-------------| | websocketUrl | string | ✅ | WebSocket URL (e.g. wss://api.example.com/voice). | | conversationId | string | No | For your app; not sent by the hook. | | visualizerOptions | object | No | elementId, color, options. | | events | object | No | onOpen, onClose, onException. | | logger | { info, error } | No | Logging; defaults to console. |

Returns: isConnected, isPlaying, connect, disconnect, reconnect, startRecording, stopRecording, isRecording.


WebSocket protocol (reference)

Client → server: start, media (base64 µ-law), TTS_PLAYING, EOC.
Server → client: media (base64 TTS), barge/BARGE, EOC, stop.


Pushing changes (release steps)

When publishing a new patch version of this package, from the repo root run:

pnpm run release:patch

This script: builds the package, stages all changes, commits with message build: v<current-version> (or skips commit if nothing to commit), bumps the patch version, then pushes commits and tags.

For a minor or major bump, run the steps manually and use pnpm version minor or pnpm version major instead of pnpm version patch before pushing.

Consumers can then install the new tag, e.g. #v1.0.2.


Troubleshooting

  • "Failed to load worklet" — By default the worklet is embedded (no hosting). If you pass a custom workletPath, ensure that URL serves the worklet script (e.g. copy src/audio-processor.js from this repo to your public folder).
  • No sound / no mic — Check mic permissions; use events.onException and logger.
  • Visualizer not showing — Set visualizerOptions.elementId to a mounted element id; install audiomotion-analyzer.

Contributing

We welcome the community to improve this code: handle edge cases, simplify logic, and improve documentation.

  • How to contribute: See CONTRIBUTING.md for development setup (pnpm install, pnpm run build, pnpm run typecheck), what we’re looking for (bugs, edge cases, cleanup, tests), and PR guidelines.
  • Quick start: Clone the repo, run pnpm install and pnpm run build, then open a pull request or issue.

TypeScript

import type { IUseWebVoiceOptions, IWebVoiceLogger, ISocketEventData, ISocketMessage } from '@gnani.ai/web-voice-hook';