Notification Service
A microservice for handling and delivering notifications through various channels like SMS and email using RabbitMQ as a message broker.
Overview
This notification service is designed as a standalone microservice that consumes notification requests from a RabbitMQ queue and routes them to the appropriate notification provider based on the notification type. Currently, it supports SMS and email notifications.
Features
- Message consumption from RabbitMQ
- Support for multiple notification channels (SMS, email)
- Extensible architecture for adding new notification types
- Asynchronous notification handling
Architecture
The notification service follows a clean architecture approach:
- Domain Layer: Contains core business logic and port interfaces
- Infrastructure Layer: Implements the ports with concrete adapters
- RabbitMQ: Used as a message broker for consuming notification requests
Installation
Prerequisites
- Go 1.x+
- RabbitMQ server
Setup
- Clone the repository:
git clone https://github.com/JscorpTech/notification.git
cd notification
- Install dependencies:
go mod download
- Build the application:
go build -o notification-service ./cmd/main.go
Configuration
Configure your RabbitMQ connection and other settings in the appropriate configuration files.
Usage
Running the service
./notification-service
This will start the notification consumer that listens for incoming notification requests.
Sending a notification
Notifications should be published to the RabbitMQ exchange with the following JSON format:
{
"type": "email",
"message": "Hello, this is a test notification.",
"to": ["user@example.com"]
}
Python example rabbitmq broker
from kombu import Connection, Exchange, Producer
# RabbitMQ ulanishi
rabbit_url = 'amqp://guest:guest@127.0.0.1:5672/'
connection = Connection(rabbit_url)
channel = connection.channel()
exchange = Exchange('notification', type='direct')
# Producer yaratish
producer = Producer(channel, exchange=exchange, routing_key="notification")
# Xabar yuborish
message = {'type': 'sms', 'message': "classcom.uz sayti va mobil ilovasiga ro'yxatdan o'tishingingiz uchun tasdiqlash kodi: 1234", "to": ["+998888112309", "+998943990509"]}
producer.publish(message)
print("Message sent to all workers!")
Python example redis broker
import redis
import json
# Redis ulanishi
r = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
# Xabar tayyorlash
message = {
'type': 'email',
'message': "Subject: test\r\n\r\nclasscom.uz sayti va mobil ilovasiga ro'yxatdan o'tishingiz uchun tasdiqlash kodi: 1234",
'to': ["JscorpTech@gmail.com", "admin@jscorp.uz"]
}
# Xabarni JSON formatga o‘tkazib, Redis listga push qilish
r.rpush('notification', json.dumps(message))
print("Message pushed to Redis list!")
Available notification types:
email: For email notificationssms: For SMS notifications
Project Structure
notification/
├── cmd/
│ └── main.go # Entry point
├── internal/
│ ├── domain/
│ │ └── ports.go # Interfaces
│ ├── notifier/
│ │ ├── email.go # Email notification implementation
│ │ └── sms.go # SMS notification implementation
│ ├── rabbitmq/
│ │ └── connection.go # RabbitMQ connection handling
│ └── consumer/
│ └── consumer.go # Implementation of the notification consumer
└── README.md
Contributing
- Fork the repository
- Create your feature branch:
git checkout -b feature/my-new-feature - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin feature/my-new-feature - Submit a pull request
License
[Add your license here]
Contact
JscorpTech - GitHub