I\'m making a chat app with socket.io, and I\'d like to use my custom client id, instead of the default ones (8411473621394412707
, 1120516437992682114
This will work with 2.2.0 and above version of Socket.IO
To set custom Socket Id, generateId
function must be overwritten.
A simple example:
Server Side
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.use((socket, next) => {
io.engine.generateId = () => {
// USE ONE OF THESE
socket.handshake.query.CustomId; // this work for me
// return socket.handshake.query.CustomId;
}
next(null, true);
});
io.on('connection', function (socket) {
console.log(socket.id);
})
Clint Side
io.connect(URL, { query: "CustomId = CUSTOM ID IS HERE" })
NOTE: *It must be in mind that socket id must be a unique value.