This project is a Node.js + Express backend that provides a REST API for managing dishes and supports real-time updates via Socket.IO and MongoDB Change Streams.
It is designed as a reference implementation for creating real-time apps where clients automatically receive updates when data changes in the database.
✅ Fetch the list of dishes
✅ Toggle the published status of a dish
✅ Real-time broadcasting of updates to all connected clients
✅ MongoDB Change Streams for efficient event detection
- Node.js
- Express.js
- MongoDB Atlas
- Mongoose
- Socket.IO
📁 models/
dish.js # Mongoose model for dishes
server.js # Main application entry point
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/dishes |
Retrieve all dishes |
| PATCH | /api/dishes/:id/toggle |
Toggle the published status |
This backend uses MongoDB Change Streams to listen for changes in the dishes collection. When a dish is updated (e.g., published/unpublished), an event (dishUpdated) is emitted to all connected Socket.IO clients, who can then re-fetch data or update their UI automatically.
git clone https://github.com/surefire01/rt-updates-api.git
cd rt-updates-apinpm installCreate a .env file in the project root:
MONGODB_URI=mongodb+srv://<username>:<password>@cluster0.mongodb.net/your-database-name
PORT=8000
Make sure your MongoDB Atlas cluster is whitelisted for your IP.
npm run devor
node server.js✅ Server should start on http://localhost:8000.
Fetch dishes
GET /api/dishesToggle publish status
PATCH /api/dishes/64f3f1234abcdef12345678Server emits:
| Event Name | Payload | Description |
|---|---|---|
dishUpdated |
(none) | Indicates a dish was changed |
Clients should listen for dishUpdated and re-fetch the dishes list.
-
Connect to the backend via Socket.IO:
const socket = io("http://localhost:8000");
-
Listen for updates:
socket.on("dishUpdated", () => { // Re-fetch dishes or update UI });
-
Make REST calls to toggle or retrieve dishes.
This backend is a great starting point for building real-time apps. You could easily extend it to support:
- Authentication and authorization
- More granular event payloads (send changed document)
- Additional CRUD operations
- Webhook integrations
- Production deployment (e.g., Render, Heroku)
MIT
