Reputation: 2415
I'm using the following plugin for managing my Socket IO instance in Redux:
https://github.com/itaylor/redux-socket.io
I need to be able to initialize a socket only once my user has logged in. The plugin suggests in the documentation to setup the socket instance when the store is setup:
import { createStore, applyMiddleware } from 'redux';
import createSocketIoMiddleware from 'redux-socket.io';
import io from 'socket.io-client';
let socket = io('http://localhost:3000');
let socketIoMiddleware = createSocketIoMiddleware(socket, "server/");
function reducer(state = {}, action){
switch(action.type){
case 'message':
return Object.assign({}, {message:action.data});
default:
return state;
}
}
let store = applyMiddleware(socketIoMiddleware)(createStore)(reducer);
store.subscribe(()=>{
console.log('new client state', store.getState());
});
store.dispatch({type:'server/hello', data:'Hello!'});
As I couldn't setup the instance at the same time my store was setup I needed to find a way to set it later on. I found this solution in the redux-socket.io issues:
https://github.com/itaylor/redux-socket.io/issues/13#issuecomment-256095866
Seemed like a perfect solution so I implemented it with my current store setup:
import {applyMiddleware,createStore} from 'redux';
import rootReducer from './reducers/___index';
import thunk from 'redux-thunk';
let socketIoMiddleware = null;
const proxyMiddleware = store => next => action => {
if (socketIoMiddleWare !== null) {
return socketIoMiddleware(store)(next)(action);
}
return next(action);
}
const middlewares = [proxyMiddleware, thunk];
const store = createStore(rootReducer,applyMiddleware(...middlewares));
The store renders fine with no errors but when I try to change anything in the stores state by clicking on a button for example I get the following error:
socketIoMiddleWare is not defined
The above solution I found in the redux-socket.io repo seems to have worked for others so i'm not entirely sure why i'm getting this error.
Any advice would be much appreciated!
Upvotes: 0
Views: 78
Reputation: 569
Here if(socketIoMiddleWare !== null) {
should be socketIoMiddleware
. Looks like a typo
Upvotes: 2