Simple redux websocket middleware for browser or node
npm install --save simple-redux-websocket-middleware
For an example of how to use this middleware in node or the browser, see this project.
import createWebSocketMiddleware from 'simple-redux-websocket-middleware';
import { createStore, applyMiddleware } from 'redux';
let store = createStore(
reducer,
{},
applyMiddleware(createWebSocketMiddleware())
);
store.dispatch({
type: 'WEBSOCKET/REQUEST/OPEN',
payload: {
id: 'coinbase', // you pick an id per connection
url: 'wss://ws-feed.exchange.coinbase.com'
}
});
store.dispatch({
type: 'WEBSOCKET/REQUEST/OPEN',
payload: {
id: 'gemini',
url: 'wss://api.gemini.com/v1/marketdata/btcusd'
}
});
function reduce(state, action) {
if (action.type == 'WEBSOCKET/EVENT/MESSAGE') {
if (action.payload.id == 'gemini') {
return reduceGemini(state, action);
} else if (action.payload.id == 'coinbase') {
return reduceCoinbase(state, action);
}
} else {
return state;
}
}
MIT