Integrating Socket.IO with Express
So far, apart from sharing the same server, the Socket.IO and Express parts of our application are completely independent. While it's good that they are loosely coupled, some cross-cutting concerns may be relevant to both.
For example, both parts of our application should have a mutually consistent way of identifying the current user. This is especially important if they are to come together to provide a single coherent user experience.
First, let's extend our user middleware to provide the current user's name as well as their ID, by looking them up in the user service as given here src/middleware/users.js
:
'use strict'; module.exports = (service) => { const uuid = require('uuid'); return function(req, res, next) { let userId = req.cookies.userId; if (!userId) { userId = uuid.v4(); res.cookie('userId', userId); req.user = { id: userId }; next(); ...