A custom React hook to communicate over WebSockets.
To install the react-ws-hook package you first need to get an access token and add it to your .npmrc to use the GitHub npm registry following these instructions.
Then you can install the package using npm or yarn:
npm install @pearces/react-ws-hook@latestOr:
yarn add @pearces/react-ws-hook@latestThis package has the following dependencies: React 18.2.0 or later.
First import the hook:
import React from 'react';
import useWebsocket from '@pearces/react-ws-hook';Then reference it in your component:
const { send, received, readyState } = useWebsocket('ws://localhost:8080');You can then use the send function to send messages to the server, and the received and readyState variables to read the last message received and the current state of the WebSocket connection.
The WebSocket ws or wss URL of the server you want to connect to.
An optional object containing the following properties:
reconnect- A boolean value to determine if the hook should attempt to reconnect to the server if the connect is lost. Default istrue.reconnectWait- The interval in milliseconds to wait before attempting to reconnect to the server. Default is 2000.reconnectAttempts- The number of times to attempt to reconnect to the server. Default isInfinity.- retrySend - A boolean value to determine if the hook should attempt to resend messages that were sent while the WebSocket connection was not open. Default is
true. onOpen- A handler function that is called when the WebSocket connection is opened. It is passed the openeventobject.onClose- A handler function handler that is called when the WebSocket connection is closed. It is passed the closeeventobject.onError- A handler function that is called when an error occurs with the WebSocket connection. It is passed the erroreventobject.onMessage- A handler function that is called when a message is received from the server. It is passed the messageeventthat contains anEventorMessageEventobject.onSend- A handler function that is called when a message is sent to the server. It is passed amessagethat is astring,ArrayBuffer,ArrayBufferView,Blob, orFormDataobject.logger- An object that is called with log messages withwarnanderrormethods. Defaults toconsole.
The result of the useWebsocket hook is an object containing the following properties:
send- A function that sends a message to the server. It takes a singlestringargument that is the message to send.received- The last message received from the server. It isnullif no message has been received, otherwise it is astring,ArrayBuffer,ArrayBufferView,Blob, orFormDataobject.readyState- The current state of the WebSocket connection. It is one of the following values:CONNECTING,OPEN,CLOSING, orCLOSED.url- The URL of the WebSocket server for the current connection.
Contributions to this project are welcome! Here are the steps to get started:
- Fork the repository.
- Clone your fork.
- Make your changes.
- Run the tests with
npm run testto make sure they pass. - Submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.