So i noticed that you can run 'io()' in console on client side. I'm worried that if someone were to loop it, it would crash the node.js server. Does anybody know how to prevent multiple connection for the same user.
2 Answers
It is a fairly complicated process to do that properly.
But on that same note, people won't be able to crash your server with socket.io as easy as you think they would be able to.
Node.js can handle a ton of connections at once, same with socket.io. Obviously these are both dependent on what your server actually is; but even as Raspberry Pi can handle a significant amount of connections.
But, if you truly must implement this, I'd recommend checking out this issue and just making a counter-based dictionary of IP's and to disconnect sockets if their IP goes above a specific number.
Get the client's IP address in socket.io
Very crude, but it would do what you need.

- 1,116
- 4
- 16
you need some helper function on server side
get user ip with this package:
npm install request-ip
create array of users:
let users = [ ];
validate and add user to array on new join request
const requestIp = require('request-ip');
const addUser = () => {
const ipMiddleware = function(req, res) {
const clientIp = requestIp.getClientIp(req);
};
const existingUser = users.find(user.clientIp === clientIp)
if (existingUser) {
return false
}
const newUser = { clientIp };
users.push(newUser)
return true
}

- 49
- 3
-
It is not safe to assume that the same client IP is the same client user. Even just two people on a home network both connecting to your server will have the same client IP (the IP of your home gateway). Even potentially worse for people at universities or companies that use proxies on their internet gateway. – jfriend00 Mar 07 '21 at 19:38