0

I'm trying to retrieve the client IP address using socket.handshake.address but it always outputs the value undefined.. I'm not sure if the placement of the io.on is a big cause of the error. But I need help with outputting some value.

The following is my code:

var app = require('http').createServer(response);
var io = require('socket.io')(app);
var fs = require('fs');
const readline = require('readline');

let clientIP;
console.log("your ip:" +clientIP);
io.on("connection", function(socket){
    socket.on("send message", function(sent_msg, callback){
        sent_msg = "[ " + getCurrentDate() + " ]: " + sent_msg;

        io.sockets.emit("update messages", sent_msg);
        callback();
    });
    clientIP = socket.handshake.address;
});

var port = process.env.PORT || 3000;
app.listen(port);
console.log("App running on port " +port);

Ultimately, I want to store the IP address into a variable and then console log it. I will use the value for a different purpose later.

THANK YOU SO MUCH!

Edit: I was able to console.log(socket.handshake.address) but it looks like it is outputting ::1. So I printed out the whole handshake and this is what console.logged

{ headers: 
   { host: 'localhost:200',
     connection: 'keep-alive',
     accept: '*/*',
     'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Mobile Safari/537.36',
     referer: 'http://localhost:200/',
     'accept-encoding': 'gzip, deflate, br',
     'accept-language': 'en-US,en;q=0.9',
     cookie: 'Phpstorm-e80bf189=08d2953b-3e27-43a8-b34b-cdd36607b656; Webstorm-dddc562d=07419dd2-1d4c-40b9-bd13-7c5507036a78; _ga=GA1.1.1050967868.1539219676; Webstorm-dddc562e=7b5b97f7-e27a-471d-9a9a-553f616ec370; _ceg.s=pjjo44; _ceg.u=pjjo44; connect.sid=s%3AswIVrUf0ofg5SnevGXjW0b1j63FGeWL1.9sDRdPEtTG5eI9Z8oDjtmuakp6mo7%2B%2FdCel%2Bih4Qb1E; io=83uH7KPwAtjR7b9PAAAA' },
  time: 'Thu Feb 28 2019 15:11:33 GMT-0800 (PST)',
  address: '::1',
  xdomain: false,
  secure: false,
  issued: 1551395493723,
  url: '/socket.io/?EIO=3&transport=polling&t=MasO9T6',
  query: { EIO: '3', transport: 'polling', t: 'MasO9T6' } }

I read some posts here on stackoverflow talking about IPv4 and IPv6 shortcuts but it still couldn't find the answer to get around that.

jemmamariex3
  • 107
  • 2
  • 17
  • Possible duplicate of [Get the client's IP address in socket.io](https://stackoverflow.com/questions/6458083/get-the-clients-ip-address-in-socket-io) – deepchudasama Feb 28 '19 at 11:57
  • @deepbchudasama thank you! i will check if it will work for my project before closing this question – jemmamariex3 Feb 28 '19 at 17:26
  • Hope it will work for you. – deepchudasama Feb 28 '19 at 20:17
  • @deepbchudasama that post sorta helped! but It was outputting ::1. Can you please read my updated post above? – jemmamariex3 Feb 28 '19 at 23:16
  • @deepbchudasama never mind! I found a post in github that helped solve that problem :) https://github.com/socketio/socket.io/issues/2982#issuecomment-353167598 (for those that still are looking for this issue) – jemmamariex3 Feb 28 '19 at 23:21

1 Answers1

1

https://github.com/socketio/socket.io/issues/2982#issuecomment-353167598 (for those that still are looking for this issue)

jemmamariex3
  • 107
  • 2
  • 17