Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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)