How can I communicate between Node/Express routes

2019-07-18 23:43发布

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?

1条回答
Explosion°爆炸
2楼-- · 2019-07-19 00:22

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.

查看更多
登录 后发表回答