# connected-react-socket

> React HOC for adding socket.io in react-redux application.

Latest version **1.0.4** (published 2020-06-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install connected-react-socket
pnpm add connected-react-socket
yarn add connected-react-socket
bun add connected-react-socket
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2020-06-14 |
| First published | 2018-12-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 51.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Chafilin |
| Maintainers | chafilin |
| Keywords | react, socket.io, redux |

## Links

- npm: https://www.npmjs.com/package/connected-react-socket
- Repository: https://github.com/chafilin/connected-react-socket
- Homepage: https://github.com/chafilin/connected-react-socket#readme
- Issues: https://github.com/chafilin/connected-react-socket/issues
- npm.io page: https://npm.io/package/connected-react-socket

## 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

- 1.0.4 (latest) — 2020-06-14
- 1.0.2 — 2019-09-12
- 1.0.1 — 2019-02-18
- 1.0.0 — 2019-02-16
- 1.0.2-alpha — 2019-02-16
- 1.0.0-alpha — 2019-02-16
- 0.2.1 — 2019-01-20
- 0.2.0 — 2019-01-20
- 0.1.4 — 2019-01-16
- 0.1.3 — 2018-12-29
- 0.1.2 — 2018-12-20

## README

[![Build Status](https://travis-ci.org/chafilin/connected-react-socket.svg?branch=master)](https://travis-ci.org/chafilin/connected-react-socket)
[![npm version](https://badge.fury.io/js/connected-react-socket.svg)](https://badge.fury.io/js/connected-react-socket)
[![Coverage Status](https://coveralls.io/repos/github/chafilin/connected-react-socket/badge.svg)](https://coveralls.io/github/chafilin/connected-react-socket)

# connected-react-socket

Lightweight Provider for socket.io connection for react-redux application. You can see the [demo](https://connected-react-socket-demo.herokuapp.com/).

## Contents

- [Install](#install)
- [ConnectedSocket](#connectedsocket)
- [SocketProvider](#socketprovider)
- [Props](#props)
- [Passed props](#passed-props)
- [Redux connection](#redux-connection)
- [Feedback](#feedback)

## Install

install it by

```
npm i --save connected-react-socket
```

or

```
yarn add  connected-react-socket
```

## ConnectedSocket

Add socket middleware with passing socket client

```js
import { createStore, combineReducers, applyMiddleware, compose } from "redux";
import io from "socket.io-client";
import { socketReducer, socketMiddleware } from "connected-react-socket";

export const socket = io();

const reducers = combineReducers({
  socket: socketReducer
});

const store = createStore(
  reducers,
  compose(applyMiddleware(socketMiddleware(socket)))
);

export default store;
```

Then wrap componet by `withSocket` HOC and use `socketEvent` prop

```jsx
const Display = props => {
  const { socketEvent } = props;
  return (
    <div>
      // Will print socket event name
      <div>{socketEvent.type}</div>
      // Let's print recieved messages
      <div>{socketEvent && socketEvent.data.join(",")}</div>
    </div>
  );
};
const mapStateToProps = state => {
  return {
    socketEvent: state.socket
  };
};
const mapDispatchToProps = dispatch => {
  return {
    emit: (type, ...message) => dispatch(emitMessage(type, message))
  };
};
const ConnectedDisplay = connect(
  mapStateToProps,
  mapDispatchToProps
)(Display);
```

Last one: dd socket provider under the redux provider

```xml
...
<ReduxProvider store="{store}">
  <ConnectedSocket socket="{socket}"> <ConnectedDisplay /> </ConnectedSocket>
</ReduxProvider>
...
```

## SocketProvider

Create component and wrap it with HOC

```jsx
const Display = props => {
  const { socketEvent } = props;
  return (
    <div>
      // Will print socket event name
      <div>{socketEvent.type}</div>
      // Let's print recieved messages
      <div>{socketEvent && socketEvent.data.join(",")}</div>
    </div>
  );
};

const DisplayEvent = withSocket(Display);
```

And add socket provider

```xml
<SocketProvider socket={socket}>
  <DisplayEvent name="WithSocket" />
</SocketProvider>
```

## Props

### SocketProvider

| Prop             |                  Decription                   | Default |
| ---------------- | :-------------------------------------------: | ------- |
| socket           |              socket.io instance               | null    |
| shouldReconnect  | Tells provider to reconect to socket on mount | false   |
| shouldDisconnect | Tells provider to close connection on unmount | false   |

### ConnectedSocket

| Prop             |                  Decription                   | Default |
| ---------------- | :-------------------------------------------: | ------- |
| socket           |              socket.io instance               | null    |
| shouldReconnect  | Tells provider to reconect to socket on mount | false   |
| shouldDisconnect | Tells provider to close connection on unmount | false   |

## Passed props

### withSocket

| Passed prop |            Decription            |
| ----------- | :------------------------------: |
| socketEvent |         socket.io event          |
| emit        | function for emit socket message |

### injectSocket

| Passed prop |            Decription            |
| ----------- | :------------------------------: |
| socket      | socket.io instance from provider |

## Redux connection

### socketReducer

Typical reducer with state and action. Let `ConnectedProvider` work with store.

### emitMessage

Action to pass message to socket if using `ConnectedProvider`.

`emitMessage(type, message)` where `type: string` and `message:array`

### socketMiddleware

Middleware for linking socket with store.

Apply `socketMiddleware(socket)` to store. Where `socket` is your socket.io instance

# Feedback

Feel free to create an issue or write me to chafiliny@gmail.com

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