# @lawcket/react-websocket

> A slim React websocket implementation

Latest version **0.2.2** (published 2019-05-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @lawcket/react-websocket
pnpm add @lawcket/react-websocket
yarn add @lawcket/react-websocket
bun add @lawcket/react-websocket
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2019-05-04 |
| First published | 2019-04-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Chris Sullivan |
| Maintainers | icarus-sullivan |
| Keywords | react, websocket, react-websocket, socket |

## Links

- npm: https://www.npmjs.com/package/@lawcket/react-websocket
- Repository: https://github.com/icarus-sullivan/react-websocket
- Homepage: https://github.com/icarus-sullivan/react-websocket#readme
- Issues: https://github.com/icarus-sullivan/react-websocket/issues
- npm.io page: https://npm.io/package/@lawcket/react-websocket

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.2.2 (latest) — 2019-05-04
- 0.2.1 — 2019-05-04
- 0.2.0 — 2019-05-04
- 0.1.3 — 2019-04-30
- 0.1.2 — 2019-04-30
- 0.1.1 — 2019-04-29
- 0.1.0 — 2019-04-28

## README

![npm downloads total](https://img.shields.io/npm/dt/@lawcket/react-websocket.svg) ![npm version](https://img.shields.io/npm/v/@lawcket/react-websocket.svg) ![npm license](https://img.shields.io/npm/l/@lawcket/react-websocket.svg)

# @lawcket/react-websocket
A slim Websocket implementation for React 16+. The libray uses the standard browser WebSocket so 'should' be supported in React-Native.

### Installation
```
npm install --save @lawcket/react-websocket
```
or
```
yarn add @lawcket/react-websocket
```

### Usage
Example:
```
import React, { useState } from 'react';
import Websocket from '@lawcket/react-websocket';

const Messages = () => {
  const [messages, addMessage] = useState([]);
  return (
    <Websocket 
      url='wss://...' 
      retry
      onOpen={({ send }) => console.log('open')}
      onClose={({ code, reason }) => console.log('closed', code, reason)}
      onMessage={({ send, data }) => console.log('message', data)}
      onError={(error) => console.log('error', error)} >
      {({ send, close }) => {
        return (
            <pre>{JSON.stringify(messages, null, 2)}</pre>
        );
      }}
    </Websocket>
  );
}
```

### PropTypes
|Name|Type|Description | Default|
|-----|-----|-----|---|
| url | string | The websocket url to connect to | |
| protocol | string | The protocol for the websocket to use | |
| retry | boolean | If an error occurs, should it reconnect | true | 
| onOpen | function | Notifies when the connection has been opened | |
| onMessage | function | Notifies when an incoming message has been received | |
| onClose | function | Notifies the connection was closed | |
| onError | function | Notifies of an error with the connection, will close the connection | |

### onOpen
|Parameter|Type|Description |
|-----|-----|-----|
| send | function(any) | A function to send data to the server | 

### onMessage
|Parameter|Type|Description |
|-----|-----|-----|
| data | any | Incoming message (usually stringified JSON data) |
| send | function(any) | A function to send data to the server | 
    
### onError
|Parameter|Type|Description |
|-----|-----|-----|
| error | Error | The error that occured during connection or usage |


### onClose

Reference: [Close codes & reasons](https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent)

|Parameter|Type|Description |
|-----|-----|-----|
| code | number | The close code sent by the server |
| reason | string | The reason the connection was closed |

---
_Source: https://npm.io/package/@lawcket/react-websocket · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
