Anansi is a backend service designed to send real-time notifications to users via WebSockets. It supports user authentication and delivery/read receipts, ensuring that notifications are sent and acknowledged. The service uses MongoDB for persistent storage and Redis for caching and managing WebSocket connections.
- User registration and login with JWT authentication
- Real-time notifications via WebSockets
- Delivery and read receipts
- Node.js
- Express.js
- WebSocket
- MongoDB
- Redis
- JWT (JSON Web Tokens)
- Mongoose
- Bcrypt
Note: This project emphasizes the functionality of WebSocket notifications rather than the overall API architecture.
The architecture follows a service-oriented approach with the following components:
- Controllers: Handle HTTP-specific logic and delegate business logic to services.
- Services: Encapsulate business logic and can be reused across different parts of the application.
- Models: Define the structure of data in MongoDB using Mongoose.
- WebSocketService: Manages WebSocket connections, message broadcasting, and receipt handling.
- RedisService: Manages caching using Redis.
- EventBus: Decouples services by using an event-driven approach.
- Node.js (>= 12.x)
- MongoDB
- Redis
- Docker (for running MongoDB and Redis in containers)
-
Clone the repository:
git clone https://github.com/yourusername/anansi.git cd anansi
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory and configure the environment variables:PORT=4000 WS_PORT=8080 MONGODB_URI=mongodb://localhost:27021/notification_service JWT_SECRET=your_jwt_secret REDIS_URL=redis://localhost:6378
-
Using Docker
If you prefer to run MongoDB and Redis using Docker, create a
docker-compose.yml
file in the root directory with the following content:Create a
.env
file to specify the Docker volume paths:MONGO_DATA=./data/mongo MONGO_DUMP=./data/dump REDIS_DATA=./data/redis
Start the MongoDB and Redis containers:
docker-compose up -d
-
Start the application:
npm start
-
Register:
POST /api/register
- Request Body:
{ "username": "string", "password": "string" }
- Response:
{ "success": true, "message": "User registered" }
- Request Body:
-
Login:
POST /api/login
- Request Body:
{ "username": "string", "password": "string" }
- Response:
{ "success": true, "token": "jwt_token" }
- Request Body:
-
Send Notification:
POST /api/notify
- Request Body:
{ "userId": "string", "message": "string" }
- Response:
{ "success": true, "message": "Notification sent" }
- Request Body:
-
Fetch Notifications:
GET /api/notifications/:userId
- Response:
[ { "id": "string", "message": "string", "status": "string", "read": "boolean" } ]
- Response:
You can find a Postman collection to test the API endpoints at the following link:
Anansi focuses on providing real-time WebSocket notifications with support for user authentication and delivery/read receipts. The project is designed to be decoupled, with an emphasis on WebSocket functionality over overall API architecture.