How to get client IP address using websocket (einaros / ws) lib in node.js?

I cannot find the client IP parameter on the client object.

Got this from printing the keys in the socket object:

mysocket._socket.address() { port: 8081, family: 2, address: '127.0.0.1' }

mysocket._socket.remoteAddress '74.125.224.194'

mysocket._socket.remotePort 41435

I don't have any documentation so I'm not sure how well this is supported across versions :/

After a bit of messing around trying to figure out which one gives the client (web browser's) IP address, the answer is:

websocket._socket.remoteAddress

Or if you prefer the cleaner version:

socket.upgradeReq.connection.remoteAddress

You can use this, for example, to GeoIP locate where the user is connecting from.

EDIT:

If you're running Node behind an Nginx reverse proxy (or any other reverse proxy for that matter), you may need to use:

socket.upgradeReq.headers['x-forwarded-for'] || socket.upgradeReq.connection.remoteAddress