- API: Next.js API Routes. A single route,
/api/socket, is used to initialize the Socket.IO server. - WebSocket Server:
Socket.IOis attached to the Next.js HTTP server. It handles client connections and broadcasts events. - Persistence: The board state is stored in a MongoDB database. A singleton connection pattern is used for efficient database access in a serverless environment. The entire board is stored as a single document, which is updated on every change.
- File-based DB vs. Real Database: The project was upgraded from a simple
db.jsonfile to MongoDB. This provides a robust, scalable, and production-ready persistence layer that can safely handle concurrent operations from multiple users.
-
Clone the repository:
git clone https://github.com/your-username/real-time-board.git](https://github.com/Mehboobiqbal-dev/real-time-board cd real-time-board -
Install dependencies:
npm install
-
Set up your Database:
- This project requires a MongoDB database. You can use a local instance or a free cloud-hosted one from MongoDB Atlas.
- Once set up, get your MongoDB Connection String (URI).
-
Configure Environment Variables:
- In the root of the project, create a file named
.env.local. - Add your MongoDB connection details to this file:
MONGODB_URI=your-mongodb-connection-string MONGODB_DB_NAME=your-database-name - Note: The
.env.localfile is included in.gitignoreand should never be committed to your repository.
- In the root of the project, create a file named
-
Run the development server:
npm run dev
-
Open your browser and navigate to
https://real-time-board-pi.vercel.app/. The first time the application runs, it will automatically "seed" your database with initial sample data.