5

Any way to check if TCP socket in NodeJS has been connected. As opposed to still connecting in transient. Am looking for a property as opposed to an emitted event. Is it even possible?

1
  • My initial answer was that there were no ways to do it without events but I edited it as there is an undocumented way to do it. I strongly advise against it though. Commented Apr 11, 2015 at 11:11

1 Answer 1

4

The manual shows you that an event is emitted for when the connection is established and that is how you should handle it the Node.js way (using non-blocking, asynchronous i/o)

There is though an undocumented way to check it, the socket object has a _connecting boolean property that if set to false means it has been connected already or failed.

Example :

var net = require('net');

//Connect to localhost:80
var socket = net.createConnection(80);

if(socket._connecting === true) {
    console.log("The TCP connection is not established yet");
}
Sign up to request clarification or add additional context in comments.

1 Comment

how to test if it is connected?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.