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.node
From: https://www.wentong.org/codex/question-2018081564702.htmlhtml
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.node
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:nginx
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); });
for 1.0.4:git
io.sockets.on('connection', function (socket) { var socketId = socket.id; var clientIp = socket.request.connection.remoteAddress; console.log(clientIp); });
If you use an other server as reverse proxy all of the mentioned fields will contain localhost. The easiest workaround is to add headers for ip/port in the proxy server.github
Example for nginx: add this after your proxy_pass
:web
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-Port $remote_port;
This will make the headers available in the socket.io node server:websocket
var ip = socket.handshake.headers["x-real-ip"]; var port = socket.handshake.headers["x-real-port"];
Note that the header is internally converted to lower case.app
If you are connecting the node server directly to the client,socket
var ip = socket.conn.remoteAddress;
works with socket.io version 1.4.6 for me.ide
For latest socket.io version use
socket.request.connection.remoteAddress
For example:
var socket = io.listen(server); socket.on('connection', function (client) { var client_ip_address = socket.request.connection.remoteAddress; }
beware that the code below returns the Server's IP, not the Client's IP
var address = socket.handshake.address; console.log('New connection from ' + address.address + ':' + address.port);
Using the latest 1.0.6
version of Socket.IO
and have my app deployed on Heroku
, I get the client IP
and port
using the headers
into the socket handshake
:
var socketio = require('socket.io').listen(server); socketio.on('connection', function(socket) { var sHeaders = socket.handshake.headers; console.info('[%s:%s] CONNECT', sHeaders['x-forwarded-for'], sHeaders['x-forwarded-port']); }
This seems to work:
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); });
Since socket.io 1.1.0, I use :
io.on('connection', function (socket) { console.log('connection :', socket.request.connection._peername); // connection : { address: '192.168.1.86', family: 'IPv4', port: 52837 } }
Edit : Note that this is not part of the official API, and therefore not guaranteed to work in future releases of socket.io.
Also see this relevant link : engine.io issue
Version 0.7.7 of Socket.IO now claims to return the client's IP address. I've had success with:
var socket = io.listen(server); socket.on('connection', function (client) { var ip_address = client.connection.remoteAddress; }
Very easy. First put
io.sockets.on('connection', function (socket) { console.log(socket);
You will see all fields of socket. then use CTRL+F and search the word address. Finally, when you find the field remoteAddress use dots to filter data. in my case it is
console.log(socket.conn.remoteAddress);
From reading the socket.io source code it looks like the "listen" method takes arguments (server, options, fn) and if "server" is an instance of an HTTP/S server it will simply wrap it for you.
So you could presumably give it an empty server which listens for the 'connection' event and handles the socket remoteAddress; however, things might be very difficult if you need to associate that address with an actual socket.io Socket object.
var http = require('http') , io = require('socket.io'); io.listen(new http.Server().on('connection', function(sock) { console.log('Client connected from: ' + sock.remoteAddress); }).listen(80));
Might be easier to submit a patch to socket.io wherein their own Socket object is extended with the remoteAddress property assigned at connection time...
Latest version works with:
console.log(socket.handshake.address);
on socket.io
1.3.4 you have the following possibilities.
socket.handshake.address
,
socket.conn.remoteAddress
or
socket.request.client._peername.address
use socket.request.connection.remoteAddress
I have found that within the socket.handshake.headers there is a forwarded for address which doesn't appear on my local machine. And I was able to get the remote address using:
socket.handshake.headers['x-forwarded-for']
This is in the server side and not client side.
In socket.io 2.0: you can use:
socket.conn.transport.socket._socket.remoteAddress
works with transports: ['websocket']
In 1.3.5 :
var clientIP = socket.handshake.headers.host;
socket.handshake.headers['x-forwarded-for'].split(",")[0]
As of socket 3.1.2