A comprehensive chat application project demonstrating two different real-time communication approaches: Long-Polling and WebSockets. Both implementations provide a simple yet functional chat interface built with HTML, JavaScript, and Express.js.
Chat-App/
├── package.json # Root project configuration
├── README # This file
├── Long-Polling/ # Long-polling implementation
│ ├── backend/
│ │ └── server.js # Express server with long-polling endpoints
│ └── frontend/
│ ├── index.html # Chat interface
│ └── script.js # Client-side script
└── WebSockets/ # WebSocket implementation
├── package.json # Backend dependencies
├── backend/
│ ├── package.json # Detailed backend configuration
│ └── sever.js # Express server with WebSocket support
└── frontend/
├── index.html # Chat interface
└── script.js # Client-side WebSocket script
- Traditional HTTP-based real-time communication
- Server holds requests open to deliver new messages
- Simpler setup with no additional WebSocket dependencies
- Best for: Older browsers or restricted network environments
- Full-duplex real-time communication
- Interactive features including message reactions (like/dislike)
- More efficient bandwidth usage
- Best for: Modern browsers and real-time interactive applications
- Node.js (v14 or higher)
- npm (Node Package Manager)
cd Long-Polling/backend
npm install # Install: express, cors
node server.jsThe server will start at http://localhost:3000. Open the frontend at Long-Polling/frontend/index.html in your browser.
cd WebSockets/backend
npm install # Install: express, cors, ws
node sever.jsThe server will start at http://localhost:3000. Open the frontend at WebSockets/frontend/index.html in your browser.
Note: Both implementations use port 3000. Ensure only one server is running at a time.
- express (^5.1.0) - Web server framework
- cors (^2.8.5) - Cross-Origin Resource Sharing middleware
- ws (^8.18.3) - WebSocket implementation for Node.js
| Method | Endpoint | Description |
|---|---|---|
| GET | /messages |
Retrieves all messages (long-polling, 30s timeout) |
| POST | /messages |
Sends a new message |
| Method | Endpoint | Description |
|---|---|---|
| GET | /messages |
Retrieves all stored messages |
| POST | /messages |
Sends a new message (broadcasts via WebSocket) |
| POST | /like |
Adds a like to a message |
| POST | /dislike |
Adds a dislike to a message |
Messages are stored with the following structure:
{
"text": "Message content",
"timestamp": "2024-01-15T10:30:00.000Z",
"likes": 0,
"dislikes": 0
}Note: The Long-Polling implementation uses a simplified format (text and timestamp only).
- Client sends GET request to
/messages - Server holds the connection open (up to 30 seconds)
- When a new message arrives, server responds immediately to all waiting clients
- Client receives the message and displays it
- Client automatically reconnects for the next message
- Client establishes a persistent WebSocket connection
- When a message is sent, it's posted via HTTP POST
- Server broadcasts the message to all connected WebSocket clients
- Clients receive the message in real-time and display it
- Connection remains open for the duration of the session
- Open the chat interface in your browser
- Type a message in the input field
- Click "Send" or press Enter
- Messages appear in the chat window in real-time
- Like (👍): Click the like button to add a reaction
- Dislike (👎): Click the dislike button to add a negative reaction
- Reactions are updated in real-time across all connected clients
- Timeout: 30 seconds per request
- Storage: In-memory array
- Scalability: Limited (not recommended for production)
- Resource Usage: Higher (creates many open connections)
- Connection: Persistent bi-directional
- Storage: In-memory array
- Scalability: Better than long-polling
- Resource Usage: More efficient for continuous communication
- In-Memory Storage: All messages are lost when the server restarts
- Single Server: Designed to run on a single server (no horizontal scaling)
- No Authentication: No user identification or access control
- Typo Alert: WebSocket backend file is named
sever.js(should beserver.js)
- Add persistent database (MongoDB, PostgreSQL)
- Implement user authentication and identification
- Add message editing and deletion
- Implement room-based chat
- Add file sharing capabilities
- Deploy to production with scaling considerations
- Fix typo in WebSocket backend filename
- Add unit and integration tests
- Implement error handling and validation