custom client ID


I'm making a chat app with, and I'd like to use my custom client id, instead of the default ones (8411473621394412707, 1120516437992682114). Is there any ways of sending the custom identifier when connecting or just using something to track a custom name for each ID? Thanks!

10/9/2011 9:06:06 AM

Accepted Answer

You can create an array on the server, and store custom objects on it. For example, you could store the id created by and a custom ID sent by each client to the server:

var util = require("util"),
    io = require('/').listen(8080),
    fs = require('fs'),
    os = require('os'),
    url = require('url');

    var clients =[];

    io.sockets.on('connection', function (socket) {

        socket.on('storeClientInfo', function (data) {

            var clientInfo = new Object();
            clientInfo.customId         = data.customId;
            clientInfo.clientId     =;

        socket.on('disconnect', function (data) {

            for( var i=0, len=clients.length; i<len; ++i ){
                var c = clients[i];

                if(c.clientId =={


in this example, you need to call storeClientInfo from each client.

    var socket = io.connect('http://localhost', {port: 8080});

    socket.on('connect', function (data) {
        socket.emit('storeClientInfo', { customId:"000CustomIdHere0000" });

Hope this helps.

10/11/2011 3:15:47 AM

To set custom socket id, generateId function must be overwritten. Both of eio and engine props of server object can be used for to manage this operation.

A simple example:

var app = require('express')();
var http = require('http').Server(app);
var io = require('')(http);

io.engine.generateId = function (req) {
    // generate a new custom id here
    return 1

io.on('connection', function (socket) {
    console.log(; // writes 1 on the console

It seems to be it has been handled.

It must be in mind that socket id must be unpredictable and unique value with considering security and the app operations!

Extra: If is returned as undefined because of your intense processes on your generateId method, async/await combination can be used to overcome this issue on node.js version 7.6.0 and later.
handshake method of node_modules/ file should be changed as following:



Server.prototype.generateId = function (req) {
  return base64id.generateId();

Server.prototype.handshake = function (transportName, req) {
  var id = this.generateId(req);


// function assignment

io.engine.generateId = function (req) {
  return new Promise(function (resolve, reject) {
    let id;
    // some intense id generation processes
    // ...


Server.prototype.handshake = async function (transportName, req) {
  var id = await this.generateId(req);

Note: At v4.0, generateId method would accept a callback. So it would not needed to change handshake method. Only generateId method replacement is going to be enough. For instance:

io.engine.generateId = function (req, callback) {
  // some intense id generation processes
  // ...

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow