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.
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();
}
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.
No errors are thrown, the format is simply not set.
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.