tlsSocket.address() Method in Node.js
×


tlsSocket.address() Method in Node.js

195

In Node.js, when working with secure connections through the tls module, it's often useful to know details about the underlying socket, such as the IP address and port it's bound to. This is where the tlsSocket.address() method becomes handy.

What is tlsSocket.address()?

The tlsSocket.address() method returns information about the local endpoint of a TLS socket. Specifically, it gives you an object that includes the IP address, port number, and IP family (like IPv4 or IPv6) of the socket connection.

This is especially useful when building network servers or debugging connection issues, as it helps identify where the server or client is bound in terms of network interface.

Syntax

        
        tlsSocket.address()
        
        

This method returns an object in the following format:

        
        {
            port: <number>,
            family: <'IPv4' | 'IPv6'>,
            address: <string>
        }
        
        

Example Usage

Here’s a basic example that sets up a TLS server and prints the address of each connected client:

        
        const tls = require('tls');
        const fs = require('fs');

        const options = {
            key: fs.readFileSync('server-key.pem'),
            cert: fs.readFileSync('server-cert.pem')
        };

        const server = tls.createServer(options, (socket) => {
            const socketAddress = socket.address();
            console.log('Connected client address:', socketAddress);
        });

        server.listen(8443, () => {
            console.log('TLS server is running on port 8443');
        });
        
        

When a client connects, the server logs the local address and port of the socket, which helps in monitoring and diagnostics.

Output

The output will be an object like this:

        
        Connected client address: {
            address: '127.0.0.1',
            family: 'IPv4',
            port: 8443
        }
        
        

Use Cases

  • Logging the server’s listening address for monitoring
  • Debugging which interface the TLS socket is bound to
  • Dynamic binding to network interfaces
  • Verifying server or client connection endpoints

Important Notes

If the socket has not been bound yet (i.e., the server hasn’t started or the connection isn’t active), calling tlsSocket.address() might return null or an empty object. So make sure the connection is established before using this method.

Also, note that this method gives you the local endpoint info (your server or client’s side), not the remote peer’s address. For that, you would use tlsSocket.remoteAddress and tlsSocket.remotePort.

Conclusion

The tlsSocket.address() method in Node.js is a helpful utility for obtaining local network details of a secure socket. Whether you’re setting up a TLS server, debugging socket behavior, or logging connection info, this method gives you easy access to the socket's IP, port, and protocol family. It’s a small but powerful feature in the toolkit for managing secure Node.js applications.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat