To differentiate clients with webSocketServer in Node.js, we canm use the sec-websocket-key
header.
For instance, we write
wss.on("connection", (ws, req) => {
ws.id = req.headers["sec-websocket-key"];
//...
});
to listen to the connection
event with on
.
And we get the sec-websocket-key
header with req.headers["sec-websocket-key"]
in the event handler to get the client’s ID.