A simple CRUD API with Redis caching, RabbitMQ Queuing, Message broadcasting using Websocket and Server Sent Events
- Node.js
- Express
- PostgreSQL
- Prisma
- Docker
- Redis
- RabbitMQ
- Websocket
- Node.js
- Docker
-
Clone the respository
git clone https://github.com/RCOM363/health-record-api cd health-record-api
-
Environment variable setup
Create an
.env
file in root directory & configure the following variablesPORT=3000 CORS_ORIGIN=* DATABASE_URL=<postgreSQL_DB_url> REDIS_URL="redis://redis:6379" // docker url RABBITMQ_URL="amqp://guest:guest@rabbitmq:5672" // docker url TOKEN_SECRET=<secret>
-
Setup Prisma Client
npx prisma generate npx prisma migrate dev npx prisma db push
-
Run the server
docker-compose up health-record-api
NOTE
: You will get connection error with RabbitMQ but it will connect after few tries
- URL :
/api/v1/auth/login
- Method :
POST
- Body :
{ "email": "[email protected]", "password": "pass#123" }
curl
command :curl -X POST http://localhost:PORT/api/v1/auth/login \ -H "Content-Type: application/json" \ -d '{"email": "[email protected]","password": "pass#123"}'
- URL :
/api/v1/records
- Method :
POST
- Body :
{ "name": "John Doe", "age": 25, "status": "Healthy" }
curl
command :curl -X POST http://localhost:PORT/api/v1/records \ -H "Content-Type: application/json" \ -d '{"name": "John Doe", "age": "25", "status": "Healthy"}'
- URL :
/api/v1/records/:id
- Method :
GET
curl
command :curl http://localhost:PORT/api/v1/records/{id}
- URL :
/api/v1/records/:id
- Method :
PUT
- Body :
{ "age": 20 }
curl
command :curl -X POST http://localhost:PORT/api/v1/records/{id} \ -H "Content-Type: application/json" \ -d '{"age": "20"}'
- URL :
/api/v1/records/:id
- Method :
DELETE
curl
command :curl -X DELETE http://localhost:PORT/api/v1/records/{id}
Connect to the websocket server
- Endpoint :
http://localhost:PORT/ws
- Use
https://hoppscotch.io/realtime/websocket
to test and addws://localhost:PORT/ws
as URL and hit connect
Connect to the SSE at
- Endpoint :
http://localhost:PORT/sse/health-updates
- Use
curl
to testcurl http://localhost:PORT/sse/health-updates
An HTML file (public/test.html
) is included to manually test all API endpoints and real-time functionalities using WebSockets and SSE.
- Organized the code into separates
modules
(models, controllers, routes etc). - Single server to handle
API
,Websocket
andSSE
. - Created a separate Notification worker to consume the messages from the queue, keeping the processes decoupled.
- Used docker to isolate environments for
Node.js server
,Redis
,RabbitMQ
andWorker
, each run in their own container.