1.0.4 • Published 4 years ago

connected-react-socket v1.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

Build Status npm version Coverage Status

connected-react-socket

Lightweight Provider for socket.io connection for react-redux application. You can see the demo.

Contents

Install

install it by

npm i --save connected-react-socket

or

yarn add  connected-react-socket

ConnectedSocket

Add socket middleware with passing socket client

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

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

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

SocketProvider

Create component and wrap it with HOC

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

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

Props

SocketProvider

PropDecriptionDefault
socketsocket.io instancenull
shouldReconnectTells provider to reconect to socket on mountfalse
shouldDisconnectTells provider to close connection on unmountfalse

ConnectedSocket

PropDecriptionDefault
socketsocket.io instancenull
shouldReconnectTells provider to reconect to socket on mountfalse
shouldDisconnectTells provider to close connection on unmountfalse

Passed props

withSocket

Passed propDecription
socketEventsocket.io event
emitfunction for emit socket message

injectSocket

Passed propDecription
socketsocket.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

1.0.4

4 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

1.0.2-alpha

5 years ago

1.0.0-alpha

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago