A simple real-time 1-1 chat demo built with Express, Handlebars, MongoDB, and Socket.IO.
This project demonstrates:
- User authentication (register/login/logout) with sessions
- Real-time message delivery using WebSocket transport via Socket.IO
- Conversation-based private rooms between two users
- Message persistence in MongoDB
- Node.js
- Express
- Express Handlebars
- Socket.IO
- MongoDB + Mongoose
- express-session + cookie-parser
- bcrypt
- express-validator
index.js: app bootstrap, middleware setup, route mounting, Socket.IO serverconfig/database.js: MongoDB connectionrouters/user.js: authentication and home routesrouters/chat.js: chat room route and conversation loading/creationmidlewares/Auth.js: auth guards (authorize,isLogin)model/user.js: user schemamodel/conversation.js: conversation schema (pair of users)model/messages.js: message schemaviews/: Handlebars templates (login/register/index/chat)public/: static assets (CSS/JS/images)
- Register account with validation
- Login with hashed password check
- Session-based access control
- User list (except current user)
- Open chat with selected user (
/chat/:id) - Existing conversation reuse or auto-create new one
- Load historical messages for a room
- Send and receive real-time messages in-room
- User opens
/chat/:id. - Server finds or creates a
Conversationbetween current user and receiver. - Server renders chat page with
roomidanduseridin HTML data attributes. - Browser creates Socket.IO client and emits
joinRoom(roomid). - On send, browser emits
chatevent with payload:usermessageroomtime
- Server saves message to MongoDB.
- Server emits
messageto other sockets in the same room. - Receiver appends incoming message immediately without page refresh.
fullname: Stringemail: String(unique)password: String(bcrypt hash)
user1: Stringuser2: String
user: String(sender id)message: Stringroom: String(conversation id)time: Date
- Node.js 16+ (recommended)
- MongoDB running locally on default port
Default Mongo connection in project:
mongodb://localhost:27017/GKWebNC
npm installnpm startThe app runs at:
http://localhost:3000
- Open
http://localhost:3000 - Register two accounts
- Login with account A
- Open chat with account B from user list
- Login account B in another browser/incognito window
- Chat in real time
joinRoom(roomid): join a conversation roomchat(payload): send a message
joined(message): join acknowledgmentmessage(payload): incoming message from another user in room
npm start: run app with nodemon (nodemon --inspect index.js)
package.jsoncurrently includes bothexpressand typo packageepxress.- Session config is minimal; production should include a strong session secret and secure cookie settings.
- Message rendering currently uses direct HTML insertion on client side; sanitize or render as text to avoid XSS.
- No automated tests are included.
- Add environment variables (
PORT,MONGO_URI,SESSION_SECRET) - Add validation for empty messages on both client and server
- Add unique conversation constraint to prevent duplicate rooms
- Add pagination for long chat history
- Add online/offline user presence
- Add unit/integration tests
ISC