3

Ultimately, the goal here is to handle DNS round robin with socket.io, to coordinate different connections (socket.io and jQuery ajax) so they go to the same server when one name maps to multiple servers.

With this code, I am able to find the client-side IP address of an incoming websockets connection:

var app = express();
   ...
var server = http.createServer(app);
   ...
var io = socketio.listen(server);
io.on('connection', function(socket) {
  var addr = socket.handshake.address;
  logger.info("Just got incoming connection from client " + addr.address
              + ":" + addr.port);

But how can I find the server-side IP address of this socket? I would like to send the server-side IP address to the client (with socket.emit('serverSideIp': <server-IP>)) so for diagnostic purposes we can track which specific IP address is in use. Also, when a socket.io connection fails and goes to a different IP, I want to redirect client-side AJAX requests to that IP so the client is not talking to different servers at the same itme.

EDIT: Get local IP address in node.js does not answer my question. Answered there is, "What is my list of IP addresses." My question is, "Given a specific socket, what is the local address (IP and port) of that specific socket" which cannot be answered on a multi-homed host by getting a list of addresses from the OS. In Java or C# it is trivial to get the local and remote port and IP address of a socket. How do you do this in node.js for a socket.io "socket" connection?

Community
  • 1
  • 1
Eddie
  • 53,828
  • 22
  • 125
  • 145
  • I'm not sure if this will work but have you tried `server.address()`? – Aust Apr 26 '13 at 21:55
  • server.address() returns this: `{ address: '0.0.0.0', family: 'IPv4', port: 80 }` – Eddie Apr 26 '13 at 22:27
  • possible duplicate of [Get local IP address in node.js](http://stackoverflow.com/questions/3653065/get-local-ip-address-in-node-js) – Pickels Apr 26 '13 at 23:49
  • Updated the question to make it more clear. My question is not at all duplicate of that. I want to know, given a specific connection (socket), what is the local side address (IP and port) of that socket? I can already get the remote side IP and port. – Eddie Apr 27 '13 at 00:43

1 Answers1

1

Socket.IO is listening for a websocket request, but it's doing so on an HTTP connection established by Express. You can find the address on which Express accepted the HTTP connection, like this:

app.get('/', function (req, res) {
    console.log(req.socket.address());

That is, don't wait for a websocket request before you figure out the accept address; you can store it for later if you want it during the Socket.IO connection event.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • This doesn't seem to work. When a socket.io request comes in, the function attached to '/' is not invoked. – Eddie May 02 '13 at 15:31
  • But before a socket.io request comes in, a regular HTTP connection was made, right? – John Zwinck May 02 '13 at 15:33
  • 1
    Not on a failover, no. We're using DNS round robin. If the server we connected to fails, socket.io retries and connect to the other one. On failover there is no previous normal HTTP request. – Eddie May 02 '13 at 15:51
  • Good point. Well, I don't know if there's a totally direct way to do what you want here, so consider disabling reconnect if you don't really need it: http://stackoverflow.com/a/11266954/4323 . Or maybe you can add a connect event callback which publishes the client's IP to the server each time a connection is made...not 100% sure about that. – John Zwinck May 03 '13 at 00:00
  • I ended up hacking socket.io to add this property. I'll soon submit the change to them for review/approval. I doubt I'm the only person who will find this useful. – Eddie May 04 '13 at 02:16
  • @Eddie Do you know if this change request ever rolled in? I am looking for the same functionality. Or what did you change at socket.io is it a fork? – Sentient May 07 '15 at 14:54
  • @Sentient: Sorry, I stopped using node.js not long after getting this working locally, so I don't know if the change made it to anything official. I just made a local hack to get it working, as I was working on a "proof of concept" item. – Eddie May 07 '15 at 18:53
  • @Eddie, thanks for the update. I currently determine it when the first client is connected socket.client.conn.remoteAddress – Sentient May 07 '15 at 19:47
  • 1
    @Sentient: I found my change. I changed lib/manager.js, which is no longer a part of socket.io, so that when the `io.on('connection' ...` event was invoked, `socket.handshake` had property `socket.handshake.localAddress`. It looks like this was entirely reimplemented just before 1.0, so my solution is no longer relevant. You may wish to check the properties of `socket` when you get a connection event to see if the local socket IP/port is available there. – Eddie May 07 '15 at 21:37