question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't find the way to integrate socket.io and redux-socket.io

See original GitHub issue

Hello, I can’t find the way to establish a communication with a socket.io port… it works on the build but i have some kind of conflict with Browser-sync, i tried different configurations, but it doesn’t establish the connection. In some cases i receive a 404 error page from express.

Here is the code, please help me on how to revolve this issue or how te remove or deactivate browser-sync, is not critic for my project. But when i tried to remove the dev version stop to works, I don’t know how to replace Browser-sync, to keep the dev environment with HMR operational.

Please I’ll appreciate any orientation, I’ve invested a lot of time in this issue and it surpasses me.

CLIENT SIDE // /src/store/configureStore.js

import createSocketIoMiddleware from 'redux-socket.io';
import { createSocket, registerListenersSocket } from '../lib/functions';

...

let socketIoMiddleware, socket;
if (process.env.BROWSER) {
  socket = createSocket();
  console.log('socket', socket);
  const socketDebug = (action, emit, next, dispatch ) => {
    console.log('socket middleware: ', { action, emit, next, dispatch });
    next();
  };
  socketIoMiddleware = createSocketIoMiddleware(socket, 'server/', socketDebug);
}

export default function configureStore(initialState, helpersConfig) {
...
  const middleware = [];
  const sagaMiddleware = createSagaMiddleware();
  middleware.push(sagaMiddleware);

  if (process.env.BROWSER) {
    console.log('adding socket middleware');
    middleware.push(socketIoMiddleware);
  }


  let enhancer;
  if (__DEV__) {
    middleware.push(createLogger());
    // https://github.com/zalmoxisus/redux-devtools-extension#redux-devtools-extension
    let devToolsExtension = f => f;
    if (process.env.BROWSER && window.devToolsExtension) {
      devToolsExtension = window.devToolsExtension();
    }

    enhancer = compose(applyMiddleware(...middleware), devToolsExtension);
  } else {
    enhancer = applyMiddleware(...middleware);
  }

  // See https://github.com/rackt/redux/releases/tag/v3.1.0
  const store = createStore(rootReducer, initialState, enhancer);
  if (process.env.BROWSER) {
    registerListenersSocket(socket, store);
  }
  // Hot reload reducers (requires Webpack or Browserify HMR to be enabled)
  if (__DEV__ && module.hot) {
    module.hot.accept('../reducers', () =>
      // eslint-disable-next-line global-require
      store.replaceReducer(require('../reducers').default),
    );
  }
  store.runSaga = sagaMiddleware.run;
  store.close = () => store.dispatch(END);

  return store;
}

/src/lib/functions.js

import io from 'socket.io-client';

import {
  socketConnect,
  socketReconnect,
  socketDisconnect,
  socketConnectionError,
} from '../actions/server';

export const calcProgress = (dateStr, limit) => {
  const date = new Date(dateStr);
  const now = new Date(Date.now());
  const days = Math.floor((Date.now() - date) / (3600000 * 24));
  return {
    days,
    percent: Math.round(Math.min(100, days / limit * 100)),
  };
};

export function createSocket() {

  // todo: access configuration by conf.
  const serverURL = process.env.SOCKET_SERVER;
  console.log('connecting socket on: ');
  return io();
}

export function registerListenersSocket(socket, store) {
  socket.on('connect', () => store.dispatch(socketConnect()));
  socket.on('connect_timeout', () => store.dispatch(socketConnectionError()));
  socket.on('connect_error', () =>
    store.dispatch(
      socketConnectionError(new Error('Error al conectar el socket.')),
    ),
  );
  socket.on('reconnect_error', () =>
    store.dispatch(
      socketConnectionError(new Error('Error al reconectar el socket.')),
    ),
  );
  socket.on('disconnect', () => store.dispatch(socketDisconnect()));
  socket.on('reconnect', () => store.dispatch(socketReconnect()));
}

image

SERVER SIDE /src/server.js

