How veetmoradiya3628/cosketch works
Project type and purpose
• This is a full-stack, real-time, browser-based collaborative whiteboard built on the MERN stack.
• Frontend: React + Redux + Rough.js for drawing, communicates over REST and Socket.IO.
• Backend: Node.js + Express, exposing REST endpoints and Socket.IO events; MongoDB as the data store; JWT-based auth; role-based access; scheduled jobs via a cron utility.
Main components
a. Client (Browser)
‑ React application
‑ Redux (whiteboardSlice, cursorSlice)
‑ Rough.js drawing layer
‑ API service module (REST calls to /auth, /boards, /users)
‑ Socket.IO client (socketConn.js)
‑ UI components (Board list, Whiteboard canvas, Modals, Navbar)
b. API Layer (Express Server)
‑ Entry point: server/index.js
‑ Middlewares: authMiddlewares.js (JWT validation), input validation
‑ REST Controllers:
• authController.js (login, registration)
• boardController.js (create/read/update/delete boards)
• boardElementController.js (CRUD on whiteboard elements, export endpoints)
• userController.js (profile, listing)
‑ Routes: authRoutes.js, boardRoutes.js, userRoutes.js
c. Real-time Layer (Socket.IO)
‑ socketHandler.js (initial handshake, join/leave room)
‑ boardDataHandler.js (sync initial board state upon connect)
‑ userSocketDataStore.js (in-memory tracking of active users per board)
d. Persistence Layer
‑ MongoDB collections:
• Users (userModel.js)
• Boards (boardModel.js)
• BoardContents (boardContentModel.js)
‑ Utils: jwtUtils.js (token creation/verification), cronjobs.js (scheduled tasks, e.g., cleanup or archiving)
e. Optional/Support
‑ Swagger UI (swagger.js) for API docs
‑ Static hosting (frontend/public/)
‑ Environment configuration (.env)
Interactions and data flows
• On page load, React → REST GET /boards ← Express → MongoDB
• User selects a board → REST GET /boards/:id/content → server retrieves boardContents
• React initializes Socket.IO, emits “join_board” → socketHandler authenticates via JWT → subscribes to a Socket.IO “room”
• As one user draws: React emits “draw_element” event → socketEventHandler receives, updates boardContents in MongoDB, broadcasts “element_added” to other clients
• Cursor movements: React emits “cursor_move” → server broadcasts to other clients via cursorSlice updates
• REST writes (e.g., create board, add member) flow through controllers → models → MongoDB
Architectural patterns and design principles
• Layered architecture: UI layer, API/Controller layer, Business logic (handlers, services), Persistence layer
• Event-driven real-time collaboration with Socket.IO
• MVC-style separation on backend (Routes → Controllers → Models)
• Centralized state management on frontend (Redux)
• JWT-based stateless auth for REST; token handshake for web sockets
• Role-based access control within middleware