Title: Sending Notifications with RabbitMQ
Table of Contents:
- Introduction
- Code
- Code Explanation
- Conclusion
Introduction
In this blog post, we will explore a code snippet that demonstrates sending notifications to users using RabbitMQ. The code utilizes the RabbitMQConnection
, Config
, and RabbitMQProducer
classes to establish a connection to RabbitMQ and publish notifications through different channels based on user preferences.
Code
# Import necessary classes and modules
from connection import RabbitMQConnection
from config import Config
from producers.base import RabbitMQProducer
def send_notification():
# User notifications data
user_notifications = {
"user1": {
"notifications": {"email": True, "sms": False},
"email": "user1@example.com",
"phone": "1234567890",
},
"user2": {
"notifications": {"email": True, "sms": True},
"email": "user2@example.com",
"phone": "9876543210",
},
"user3": {
"notifications": {"email": False, "sms": True},
"email": "user3@example.com",
"phone": "5555555555",
},
}
# Configuration setup
config = Config(override=True)
# Establish RabbitMQ connection
with RabbitMQConnection(host=config.RABBITMQ_HOST, port=config.RABBITMQ_PORT, username=config.RABBITMQ_USER,
password=config.RABBITMQ_PASSWORD) as connection:
producer = RabbitMQProducer(connection)
# Iterate over user notifications
for user_id, user_data in user_notifications.items():
notification = user_data["notifications"]
email = user_data["email"]
phone = user_data["phone"]
# Publish notifications for enabled types
for notification_type, enabled in notification.items():
if enabled:
routing_key = f"notif.{notification_type}"
data = {
"user_id": user_id,
"type": notification_type,
"email": email,
"phone": phone
}
producer.publish_message(exchange=config.EXCHANGE_NAME, routing_key=routing_key, data=data)
print(f"Notification sent to user {user_id} via {notification_type}")
Code Explanation
The code snippet demonstrates the send_notification
function, which sends notifications to users through RabbitMQ. Here's a detailed explanation of the code:
- User Notifications Data
The user_notifications
dictionary contains information about different users and their notification preferences. Each user is represented by a unique ID, and their data includes a dictionary of notification types (email and SMS), along with email and phone contact details.
- Configuration Setup
The config
object is created using the Config
class, which reads configuration values from the environment or uses default values. This allows flexibility in configuring RabbitMQ host, port, username, password, and other settings.
- Establishing Connection and Publishing Notifications
Within a
with
block, aRabbitMQConnection
instance is created, providing the necessary connection parameters from theconfig
object. This ensures the connection is automatically closed when the block is exited.A
RabbitMQProducer
instance is created, passing the established connection as a parameter.The code iterates through the
user_notifications
dictionary, retrieving the user ID, notification preferences, email, and phone details.For each enabled notification type, a routing key is constructed using the format
"notif.{notification_type}"
.A
data
dictionary is created,
containing the user ID, notification type, email, and phone details.
The
producer.publish_message
method is called to publish the notification message to the RabbitMQ exchange. It uses the exchange name from theconfig
object, the constructed routing key, and the data dictionary.A success message is printed, indicating the user ID and the notification type through which the notification was sent.
Conclusion
The code snippet showcases an example of sending notifications using RabbitMQ. By leveraging the RabbitMQConnection
, Config
, and RabbitMQProducer
classes, you can establish a connection to RabbitMQ and publish notifications to different channels based on user preferences. This code provides a foundation for building a robust and flexible notification system within your applications.
Top comments (0)