Vandervidi
Vandervidi

Reputation: 692

How to get client IPv4 address (not IPv6) using Sockets.io + Node.js server

I would like to get an IPv4 IP address of clients that connect my Node.js server using Socket.io version 1.3.5.

I tried most of the ways suggested in SO but most of them are or deprecated or they return an IPv6 address.

The only thing that returned some value was

socket.request.connection.remoteAddress

but it was in a IPv6 format.

Any way getting an IPv4 address?

Upvotes: 1

Views: 3545

Answers (1)

galethil
galethil

Reputation: 1016

If client is connecting using IPv6 you could not get IPv4 address. If you want to have IPv4 addresses you need to turn off IPv6 support on your server.

Than I'm using this to get IP address var ip = socket.client.request.headers['x-forwarded-for'] || socket.client.conn.remoteAddress || socket.conn.remoteAddress || socket.request.connection.remoteAddress;

Upvotes: 3

Related Questions