A Node.js microservice for queuing and processing notifications (email, SMS, in-app) using RabbitMQ and MongoDB.
- Queue notifications via REST API.
- Batch or single notification support.
- Priority & category determined by message content.
- RabbitMQ for async messaging.
- MongoDB for persistent storage.
- Extensible: Add new notification types easily.
- user/resgister : Register a new user.
- user/:username : Get user profile info by username.
- user/:username/notifications : Retrieve all notifications for a given user.
- /notifications : Queue one or more notifications for processing (single or batch).
Install and run the following:
-
MongoDB
-
RabbitMQ
git clone https://github.com/your-username/notification-service.git
cd notification-servicenpm installCreate a .env file in the root with the following:
# MongoDB connection string (required)
CONNECTION_STRING=mongodb://localhost:27812/Notification-Service
# RabbitMQ connection string (required)
RABBITMQ_URL=amqp://localhost
# Application port (optional)
PORT=4001npm run devnode index.jsIn a separate terminal:
npm run rabbitmqnode worker/consumer.jsQueue one or more notifications.
{
"username": "arup04",
"type": "email",
"message": "Your OTP is 123456"
}[
{ "username": "arup04", "type": "in-app", "message": "You got a like!" },
{ "username": "arup04", "type": "sms", "message": "Your balance is low." }
]{
"message": "Notifications processed",
"results": [
{
"success": true,
"queue": "notification_in-app",
"username": "arup04",
"type": "in-app"
},
{
"success": true,
"queue": "notification_sms",
"username": "arup04",
"type": "sms"
}
]
}typemust be one of:email,sms,in-app- Each notification must include:
username,type,message
Notification priority is auto-detected based on keywords in the message:
| Keyword(s) | Priority | Level |
|---|---|---|
otp, verify, code |
1 | High |
payment, bill, txn |
2 | Medium |
| Anything else | 3 | Low |
# MongoDB
CONNECTION_STRING=mongodb://localhost:27017/notificationdb
# RabbitMQ
RABBITMQ_URL=amqp://localhost
# Optional server port
PORT=4001
├── config/
│ └── dbconnection.js
├── controller/
│ ├── notificationController.js
│ └── userController.js
├── middleware/
│ └── errorHandler.js
├── routes/
│ ├── notificationRoutes.js
│ └── userRoutes.js
├── schema/
│ ├── notificationSchema.js
│ └── userSchema.js
├── utils/
│ └── rabbitmq.js
├── worker/
│ └── consumer.js
├── .gitignore
├── constants.js
├── index.js
├── package-lock.json
├── package.json
└── README.md
- Ensure MongoDB and RabbitMQ are running before starting the app.
- Double-check your
.envfile values. - Use tools like Postman or curl to test the API.
- Watch terminal logs for any errors.