2

I would like to use socket.io to keep track of daily active users of an application. My socket connection looks like this:

let visitorData = {};

io.on('connection', (socket) => {
    socket.on('user', (data) => {
        visitorData[socket.id] = data;
    })

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

I would like to be able to incorporate existing Node/Express routes and the data received to socket connections. In particular, how would I handle a POST to a /login endpoint to associate the login username to the socket id connection.

For example:

app.get('/login', (req, res, next) => {
  const someSocket = /* socket */
  const someSocketId = /* socket id */;

  visitorData[someSocketId] = req.body.username;

  someSocket.emit('user', visitorData);
})

Is there a way I can incorporate socket.io into Node/Express routes to (a) associate the user with a socket.id and (b) emit information from a request/response body?

4
  • Looks like a duplicate of stackoverflow.com/questions/22285299/… Dec 16, 2016 at 19:58
  • Hmm, similar idea. But that post has to do with PHP and sessions... I'm dealing with Node/Express and my question has nothing to do with sessions. Just incorporating route request/response bodies into sockets.
    – Himmel
    Dec 16, 2016 at 20:01
  • Are you trying to track active users, or just have a count of current active users? What exactly are you wanting to do with this? Is it a chat thing that keeps track of the amount of people in a room? A count is much easier than keeping track of each user individually. It also becomes more challenging if you need multiple servers(cpu cores) which you'll need if you have quite a few people.
    – Morgan G
    Dec 16, 2016 at 20:21
  • I want to track active users and associate usernames (upon /login POST) with a given socket ID, and then emit that data for use on the client.
    – Himmel
    Dec 16, 2016 at 20:24

1 Answer 1

2

I set up something just like this using passport socket.io. It creates middleware that allows you to access users from sockets. Here is some of my implementation:

app.js:

import GameSocket from './lib/game-socket';

var app = express();

GameSocket(app, 3700);

lib/game-socket.js:

import Server from 'socket.io';
import passportSocketIo from 'passport.socketio';
import cookieParser from 'cookie-parser';

// Configured redisStore for authentication
import {redisStore} from '../lib/session';

// Configured passport strategy for authentication
import passport from '../lib/passport';

export default function GameSocket(app, port) {
  // Setup socket
  var server = new Server();
  var io = server.listen(app.listen(port));

  // Setup authentication
  io.use(passportSocketIo.authorize({
    key: 'connect.sid',
    secret: 'your_secret',
    store: redisStore,
    passport: passport,
    cookieParser: cookieParser,
    success: (data, accept) => accept(null, true),
    failure: (data, message, err, accept) => {
      console.log(err);
      if(err) throw new Error(message);
    }
  }));

  // On connection listener
  io.sockets.on('connection', function (socket) {

      // This is an example of getting user info from the socket
      console.log('Success: New connection with: ', socket.request.user.username);

      // Add your socket events here
    });

  return io;
}

In order to use this you need to use a session store. I personally used a redis session store, connect-redis. This is configured in lib/session.js.

3
  • I already have an existing session/Node configuration setup that doesn't use connect-redis. I'm just using JWTs.
    – Himmel
    Dec 16, 2016 at 21:51
  • Sorry about that, I didn't know how flexible your setup was. You can use any of the session stores on this list, so you don't need to use redis specifically. I don't have another implementation at the moment I'm afraid. Dec 16, 2016 at 21:55
  • Cool no worries. JWTs don't use sessions at all.
    – Himmel
    Dec 17, 2016 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.