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.
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.
This works for the 2.3.0 version:
io.on('connection', socket => {
const ip = socket.handshake.headers['x-forwarded-for'] || socket.conn.remoteAddress.split(":")[3];
console.log(ip);
});
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);
});
In version v2.3.0
this work for me :
socket.handshake.headers['x-forwarded-for'].split(',')[0]
I went through blog posts and even peoples answers to the question.
I tried socket.handshake.address and it worked but was prefixed with ::ffff:
I had to use regex to remove that.
This works for version 4.0.1
socket.conn.remoteAddress
Welcome in 2019, where typescript slowly takes over the world. Other answers are still perfectly valid. However, I just wanted to show you how you can set this up in a typed environment.
In case you haven't yet. You should first install some dependencies
(i.e. from the commandline: npm install <dependency-goes-here> --save-dev)
"devDependencies": {
...
"@types/express": "^4.17.2",
...
"@types/socket.io": "^2.1.4",
"@types/socket.io-client": "^1.4.32",
...
"ts-node": "^8.4.1",
"typescript": "^3.6.4"
}
I defined the imports using ES6 imports (which you should enable in your tsconfig.json file first.)
import * as SocketIO from "socket.io";
import * as http from "http";
import * as https from "https";
import * as express from "express";
Because I use typescript I have full typing now, on everything I do with these objects.
So, obviously, first you need a http server:
const handler = express();
const httpServer = (useHttps) ?
https.createServer(serverOptions, handler) :
http.createServer(handler);
I guess you already did all that. And you probably already added socket io to it:
const io = SocketIO(httpServer);
httpServer.listen(port, () => console.log("listening") );
io.on('connection', (socket) => onSocketIoConnection(socket));
Next, for the handling of new socket-io connections,
you can put the SocketIO.Socket type on its parameter.
function onSocketIoConnection(socket: SocketIO.Socket) {
// I usually create a custom kind of session object here.
// then I pass this session object to the onMessage and onDisconnect methods.
socket.on('message', (msg) => onMessage(...));
socket.once('disconnect', (reason) => onDisconnect(...));
}
And then finally, because we have full typing now, we can easily retrieve the ip from our socket, without guessing:
const ip = socket.conn.remoteAddress;
console.log(`client ip: ${ip}`);
Here's how to get your client's ip address (v 3.1.0):
// Current Client
const ip = socket.handshake.headers["x-forwarded-for"].split(",")[1].toString().substring(1, this.length);
// Server
const ip2 = socket.handshake.headers["x-forwarded-for"].split(",")[0].toString();
And just to check if it works go to geoplugin.net/json.gsp?ip= just make sure to switch the ip in the link. After you have done that it should give you the accurate location of the client which means that it worked.
for my case
i change my
app.configure(socketio)
to
app.configure(socketio(function(io) {
io.use(function (socket, next) {
socket.feathers.clientIP = socket.request.connection.remoteAddress;
next();
});
}));
and i'm able to get ip like this
function checkIP() {
return async context => {
context.data.clientIP = context.params.clientIP
}
}
module.exports = {
before: {
all: [],
find: [
checkIp()
]}
}