Categories
JavaScript Answers

How to get the client’s IP address in socket.io and JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *