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-socket-store

v0.0.4

Published

It is for easily using Websocket in React.It inspired by React-redux.

Readme

react-socket-store

npm version

It is for easily using Websocket in React. It inspired by React-redux. It is provider of socket-store.

Quick Start

1. Install

#npm

npm install react-socket-store

#yarn

yarn add react-socket-store

2. Create MessageHandler(s), and Socket Store

MesssageHandler and Socket store is based on socket-store.

2-1. Create MessageHandler

First, create a message handler. Define the topic, callback for the topic, and default status. This will be provided in the store.

  • createMessageHandler(key, callback, state)
    • key : it will be subject of message.
    • callback: it will works like reducer. it must return state!
    • state: it is defualt state.
const talkHandler = createMessageHandler<string[], string>(
  "talk",
  (state, data) => {
    return [...state, data];
  },
  []
);

2-2. Create SocketStore

Next, create a socket store. Store gets two or three parameters for web sockets and message handlers.

  1. WebSocket instance,
  2. array of message handler,
  3. options options has callbacks about connection status.
  • createSocketStore(ws: WebSocket, messageHandlers: MessageHandler[], options?: SocketStoreOptions)
const socketStore = createSocketStore(
  new WebSocket("ws://localhost:3000"),
  [talkHandler],
  {
    onOpen: () => console.log("open"),
    onClose: () => console.log("close"),
    onError: () => console.log("error"),
  }
);

2-3. Provider

  • Wrap your <App> with <SocketProvider>, and provide a previously created store as a prop for the socket provider.
import { SocketProvider } from "react-socket-store";
import store from "./store";

const Index = (prop: Props) => {
  return (
    <SocketProvider store={store}>
      <App />
    </SocketProvider>
  );
};

3. Use SocketStore with Hook

we supply API for using SocketStore, by hooks.

3-1. useSocket

useSocket gets the parameter for the key of the MessageHandler, and returns the state, and sendfunction for the key.

const Component = (props: ComponentsProps)=>{
  const [value, setValue] = useState('');
  const [state, send] = useSocket('talk');

  const onChange = (e)=>{
    setValue(e.target.value);
  }

  const submit = (e)=>{
    e.preventDefault();
    send(value);
  }

  return (
    <>
    <div>
    {state.map(message)=> <span>{message}</span>}
    </div>
    <form onSubmit={submit}>
      <input value={value} onChange={onChange} />
    </form>
    </>

  )
}

3-2. useSend

useSend gets the paramerter for the key of the MessageHandler, and returns only sendfunction for the key.

const Component = (props: ComponentsProps) => {
  const [value, setValue] = useState("");
  const [send] = useSocket("talk");

  const onChange = (e) => {
    setValue(e.target.value);
  };

  const submit = (e) => {
    e.preventDefault();
    send(value);
  };

  return (
    <>
      <form onSubmit={submit}>
        <input value={value} onChange={onChange} />
      </form>
    </>
  );
};

3-3. useListen

useSend gets the paramerter for the key of the MessageHandler, and returns only state for the key.

const Component = (props: ComponentsProps) => {
  const [state] = useSend("talk");

  return(
    <div>
      {state.map((message)=> <span>{message}</span>)}
    </div>
  )
};

Contributors 👏🏻

LICENSE