redux-via Socket.IO adapter
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
test
.babelrc
.editorconfig
.eslintrc
.gitattributes
.gitignore
.npmignore
README.md
package.json

README.md

redux-via-socket.io

redux-via Socket.IO adapter.

Usage

npm install --save redux-via-socket.io

Client side

import io from 'socket.io-client';
import {outClientViaSocketIO, inClientViaSocketIO} from 'redux-via-socket.io';
// ...
const socket = io('...'); // socket.io client initialization

const finalCreateStore = compose(
  applyMiddleware(
    outClientViaSocketIO(socket), // initialize for outcoming actions
    anotherMiddleware,
    yetAnotherMiddleware,
  )
)(createStore);

const store = finalCreateStore(rootReducer, initialState);

// initialize for incoming actions
inClientViaSocketIO(socket, store.dispatch);

Server side

import Server from 'socket.io';
import {outServerViaSocketIO, inServerViaSocketIO} from 'redux-via-socket.io';
// ...
const server = new Server({...}); // socket.io server initialization

const finalCreateStore = compose(
  applyMiddleware(
    outServerViaSocketIO(server), // initialize for outcoming actions
    anotherMiddleware,
    yetAnotherMiddleware,
  )
)(createStore);

const store = finalCreateStore(rootReducer, initialState);

// initialize for incoming actions
inServerViaSocketIO(server, store.dispatch);

You can tweak the incoming actions before dispatch them using socket metadata. For example, if you're using something like passport.socketio:

inServerViaSocketIO(server, (action, socket) => {
  action.meta.user = socket.request.user;
  store.dispatch(action);
});

You can also filter outcoming actions that are broadcasted. For example, send actions only to all current user connections, except specified client:

outServerViaSocketIO(server, (action, socket) =>
  action.meta.user.email === socket.user.email &&
  action.meta.client !== socket.id
)

Meta options

Action flow is controlled by options specified in the meta property.


License

MIT