A real-time notification microservice for the Dexbooru platform, built with Bun, TypeScript, RabbitMQ, and MongoDB.
This service handles the ingestion, persistence, and real-time delivery of notifications and events to users. It consumes events from a RabbitMQ message broker, stores relevant persistent data (like friend invites) in MongoDB, and broadcasts events to connected clients via WebSockets.
- Runtime: Bun (v1.3.3+)
- Language: TypeScript
- Database: MongoDB (via Mongoose)
- Message Broker: RabbitMQ
- Validation: Zod
- Testing: Bun Test
- Bun installed.
- Docker & Docker Compose (optional, for running dependencies).
bun installCopy the example environment file and configure it:
cp .env.example .envEnsure your .env file points to valid MongoDB and RabbitMQ instances.
If you don't have local instances of MongoDB and RabbitMQ, you can use the provided docker-compose.yml:
docker-compose up -dDevelopment Mode (with hot reload):
bun run devProduction Build:
bun run build
bun start # or run the generated ./dist/index.jsbun testThe system uses a consumer-based architecture to process events.
- Producers publish events to the
notification_eventsexchange in RabbitMQ. - Consumers (running in this service) subscribe to specific routing keys.
FriendInviteConsumer: Listens for friend invites, validates them, and persists them to MongoDB.NewPostCommentConsumer: Listens for new post comments, validates them, and persists them to MongoDB.GlobalEventConsumer: Listens for all events (event.#) and broadcasts them to relevant WebSocket clients.
- Clients connect via WebSocket to receive real-time updates.
graph TD
subgraph "External Systems"
Publisher[Event Publisher]
end
subgraph "Infrastructure"
RMQ[(RabbitMQ)]
Mongo[(MongoDB)]
end
subgraph "Dexbooru Notifications Service"
FIC[FriendInviteConsumer]
NPCC[NewPostCommentConsumer]
GEC[GlobalEventConsumer]
FIS[FriendInviteService]
NPCS[NewPostCommentService]
WSS[WebSocketService]
API[API Controllers]
end
subgraph "Clients"
User[User Client]
end
Publisher -- Publishes to 'notification_events' --> RMQ
RMQ -- "event.friend_invite.*" --> FIC
RMQ -- "event.new_post_comment.*" --> NPCC
RMQ -- "event.#" --> GEC
FIC -- Validates & Batches --> FIS
FIS -- Persists --> Mongo
NPCC -- Validates & Batches --> NPCS
NPCS -- Persists --> Mongo
GEC -- "Broadcasts" --> WSS
WSS -- "WebSocket Push" --> User
User -- "GET /api/notifications" --> API
API -- "Reads" --> Mongo
src/api: REST API controllers.src/consumers: RabbitMQ message consumers.src/core: Core logic (Middleware, Base classes, DI Container, Logger).src/models: Mongoose models and Zod schemas.src/repositories: Data access layer.src/services: Business logic.tests: Unit and integration tests.
Contributions are welcome! To contribute, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bugfix:
git checkout -b feature/your-feature-name. - Make your changes and ensure they adhere to the project's coding standards.
- Write and run tests to verify your changes:
bun test. - Commit your changes with a descriptive message.
- Push your branch to your fork:
git push origin feature/your-feature-name. - Open a Pull Request against the
mainbranch.
Please ensure that your code follows the existing architectural patterns and that all new features include appropriate unit tests.