Client Configuration
Overview
To configure your client make sure you have the access credentials. Next you need to choose and set up the client library for your preferred programming language.
Here is example of how to connect and recieve updates in Python using pika library.
import pika
import json
import os
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
class Config:
def __init__(self):
self.RABBITMQ_HOST = os.getenv('RABBITMQ_HOST')
self.QUEUE_NAME = os.getenv('QUEUE_NAME')
self.RABBITMQ_USER = os.getenv('RABBITMQ_USER')
self.RABBITMQ_PASSWORD = os.getenv('RABBITMQ_PASSWORD')
class RabbitMQListener:
def __init__(self, config):
self.host = config.RABBITMQ_HOST
self.queue = config.QUEUE_NAME
self.credentials = pika.PlainCredentials(config.RABBITMQ_USER, config.RABBITMQ_PASSWORD)
self.connection = None
self.channel = None
def setup_connection(self):
self.connection = pika.BlockingConnection(pika.ConnectionParameters(host=self.host,
credentials=self.credentials, heartbeat=30))
self.channel = self.connection.channel()
logging.info(f"Connected to RabbitMQ host: {self.host}")
def declare_queue(self):
self.channel.queue_declare(queue=self.queue, passive=True)
logging.info(f"Queue '{self.queue}' declared")
def callback(self, ch, method, properties, body):
message = json.loads(body)
logging.info(f"Received message: {json.dumps(message, indent=4)}")
def listen_for_updates(self):
self.setup_connection()
self.declare_queue()
self.channel.basic_consume(queue=self.queue, on_message_callback=self.callback, auto_ack=True)
logging.info("Waiting for updates...")
self.channel.start_consuming()
if __name__ == "__main__":
config = Config()
listener = RabbitMQListener(config)
listener.listen_for_updates()
Consmer configuration guidelines
-
Consumers should maintain long-lived connections and avoid frequently opening and closing them. It's recommended to configure a heartbeat interval of 20-30 seconds on the RabbitMQ connection. Please refer to your client library documentation for instructions on setting this up.
-
Messages in outbox queues have a time-to-live (TTL) of 30 minutes, meaning that any unconsumed messages will be discarded after this period. Consequently, when a consumer connects for the first time, they might receive messages from within this 30-minute window.