A websocket Middleware
This middleware send and receive actions from a server, dispatching as needed
const FOO = 'FOO';
function Bar() {
return {
type: FOO,
payload: "Receive this"
socket: {
send: true
}
};
}
npm install react-redux-websocket
Then you create the socket and use ApplyMiddleware
import {createStore, applyMiddleware} from 'redux';
import reduxWebsocket from 'react-redux-websocket';
const socket = new WebSocket('ws://localhost:3030');
const store = createStore(rootReducer, applyMiddleware(reduxWebsocket(socket)));
Any action received through the websocket will be dispatched and handled normally, so your reducers should handle them.
To send actions through the socket you need the special object socket with the key send set to true
const FOO = 'FOO';
function Bar() {
return {
type: FOO,
payload: "Receive this"
socket: {
send: true
}
};
}
The socket object has some properties that you can use, currently those properties are: send, keepSocket and silent.
const FOO = 'FOO';
function Bar() {
return {
type: FOO,
socket: {
send: true, // The action will be send through the socket, default: false
keepSocket: false, // If false, the socket object will be removed after send, default: false
silent: false // If true, it will not raise erros if fail to send, default: false
}
};
}
run
npm test
react-redux-websocket is free and unencumbered software released into the public domain. See UNLICENSE for details.