3

I have read here that I can get client's IP in node.js with

socket.request.connection.remoteAddress

for example:

var socket = io.listen(server);
socket.on('connection', function(client){
    var client_ip_address = socket.request.connection.remoteAddress;
}

But when I use this solution I get the error message "Cannot read property 'connection' of undefined" which means that socket.request is undefined.

Why would that be the case?

I have also tried another option found on the same page:

var sHeaders = socket.handshake.headers;
console.info('[%s:%s] CONNECT', sHeaders['x-forwarded-for'], sHeaders['x-forwarded-port']);

but this just outputs

[undefined:undefined] CONNECT
Community
  • 1
  • 1
Sulli
  • 763
  • 1
  • 11
  • 33

3 Answers3

2

I just tried something like this on my computer and it returned 127.0.0.1 which is correct:

var express = require('express');
var io = require('socket.io');
var path = require('path');

var app = express();
var server = http.createServer(app);
var ioServer = io.listen(server);

ioServer.sockets.on('connection', function (socket) {
    console.log(socket.request.connection.remoteAddress);
    // This gave 127.0.0.1
});

The socket from your code is not the clients socket connection but the "server" itself so it's normal that it does not have a request property. Use the code I wrote or try changing:

socket.on(

to:

socket.sockets.on(

and:

socket.request.connection.remoteAddress

to (for Express v4.x, Socket.IO v1.x):

client.request.connection.remoteAddress

or (for Express v3.x, Socket.IO v0.x) (this apparently returns the server IP):

socket.handshake.address

and it should give you what you need.

EDIT: If I remember correctly socket.io versions < 1.x don't use client.request but rather client.handshake so try that one.

To upgrade express and socket.io, check these two links:

tkit
  • 8,082
  • 6
  • 40
  • 71
  • The problem is probably with the version number (your suggested changes don't fix the problem). I'm running express 3.2.6 and socketio 0.9.6 at the moment. What is the minimum version that should work without breaking up everything? I have tried to update to socketio 1.2.0 for example but I wasn't able to run my app afterwards `TypeError: Object # has no method 'configure'` – Sulli Nov 13 '14 at 09:21
  • I already tried with `handshake`, in local it returns 127.0.0.1, which is fine, but when I upload my app on heroku I get different IPs every time I connect to the app, even if the connections come from the same machine. I also read here http://stackoverflow.com/a/24881422/1967110 that `handshake` actually returns the server's IP not the client's IP. – Sulli Nov 13 '14 at 12:40
  • @SullivanOrlean have you tried outputting the whole `socket.handshake.headers` now that that part has been fixed and checking if the IP you're looking for is somewhere in there? If not, I believe your best bet is to upgrade both Express and Socket.IO and try the `socket.request`. – tkit Nov 13 '14 at 13:14
0
var server = require('http').Server(app);
var io = socketIO(server);

io.on('connection', function(socket) {
    console.log('client IP: ',socket.handshake.address);
// this should work too
    console.log('client IP: ',socket.client.conn.remoteAddress);
// or
    console.log('client IP: ',socket.conn.remoteAddress);
});

server.listen(3000);

I just read that you are using heroku.. Well makes sense that you get a different ip Address, usually web traffic in heroku is over a proxy.. because of that you wouldnt be able to get the remoteAddress..

rahpuser
  • 1,224
  • 10
  • 31
0

in socketio.js, simply update:

 console.info('[%s] DISCONNECTED', socket.address);

to:

 console.info('[%s] DISCONNECTED', socket.handshake.address);

Same for the CONNECTED logs.