Socket.io: GenerateId not working on 3x

Created on 23 Nov 2020  ·  5Comments  ·  Source: socketio/socket.io

I want to:

  • [x] report a bug
  • [ ] request a feature

Current behaviour

When setting io.engine.generateId in v2.3.0, socket ids are set accordingly.
In 3x (tested on 3.0.0 and 3.0.3) they are not.

Steps to reproduce

In Node.js:

const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const { v4: uuidv4 } = require('uuid');

io.engine.generateId = function (req)
{
    return uuidv4();
}

Expected behaviour

As working in v2.3.0, the expected format of the socket ids should be, in this case, "xxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", being x alphanumerical. Instead I get the default socket id format.

Setup

  • OS: Windows 10, running on Node.js
  • socket.io version: 3.0.0 and 3.0.3

Other information (e.g. stacktraces, related issues, suggestions how to fix)

No errors are thrown, the format is simply not set.

question

All 5 comments

Yes, that is expected.

Here is the related commit: https://github.com/socketio/socket.io/commit/2875d2cfdfa463e64cb520099749f543bbc4eb15

So, the generateId method is still working in v3, but it will now only affect the sid query param, not the id attribute of the socket.

May I ask what is your use case?

Oh, okay! Thanks for the info.

I was reusing the id to transmit it to other clients for video calls using peer.js, but peer.js doesn't like its format.

But I just saw in the commit that you mentioned that I shouldn't be doing this for security reasons, so I guess I can close this issue.

Thanks!

I think this issue should be re-opened (or a new issue created), as in version 3.0, it doesn't appear that the generateId function is ever getting called. I understand that it is no longer replacing the socket.id property, but @darrachequesne earlier comment suggests that it is changing a different property.

However, as per the documentation, doing the following:

const content = require('fs').readFileSync(__dirname + '/index.html', 'utf8');

const httpServer = require('http').createServer((req, res) => {
  // serve the index.html file
  res.setHeader('Content-Type', 'text/html');
  res.setHeader('Content-Length', Buffer.byteLength(content));
  res.end(content);
});

const io = require('socket.io')(httpServer);
io.engine.generateId((req) => {
  console.log('This line is never executed');
  return new Date().valueOf().toString()
});

io.on('connection', socket => {
  console.log('connect');
  console.log(socket.sid);
});

httpServer.listen(3000, () => {
  console.log('go to http://localhost:3000');
});

The function is never invoked.

@BrianHVB I think there is a typo in your code example:

io.engine.generateId = ((req) => {
  console.log('This line is never executed');
  return new Date().valueOf().toString()
});

Could you please confirm that it fixes the issue?

@darrachequesne - Oops, yes, that does. Thank you.

Was this page helpful?
0 / 5 - 0 ratings