DEV Community

Cover image for RabbitMQ with Nodejs Part 2
lalit292929
lalit292929

Posted on

1

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

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay