A modern, scalable real-time chat application built with React (TypeScript) and Node.js, featuring Socket.io for real-time communication. This application uses in-memory storage for simplicity and can be easily extended with database integration.
- Real-time messaging with Socket.io
- Multiple chat rooms support
- Typing indicators to show when users are composing messages
- Online users list with real-time updates
- Message history for each room (last 100 messages)
- Responsive design that works on desktop and mobile
- Modern UI with TailwindCSS
- TypeScript for better development experience
- In-memory storage (no database required)
- Node.js - JavaScript runtime
- Express.js - Web framework
- Socket.io - Real-time communication
- CORS - Cross-origin resource sharing
- UUID - Unique identifier generation
- React 18 - UI library
- TypeScript - Static type checking
- Vite - Build tool and development server
- TailwindCSS - Utility-first CSS framework
- Socket.io-client - Real-time client communication
Real-Time-Chat/
βββ backend/
β βββ package.json
β βββ server.js # Express + Socket.io server
βββ frontend/
β βββ public/
β βββ src/
β β βββ components/
β β β βββ Login.tsx # User authentication & room selection
β β β βββ ChatRoom.tsx # Main chat interface
β β β βββ MessageList.tsx # Message display with grouping
β β β βββ MessageInput.tsx # Message composition
β β β βββ TypingIndicator.tsx # Typing status display
β β β βββ OnlineUsers.tsx # Online users sidebar
β β βββ services/
β β β βββ socketService.ts # Socket.io client wrapper
β β βββ App.tsx # Main application component
β β βββ main.tsx # Application entry point
β β βββ index.css # Global styles with TailwindCSS
β βββ package.json
β βββ vite.config.ts
β βββ tailwind.config.js
β βββ tsconfig.json
βββ README.md
βββ LICENSE
- Node.js (v16 or higher)
- npm or yarn
-
Clone the repository
git clone <repository-url> cd Real-Time-Chat
-
Setup Backend
cd backend npm install npm startThe backend server will start on
http://localhost:3001 -
Setup Frontend (in a new terminal)
cd frontend npm install npm run devThe frontend will start on
http://localhost:5173 -
Open your browser
Navigate to
http://localhost:5173and start chatting!
For development with auto-reload:
Backend:
cd backend
npm run dev # Uses nodemon for auto-restartFrontend:
cd frontend
npm run dev # Vite dev server with hot reloadThe server can be configured through environment variables:
PORT=3001 # Server port (default: 3001)Update the Socket.io server URL in src/services/socketService.ts:
constructor(serverUrl: string = 'http://localhost:3001') {
this.serverUrl = serverUrl;
}| Event | Payload | Description |
|---|---|---|
joinRoom |
{ username: string, room: string } |
Join a chat room |
leaveRoom |
{} |
Leave current room |
sendMessage |
{ message: string } |
Send a message |
typing |
{ isTyping: boolean } |
Toggle typing indicator |
| Event | Payload | Description |
|---|---|---|
message |
Message |
New message received |
roomHistory |
Message[] |
Chat history when joining |
onlineUsers |
User[] |
Updated online users list |
typing |
{ users: User[] } |
Users currently typing |
leftRoom |
{} |
Confirmation of leaving room |
error |
{ message: string } |
Error messages |
interface User {
id: string;
username: string;
room: string;
socketId: string;
}
interface Message {
id: string;
username: string;
message: string;
timestamp: string;
type: 'user' | 'system';
}| Method | Endpoint | Description |
|---|---|---|
GET |
/health |
Server health check with statistics |
- Username validation (2-20 characters, alphanumeric + spaces)
- Room name input with suggestions
- Form validation with error handling
- Loading states during connection
- Header with room name and user info
- Message list with auto-scroll
- Real-time typing indicators
- Online users sidebar (desktop) / modal (mobile)
- Responsive design for all screen sizes
- Message grouping for better readability
- Timestamp display
- User/system message differentiation
- Auto-scroll to new messages
- Empty state handling
- Auto-resizing textarea
- Send on Enter, new line on Shift+Enter
- Typing indicator with debouncing
- Character counter for long messages
- Disabled state handling
const storage = {
users: new Map(), // userId β User object
rooms: new Map(), // roomName β { users: Set(), messages: [] }
sockets: new Map(), // socketId β userId
typingUsers: new Map() // roomName β Set(userIds)
};- Message Limit: Only the last 100 messages per room are stored
- Automatic Cleanup: Users are removed from storage on disconnect
- Typing Timeout: Typing indicators auto-clear after 2 seconds
- Connection Management: Proper cleanup on socket disconnection
The application is fully responsive with:
- Mobile-first approach using TailwindCSS
- Adaptive layouts for different screen sizes
- Touch-friendly interfaces for mobile devices
- Collapsible sidebar on smaller screens
- Optimized message bubbles for readability
- JWT-based authentication
- User registration and profiles
- Private messaging
- Message encryption
- Rate limiting and spam protection
- MongoDB/PostgreSQL for persistent storage
- Message history pagination
- User preferences and settings
- File upload and sharing
- Message search functionality
- Redis for session management
- Horizontal scaling with multiple server instances
- Load balancing
- CDN integration for assets
- Caching strategies
- Voice and video calling
- Screen sharing
- Rich text formatting
- Emoji reactions
- Message threads and replies
- Push notifications
- Dark mode theme
- Multiple language support
- Docker containerization
- CI/CD pipeline
- Environment-specific configs
- Monitoring and logging
- Health checks and alerts
-
Connection refused
- Ensure backend server is running on port 3001
- Check firewall settings
- Verify CORS configuration
-
Messages not appearing
- Check browser console for JavaScript errors
- Verify Socket.io connection status
- Ensure proper event handling
-
Styling issues
- Verify TailwindCSS is properly configured
- Check for conflicting CSS rules
- Ensure proper class names are used
- Use browser developer tools to monitor Socket.io events
- Check server logs for connection and error messages
- Test with multiple browser tabs to simulate multiple users
- Use React Developer Tools for component debugging
Developed by: Danuja Adikari
Email: danujadikari2001@gmail.com
GitHub: @danujaadikari
This project is licensed under the MIT License - see the LICENSE file for details.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
If you encounter any issues or have questions:
- Check the troubleshooting section above
- Search existing issues in the repository
- Create a new issue with detailed information
- Include error messages and reproduction steps
Happy Chatting! π