...
import socket from './lib/socket';

const app = express();
app.server = http.createServer(app);
socket(app, process);
...

/src/lib/socket

import SocketIO from 'socket.io';
...
export default function socketConnection (app, process) {
...

  /** SOCKET CONNECTION */
  console.log('** socket connection');
  const { server } = app;
  const sockets = {};
  const io = new SocketIO(server);
  io.on('connection', socket => {
    console.log('socket connected'); /** <-- I never get this **/
    sockets[socket.id] = socket; // socket table.
    console.log('sending hello acknowledge');
    socket.emit('action', {type: 'server/HELLO', payload: {message: 'socket connected', code: 1} });

    socket.on('action', (action) => {
      console.log('socket action:', action);
      switch (action.type) {
        case 'server/CALL': {
          const {source, target, leadId, agentId} = action.payload;
          console.log('call');
          console.log('source', source);
          console.log('target', target);
          crmami.call(source, target, {
            socketId: socket.id,
            leadId,
            agentId
          },
            (error) => {
              socket.emit('action', {
                type: 'server/CALL_FAIL',
                payload: { error }
              })
            });
        }
      }
    });


    socket.on('disconnect', () => {
      delete sockets[socket.id];
    });

  });

  process.on('exit', () => {
    crmami.disconnect();
    crmami.destroy();
  })
}

/tools/start.js

...
  await new Promise((resolve, reject) =>
    browserSync.create().init(
      {
        socket: {
          path: '/bs/socket.io',
          clientPath: '/browser-sync',
          namespace: '/bs',
          domain: undefined,
          port: undefined,
          'clients.heartbeatTimeout': 5000
        },
  //       // socket: {
  //       //   namespace: `http://localhost:3000/bs`
  //       // },
        server: 'src/server.js',
        port: config.port,
        middleware: [server],
        open: !process.argv.includes('--silent'),
        ...(isDebug ? {} : { notify: false, ui: false }),
      },
      (error, bs) => (error ? reject(error) : resolve(server)),
    )
  );

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:17 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Shubh1692commented, Jan 28, 2019

@alvaroqt, Hey you can integrate Socket IO server for prod build by replacing this code in src/server.js

if (!module.hot) {
  promise.then(() => {
    app.listen(config.port, () => {
      console.info(`The server is running at http://localhost:${config.port}/`);
    });
  });
}

with this

if (!module.hot) {
  promise.then(() => {
      const mainServer = app.listen(config.port, () => {
        console.info(`The server is running at http://localhost:${config.port}/`);
      });
      const io = require('socket.io').listen(mainServer);
      io.on('connection', function(socket){
             socket.on('chat message', function(msg){
             io.emit('chat message', msg);
      });
   });
 });
} 
1reaction
tim-softcommented, Jan 11, 2018

Hypothetically speaking, I’d try to make a socket.js file under src, add a socket section in the webpack config and then mimic the server creation steps in /tools/start.js for the socket server. The socket should be it’s own entity just like the current server and in a production build, be separable so you could slap it on it’s own machine. Then adding in the ws apollo link is pretty self explanatory

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Redux + Socket.IO + Create React App - Stack Overflow
I find a lot of the pre-configurations in create react app to be very helpful; jest, hot loading, build outputs. As you know...
Read more >
Client Initialization | Socket.IO
Once you have installed the Socket.IO client library, you can now init the client. The complete list of options can be found below....
Read more >
Creating a Real Time Chat App using React and Socket IO ...
This tutorial will give readers a detailed guide on how to implement a real-time chat application using React and Socket.io with end-to-end ...
Read more >
How to build a real time chat app in React (with node, socket ...
Type this into the command line. npm install --save @material-ui/core chance concurrently cors express nodemon redux socket.io socket.io-client react-reduxnpm.
Read more >
Socket.IO, React and Node.js: Going Real-Time
The application calls also the index route: even if the server won't serve any HTML content we will need a very simple route...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found