https://github.com/chadly/react-socket-hooks
A set of react hooks to work real nice with the WebSocket API
https://github.com/chadly/react-socket-hooks
hooks react websocket
Last synced: 4 months ago
JSON representation
A set of react hooks to work real nice with the WebSocket API
- Host: GitHub
- URL: https://github.com/chadly/react-socket-hooks
- Owner: chadly
- License: mit
- Created: 2019-04-05T16:05:53.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T19:14:59.000Z (over 3 years ago)
- Last Synced: 2025-08-21T08:39:26.808Z (10 months ago)
- Topics: hooks, react, websocket
- Language: JavaScript
- Size: 1.14 MB
- Stars: 4
- Watchers: 8
- Forks: 0
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# React Socket Hooks
> A set of react hooks to work real nice with the WebSocket API.
```js
import { useCallback } from "react";
import useSocket from "react-socket-hooks";
const mySocketMessageHandler1 = useCallback(msg => console.log(msg), []);
const mySocketMessageHandler2 = useCallback(msg => console.log("ohmy", msg), []);
const { useMessageHandler, send } = useSocket("wss://example.com");
useMessageHandler(mySocketMessageHandler1);
useMessageHandler(mySocketMessageHandler2);
// …later, perhaps in response to a user action
send("hello, server");
```
Install with [Yarn](https://yarnpkg.com/en/):
```shell
yarn add react-socket-hooks
```
or if Yarn isn't your thing:
```shell
npm install react-socket-hooks --save
```
This library makes use of and assumes the [`WebSocket` API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) is available on the global scope. You will need to polyfill it if it is not available in the current environment.
## Build/Run Locally
After cloning this repo, run:
```shell
yarn
yarn lint
yarn test
yarn compile
```