DEV Community

Bahman Shadmehr
Bahman Shadmehr

Posted on

Building a Command-Line Interface (CLI) for RabbitMQ Messaging app

Introduction:
In this blog post, we will explore how to build a Command-Line Interface (CLI) for RabbitMQ messaging. Using the click library, we will create a CLI that allows us to consume messages from RabbitMQ queues and send notifications using RabbitMQ producers. This CLI will provide a convenient way to interact with the RabbitMQ messaging system from the command line.

Table of Contents:

  1. Overview
  2. Setting up the CLI
  3. Consuming SMS Messages
  4. Consuming All Messages
  5. Sending Notifications
  6. Conclusion

Overview:
In this blog post, we will guide you through the process of building a CLI for RabbitMQ messaging. We will utilize the click library, a powerful tool for creating command-line interfaces, to define commands and options for our CLI. We will demonstrate how to consume SMS messages and all messages from RabbitMQ queues, as well as how to send notifications using RabbitMQ producers.

Setting up the CLI:
To begin, let's set up the necessary infrastructure for our CLI. First, install the click library by running the following command:

pip install click
Enter fullscreen mode Exit fullscreen mode

Next, create a new Python file and import the necessary modules:

import click
from consumers.sms_consumer import SMSConsumer
from producers.notification import send_notification
from consumers.all_consumer import ALLConsumer
Enter fullscreen mode Exit fullscreen mode

Define the CLI using the click.group() decorator and the cli() function as the entry point for our CLI:

@click.group()
def cli():
    pass
Enter fullscreen mode Exit fullscreen mode

Consuming SMS Messages:
Next, let's define a command sms that allows us to consume SMS messages from a specific RabbitMQ queue. We will use the click.command() decorator to define the command and click.option() decorator to add options for specifying the RabbitMQ connection details:

@cli.command()
@click.option("--host", "-h", default="localhost", help="RabbitMQ host")
@click.option("--port", "-p", default=5672, help="RabbitMQ port")
@click.option("--username", "-u", default=None, help="RabbitMQ username")
@click.option("--password", "-w", default=None, help="RabbitMQ password")
def sms(host, port, username, password):
    consumer = SMSConsumer("sms", "notif.sms", host, port, username, password)
    consumer.consume()
Enter fullscreen mode Exit fullscreen mode

Consuming All Messages:
Similarly, let's define a command all that enables us to consume all messages from a RabbitMQ queue. We will use the same click.command() and click.option() decorators to define the command and options:

@cli.command()
@click.option("--host", "-h", default="localhost", help="RabbitMQ host")
@click.option("--port", "-p", default=5672, help="RabbitMQ port")
@click.option("--username", "-u", default=None, help="RabbitMQ username")
@click.option("--password", "-w", default=None, help="RabbitMQ password")
def all(host, port, username, password):
    consumer = ALLConsumer("all", "notif.*", host, port, username, password)
    consumer.consume()
Enter fullscreen mode Exit fullscreen mode

Sending Notifications:
Lastly, let's define a command send_message that sends a notification using a RabbitMQ producer. This command will call the send_notification() function from the producers.notification module, and print a success message indicating that the messages were sent successfully:

@cli.command()
def send_message():
    send_notification()
    print("Messages sent successfully...")
Enter fullscreen mode Exit fullscreen mode

Conclusion:
In this blog post, we have covered how to

build a Command-Line Interface (CLI) for RabbitMQ messaging using the click library. We have demonstrated how to consume SMS messages and all messages from RabbitMQ queues, as well as how to send notifications using RabbitMQ producers. By leveraging this CLI, you can easily interact with the RabbitMQ messaging system from the command line, enabling efficient and convenient message consumption and notification sending.

To run the CLI, execute the following command in your terminal:

python <filename>.py <command> [--host <RabbitMQ_host>] [--port <RabbitMQ_port>] [--username <RabbitMQ_username>] [--password <RabbitMQ_password>]
Enter fullscreen mode Exit fullscreen mode

Replace <filename>.py with the name of your Python file, and <command> with the desired command (e.g., sms, all, send_message). The RabbitMQ connection details (<RabbitMQ_host>, <RabbitMQ_port>, <RabbitMQ_username>, <RabbitMQ_password>) are optional. If not provided, default values will be used.

Here's an example command to consume SMS messages:

python <filename>.py sms --host localhost --port 5672 --username guest --password guest
Enter fullscreen mode Exit fullscreen mode

This command will execute the sms command of your CLI, consuming SMS messages from RabbitMQ using the provided connection details.

Remember to replace <filename>.py with your Python file's name.

Top comments (0)