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?
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
:lib/game-socket.js
: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
.