DEV Community

Cover image for RabbitMQ with Nodejs Part 2
lalit292929
lalit292929

Posted on

RabbitMQ with Nodejs Part 2

In the previous post, We had our introduction with the message broker and rabbitmq, In this post, we will discuss the first component ie. Connection

Connection

Below are the few properties which will help us to understand the connection to rabbitmq
1. One client library connection uses a single TCP connection. In order for a client to successfully connect with a message broker.
2. Connections should not be opened by the client for each individual operation.
3. Once the client connects successfully, it can publish and consume messages.
4. When the connection is no longer needed, it should be closed to save on resources.

Lifecycle:

Below are the major lifecycle component of a connection with the message broker ( RabbitMQ )
1. Application configures the client library to use a certain connection endpoint (e.g. hostname and port)
2. The library resolves the hostname to one or more IP addresses
3. The library opens a TCP connection to the target IP address and port
4. After the server has accepted the TCP connection, a protocol-specific negotiation procedure is performed
5. The server then authenticates the client
6. The client now can perform operations, each of which involves an authorization check by the server.
7. Once the requirement is completed, a connection is closed.

Example Code to establish a connection in nodejs application

const amqp = require('amqplib');
const connection = await amqp.connect('amqp://localhost');
// your application logic
connection.close();
Enter fullscreen mode Exit fullscreen mode

Logging and Monitoring:

RabbitMQ logs all inbound client connections that send at least 1 byte of data. Connections that are opened without any activity will not be logged. This is to prevent TCP load balancer health checks from flooding the logs.

Successful authentication, clean and unexpected connection closure will also be logged.

Interface :

The following interface of a connection is provided to us by
amqplib module

{
    close(): Promise<void>;
    createChannel(): Promise<Channel>;
    createConfirmChannel(): Promise<ConfirmChannel>;
    connection: {
        serverProperties: ServerProperties;
    };
}
Enter fullscreen mode Exit fullscreen mode

Read More -> https://www.rabbitmq.com/connections.html

Note: I am writing for the first time and will highly appreciate any feedback or suggestions.
Find me on Linkedin

Top comments (0)