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.

socket.request.connection.remoteAddress is undefined for AJAX node.js clients

See original GitHub issue

Issues such as #1483 and #1387 illustrate that socket.request.connection.remoteAddress is not always defined.

In #1387, someone pointed out that a websocket client’s IP address is present, but that a client using a polling transport (like socket.io-client does by default when running in node.js) does not have remoteAddress present.

Test case:

server.js

var io = require('socket.io')(5555);

io.on('connection', function (socket) {
    console.log(socket.request.connection.remoteAddress);
});

client1.js (running this should produce undefined on the server console output)

var io = require('socket.io-client')('http://localhost:5555');

client2.js (running this should produce the client’s IP address on the server console output)

var io = require('socket.io-client')('http://localhost:5555', { transports: ['websocket'] });

Is there any particular reason that there is not a way to retrieve a client’s IP address if they are using a node.js client with the AJAX transport? I would prefer not to require all of my clients to manually override the transport.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
calzonemancommented, Aug 24, 2014

Ok so the cached engine.io address can actually be accessed without any patching to socket.io via socket.client.conn.remoteAddress, but this depends on this commit, which seems not to have affected the version being pulled from npm. I suspect that either the change wasn’t published, or because the patch version didn’t change, npm is caching the wrong version.

0reactions
polkovnikov-phcommented, Dec 28, 2017

@darrachequesne I ended up forking and patching source code anyway. Figuring out, which of the requests counts as handshake, whether it will be really stored at any moment of operation, what it will be named in the next version of the library, how to put requests through an Agent, how to use socket.io-client with a cookie and other headers (for either part of handshake, websockets included) and how to get around other bugs that are not closed for 4 years is just way too time consuming.

Read more comments on GitHub >

github_iconTop Results From Across the Web

express req.connection.remoteAddress is sometimes undefined
I need to have the client's remote IP address and I am obtaining it in the following way: function onPostRequest(req, res) { var...
Read more >
Real time monitoring PHP applications with websockets and ...
Using node.js to track PHP error/warnings/notices in real time. One experiment with HTML5, websockets and node.js.
Read more >
TutorialsPoint node.js.pdf - GitHub Pages
Read request and return response: The server created in an earlier step will read the HTTP request made by the client which can...
Read more >
Node.js Notes for Professionals
Section 1.10: TLS Socket: server and client ... Section 24.1: Connect to MongoDB Using Mongoose ... Chapter 64: Routing AJAX requests with Express.JS....
Read more >
node req ip address Code Example - Code Grepper
Socket object has a property. 3. //remoteAddress, therefore you should be able to get the IP with this call: 4. ​. 5. request.connection....
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