socket.io: Remote address (IP) is always undefined

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.

About this issue

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

Commits related to this issue

Most upvoted comments

@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.

io.on('connection', function(socket){
  const address = socket.handshake.headers["x-forwarded-for"].split(",")[0];
  console.log(address);
});

We also experience this issue on 2.x and are unable to upgrade for now.

https://github.com/thelounge/lounge/pull/1143 https://travis-ci.org/thelounge/lounge/builds/241839980

Thanks for your prompt response.