question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Remote address (IP) is always undefined

See original GitHub issue

After upgrading socket.io from 1.x.x to 2.0.3 I can’t get client’s IP address — it’s always undefined.

Test environment: Ubuntu 14.04.5 LTS Node.js 8.1.2 Socket.io 2.0.3

Server:

require('socket.io')(8888).on('connection', socket => {
    console.log('socket.client.conn.remoteAddress', socket.client.conn.remoteAddress);
    console.log('socket.request.connection.remoteAddress', socket.request.connection.remoteAddress);
    console.log('socket.handshake.address', socket.handshake.address);
});

Client:

<script src="http://192.168.13.13:8888/socket.io/socket.io.js"></script>
<script>
    var socket = io('http://192.168.13.13:8888', { transports: [ 'websocket' ] });
    socket.on('connect', function(){
        console.log('connected');
    });
</script>

Aaaaand it’s gone.

daniil@ubuntu:/var/srv/node-test# node server.js
socket.client.conn.remoteAddress undefined
socket.request.connection.remoteAddress undefined
socket.handshake.address undefined

When I remove { transports: [ 'websocket' ] } from client’s code, I got this:

socket.client.conn.remoteAddress ::ffff:192.168.13.12
socket.request.connection.remoteAddress ::ffff:192.168.13.12
socket.handshake.address ::ffff:192.168.13.12

Socket.io 1.7.4 works fine. Socket.io >=2.0.1 is not.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:13
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
polkovnikov-phcommented, Dec 20, 2017

@Serveurperso You can also turn off IPv6 if you http.listen(port, '0.0.0.0'). This way your server won’t be IPv6-compatible, but also you will have remote addresses in IPv4 format. Given the fact you were going to convert IPv6 address to IPv4, IPv6 connectivity shouldn’t be an issue for you.

3reactions
lolgagscommented, Dec 21, 2018
io.on('connection', function(socket){
  const address = socket.handshake.headers["x-forwarded-for"].split(",")[0];
  console.log(address);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Socket.io - Getting client's IP always results in undefined
Since my comment worked for you, I'll put it in an answer. socket.request.connection.remoteAddress is the supported way to get the remote IP ...
Read more >
Net | Node.js v19.3.0 Documentation
The string representation of the remote IP address. For example, '74.125.127.100' or '2001:4860:a005::68' . Value may be undefined if the socket is ...
Read more >
request-ip
A small Node.js module to retrieve the request's IP address. ... Start using request-ip in your project by running `npm i request-ip`.
Read more >
How to get a client IP Address in Node.js (6 ways)
Retrieve User IP Address From the Request Object's Socket Property. First up, we find the request's IP address with a simple vanilla Node...
Read more >
How To Fix the “Server IP Address Could Not Be Found” ...
The message itself tells you that the issue lies with a server's internet protocol (IP) address, but doesn't specify which end of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found