0

In my node application i need to get the ip address of the user using socket.io.I have tried below code but till i'm getting 127.0.0.1.How can i get my ip address.

1.

  var io = require("socket.io").listen(server);

    io.sockets.on("connection", function (socket) {
    var address = socket.handshake.address;
    console.log("New connection from " + address.address + ":" + address.port);
 }

2.

   var io = require('socket.io').listen(80);
     io.sockets.on('connection', function (socket) {
     var endpoint = socket.manager.handshaken[socket.id].address;
     console.log('Client connected from: ' + endpoint.address + ":" + endpoint.port);
  });

3.

   var socket = io.listen(server);
       socket.on('connection', function(client){
          var ip_address = client.connection.remoteAddress;
       });
sachin
  • 13,605
  • 14
  • 42
  • 55
  • 1
    I'm pretty sure that if you're running node locally your ip _is_ 127.0.0.1. Any other ip is most likely irrelevant. – Andreas Hultgren Apr 24 '13 at 07:19
  • yes my app is running locally localhost:4000.. – sachin Apr 24 '13 at 07:27
  • 1
    So then you're getting the correct ip. If you put the app on an external server you'll get whatever ip your isp provided (the one you see if you google "ip"). If you put it on another machine on the same network you'll get the ip of you machine in the internal network. – Andreas Hultgren Apr 24 '13 at 07:32
  • Related : http://stackoverflow.com/questions/6458083/socket-io-get-clients-ip-address – nha Oct 13 '14 at 19:54

1 Answers1

2

Try this, this works for me.

Var client = require('socket.io').listen(8080).sockets;

client.on('connection',function(socket){ 
var clientIpAddress= socket.request.socket.remoteAddress;
});
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
VoidA313
  • 516
  • 3
  • 10
  • The line `socket.request.socket.remoteAddress` returns the websocket IP address not the user's IP. I am not sure what I am doing wrong here. by my code is the same as yours – Junior Sep 15 '15 at 00:11