Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the client's IP address in socket.io

When using socket.IO in a Node.js server, is there an easy way to get the IP address of an incoming connection? I know you can get it from a standard HTTP connection, but socket.io is a bit of a different beast.

like image 578
Toji Avatar asked Jun 23 '11 17:06

Toji


People also ask

How do I connect Socket.IO to client side?

var socket = require('socket. io-client')('ws://ws.website.com/socket.io/?EIO=3&transport=websocket'); socket. on('connect', function() { console. log("Successfully connected!"); });

Is socket IO client side?

The Socket instance (client-side) | Socket.IO.

How do I monitor Socket.IO traffic?

Use Monitor.io to observe connections and replay messages When you add it to your Node. js application, it provides a monitoring interface that you can access remotely by opening a Telnet connection to a particular port. It shows a list of active Socket.io client connections for your application.


1 Answers

Okay, as of 0.7.7 this is available, but not in the manner that lubar describes. I ended up needing to parse through some commit logs on git hub to figure this one out, but the following code does actually work for me now:

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); }); 
like image 152
Toji Avatar answered Sep 26 '22 10:09

Toji