redux-socket.io-middleware
Another dead-simple middleware which allows you to connect Redux and Socket.io.
It uses meta
property of your action object to recognize whether send it to backend, or not.
Idea
- Whenever you want to send an event from Client to your Backend through socket.io,
use
store.dispatch
with regularaction
withmeta
object (see example actionCreator.js) - Whenever you want to send and handle an event from your Backend to
Client - emit it on backend with socket.io in a form of regular
action
and it will be handled by forwarding it tostore.dispatch()
- your reducers should take care of the rest.
Important side note - action object send thorough Socket.io must be standard javascript object literal. It can't be a function (so unfortunately thunk drops out).
Usage
Installation
npm install --save redux-socket.io-middleware
Client side
store.js
; const reducers = ...; const io = socketIO; const store = ;
actionCreator.js
{ return type: "ADD_USER_REQUEST" meta: remote: true user } { return type: "ADD_USER_REQUEST_SUCCESS" } { return type: "ADD_USER_REQUEST_FAILURE" }
And then you dispatch this type of action like any other:
yourReactComponentContainer.js
const mapDispatchToProps = { return }
Server side
If we share actionCreator.js between client and server we can youse it like below. Otherwise, just create action by hand.
userController.js
io;
Configuration
You can configure the name of channel which will be used for sending websocket messages.
Default value is action
. To change it, simply add second parameter when registering middleware:
const store = ;
and then on your backend side, listen on that string:
io;
Yeah, I know that this readme is much longer than a code itself :)