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

@soundxyz/graphql-react-ws

v3.0.1

Published

[![NPM Version](https://img.shields.io/npm/v/%40soundxyz%2Fgraphql-react-ws)](https://www.npmjs.com/package/@soundxyz/graphql-react-ws)

Readme

GraphQLReactWS – React GraphQL Subscriptions with graphql-ws and Valtio

NPM Version

A React utility for managing GraphQL subscriptions over WebSockets, with Valtio-powered state and effect hooks.


Table of Contents


Example Usage

Feel free to check out full end-to-end example in examples/next/src/pages/subscription.tsx


Setup

import { GraphQLReactWS } from './GraphQLReactWS';
import { createClient } from 'graphql-ws';

const wsClientOptions = {
  url: 'wss://your-graphql-endpoint/graphql',
  // ...other options
};

const graphql = GraphQLReactWS({ graphqlWsOptions: wsClientOptions });

API

GraphQLReactWS

const graphql = GraphQLReactWS({ graphqlWsOptions });
  • Parameters:
    • graphqlWsOptions: Options for the graphql-ws client.
  • Returns: An object with the following properties:
    • client graphql-ws client
    • subscribe subscribe to a GraphQL subscription
    • useSubscription React hook to subscribe to a GraphQL subscription
    • subscriptionStores Map of subscription stores
    • setSubscriptionData Manually set the data for a subscription store
    • getSubscriptionStore Get a subscription store
    • Effects Register global effects for when data arrives or a subscription completes

useSubscription

A React hook to subscribe to a GraphQL subscription and reactively receive data and errors.

const { data, error, store } = graphql.useSubscription({
  query: MySubscriptionDocument,
  variables: { id: '123' }, // optional if your subscription has no variables
  onData: result => {
    /* handle new data */
  },
  onError: error => {
    /* handle errors */
  },
  enabled: true, // optional, default true
});
  • Parameters:

    • query: The GraphQL subscription document.
    • variables: (optional) Variables for the subscription.
    • onData: (optional) Callback for new data.
    • onError: (optional) Callback for errors.
    • initialData: (optional) Initial data for the store.
    • enabled: (optional) Whether the subscription is active.
  • Returns:

    • data: Latest subscription data.
    • error: Latest error, if any.
    • store: The underlying Valtio store.

setSubscriptionData

Manually set the data for a subscription store.

graphql.setSubscriptionData(
  { query: MySubscriptionDocument, variables: { id: '123' } },
  { data: { ... } }
);
  • Parameters:
    • query: The GraphQL subscription document.
    • variables: (optional) Variables for the subscription.
    • data: The new data to set.

Effects

Register global effects for when data arrives or a subscription completes.

Effects.onData

Register a callback to be called every time the specified operation receives data.

const remove = Effects.onData(MySubscriptionDocument, ({ operation, result, variables }) => {
  // Do something with result.data
});
  • Returns: A function to remove the effect.

Effects.onComplete

Register a callback to be called when the specified operation completes or is stopped.

const remove = Effects.onComplete(MySubscriptionDocument, ({ operation, variables }) => {
  // Do something on completion
});
  • Returns: A function to remove the effect.

Examples

Basic Usage

const { data, error } = useSubscription({
  query: MySubscriptionDocument,
  variables: { id: 'abc' },
  onData: result => {
    console.log('New data:', result.data);
  },
  onError: err => {
    console.error('Subscription error:', err.errors);
  },
});

Global Effects

const removeEffect = Effects.onData(MySubscriptionDocument, ({ result }) => {
  console.log('Received data:', result.data);
});

// Later, to remove the effect:
removeEffect();

Manually Setting Data

graphql.setSubscriptionData(
  { query: MySubscriptionDocument, variables: { id: 'abc' } },
  { data: { myField: 'newValue' } },
);