Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lxsmnsyc/react-use-websockets
React Hooks + WebSockets API
https://github.com/lxsmnsyc/react-use-websockets
react react-hooks websockets
Last synced: 3 months ago
JSON representation
React Hooks + WebSockets API
- Host: GitHub
- URL: https://github.com/lxsmnsyc/react-use-websockets
- Owner: lxsmnsyc
- Created: 2019-06-14T06:10:36.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-11T13:27:14.000Z (about 2 years ago)
- Last Synced: 2024-10-06T11:16:17.891Z (3 months ago)
- Topics: react, react-hooks, websockets
- Language: TypeScript
- Size: 3.17 MB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @lxsmnsyc/react-use-websockets
> React Hooks + WebSockets
[![NPM](https://img.shields.io/npm/v/@lxsmnsyc/react-use-websockets.svg)](https://www.npmjs.com/package/@lxsmnsyc/react-use-websockets) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
## Install
```bash
npm install --save @lxsmnsyc/react-use-websockets
``````bash
yarn add @lxsmnsyc/react-use-websockets
```## Usage
```tsx
import React from 'react';import {
useWebSocketMessage,
useWebSocketError,
useWebSocketClose,
useWebSocketOpen,
} from '@lxsmnsyc/react-use-websockets';const wss = new WebSocket('ws://localhost:8080');
function App() {
const onMessageData = useWebSocketMessage(wss);
const onErrorData = useWebSocketError(wss);
const onCloseData = useWebSocketClose(wss);
const onOpenData = useWebSocketOpen(wss);if (onErrorData) {
return (
An error occured.
);
}
if (onCloseData) {
return (
Connection closed.
);
}
if (onMessageData) {
return (
Message received : { onMessageData }
);
}
if (onOpenData) {
return (
Connection established.
);
}
return (
Waiting for a connection.
);
}export default App;
```## License
MIT © [lxsmnsyc](https://github.com/lxsmnsyc)