To get the client’s IP address in socket.io and JavaScript, we use the handshake.address
property.
For instance, we write
const io = require("socket.io").listen(server);
io.sockets.on("connection", (socket) => {
const address = socket.handshake.address;
console.log(address.address, address.port);
});
to listen for the connection
event with on
.
We get the client address from socket.handshake.address
.
And we get the IP address
and port
from the returned address
.