Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

Repository files navigation

Chat Web Demo (Node.js + Express + Socket.IO)

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

Tech Stack

  • Node.js
  • Express
  • Express Handlebars
  • Socket.IO
  • MongoDB + Mongoose
  • express-session + cookie-parser
  • bcrypt
  • express-validator

Project Structure

  • index.js: app bootstrap, middleware setup, route mounting, Socket.IO server
  • config/database.js: MongoDB connection
  • routers/user.js: authentication and home routes
  • routers/chat.js: chat room route and conversation loading/creation
  • midlewares/Auth.js: auth guards (authorize, isLogin)
  • model/user.js: user schema
  • model/conversation.js: conversation schema (pair of users)
  • model/messages.js: message schema
  • views/: Handlebars templates (login/register/index/chat)
  • public/: static assets (CSS/JS/images)

Features

  • 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

How Real-time Chat Works

  1. User opens /chat/:id.
  2. Server finds or creates a Conversation between current user and receiver.
  3. Server renders chat page with roomid and userid in HTML data attributes.
  4. Browser creates Socket.IO client and emits joinRoom(roomid).
  5. On send, browser emits chat event with payload:
    • user
    • message
    • room
    • time
  6. Server saves message to MongoDB.
  7. Server emits message to other sockets in the same room.
  8. Receiver appends incoming message immediately without page refresh.

Data Models

User (model/user.js)

  • fullname: String
  • email: String (unique)
  • password: String (bcrypt hash)

Conversation (model/conversation.js)

  • user1: String
  • user2: String

Message (model/messages.js)

  • user: String (sender id)
  • message: String
  • room: String (conversation id)
  • time: Date

Prerequisites

  • Node.js 16+ (recommended)
  • MongoDB running locally on default port

Default Mongo connection in project:

  • mongodb://localhost:27017/GKWebNC

Installation

npm install

Run

npm start

The app runs at:

  • http://localhost:3000

Typical Usage

  1. Open http://localhost:3000
  2. Register two accounts
  3. Login with account A
  4. Open chat with account B from user list
  5. Login account B in another browser/incognito window
  6. Chat in real time

Socket Events

Client -> Server

  • joinRoom(roomid): join a conversation room
  • chat(payload): send a message

Server -> Client

  • joined(message): join acknowledgment
  • message(payload): incoming message from another user in room

Scripts

  • npm start: run app with nodemon (nodemon --inspect index.js)

Notes and Known Issues

  • package.json currently includes both express and typo package epxress.
  • 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.

Future Improvements

  • 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

License

ISC

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages