A modern, full-stack real-time chat application built with React, Express.js, and Socket.io. This project demonstrates real-time bidirectional communication between clients and servers, featuring user authentication, message persistence, and online status tracking.
๐ A learning project to master Socket.io for real-time communication
- Features
- Tech Stack
- Project Structure
- Installation & Setup
- Environment Variables
- Running the Application
- Usage
- Socket Events
- API Endpoints
- ๐ User Authentication - Secure signup/signin with JWT tokens
- ๐ฌ Real-time Messaging - Instant message delivery using Socket.io
- ๐ฅ User Directory - Browse and select users to chat with
- ๐ข Online Status - See which users are currently online
- ๐ Message History - All messages are persisted in the database
- ๐ค User Profiles - Profile pictures with Cloudinary integration
- ๐จ Responsive Design - Beautiful UI with resizable chat panels
- TypeScript for type-safe code
- Real-time bidirectional communication with Socket.io
- JWT-based authentication with secure cookies
- MongoDB for data persistence
- React Query for efficient data fetching
- Tailwind CSS + Shadcn UI for modern styling
- React 19 - UI library
- Vite - Build tool and dev server
- React Router - Client-side routing
- Socket.io-client - Real-time communication
- React Query - Data fetching and caching
- Tailwind CSS - Utility-first CSS framework
- Shadcn UI - High-quality React components
- React Hook Form - Efficient form handling
- Axios - HTTP client
- TypeScript - Type safety
- Node.js - JavaScript runtime
- Express - Web application framework
- Socket.io - Real-time event-based communication
- MongoDB - NoSQL database
- Mongoose - MongoDB object modeling
- JWT (jsonwebtoken) - Token-based authentication
- bcryptjs - Password hashing
- Cloudinary - Image storage and delivery
- TypeScript - Type safety
- Nodemon - Development server with auto-reload
NexTalk/
โโโ Client/ # React Frontend
โ โโโ src/
โ โ โโโ App.tsx # Main app routes
โ โ โโโ main.tsx # Entry point
โ โ โโโ Auth/ # Authentication pages
โ โ โ โโโ AuthLayout.tsx
โ โ โ โโโ Forms/
โ โ โ โโโ Signin.tsx
โ โ โ โโโ Signup.tsx
โ โ โโโ components/
โ โ โ โโโ Shared/ # Reusable components
โ โ โ โ โโโ Avatar.tsx
โ โ โ โ โโโ Loader.tsx
โ โ โ โ โโโ SideBar.tsx # User list sidebar
โ โ โ โ โโโ UserProfile.tsx
โ โ โ โโโ ui/ # Shadcn UI components
โ โ โโโ Root/
โ โ โ โโโ RootLayout.tsx # Main layout with resizable panels
โ โ โ โโโ Pages/
โ โ โ โโโ Home.tsx # Welcome/user selection
โ โ โ โโโ Chat.tsx # Chat interface
โ โ โโโ lib/
โ โ โ โโโ socket.ts # Socket.io client configuration
โ โ โ โโโ axios.ts # Axios instance
โ โ โ โโโ api.ts # API calls
โ โ โ โโโ query.ts # React Query hooks
โ โ โ โโโ utils.ts # Utility functions
โ โ โโโ Types/ # TypeScript interfaces
โ โ โโโ globals.css # Global styles
โ โโโ package.json
โ โโโ tsconfig.json
โ โโโ vite.config.ts
โ โโโ index.html
โ
โโโ Server/ # Express Backend
โโโ src/
โ โโโ index.ts # Server entry point & Socket.io setup
โ โโโ controllers/ # Business logic
โ โ โโโ auth.controller.ts
โ โ โโโ user.controller.ts
โ โ โโโ message.controller.ts
โ โโโ models/ # MongoDB schemas
โ โ โโโ user.model.ts
โ โ โโโ message.model.ts
โ โโโ routes/ # API routes
โ โ โโโ auth.route.ts
โ โ โโโ user.route.ts
โ โ โโโ message.route.ts
โ โโโ middleware/
โ โ โโโ auth.middleware.ts # JWT verification
โ โโโ lib/
โ โ โโโ db.ts # MongoDB connection
โ โ โโโ cloudinary.ts # Image upload configuration
โ โ โโโ utils.ts # Utility functions
โ โโโ types/
โ โโโ express.d.ts # Express type extensions
โโโ package.json
โโโ tsconfig.json
โโโ .env
โโโ .gitignore
- Node.js (v18 or higher)
- npm or yarn
- MongoDB instance (local or cloud - MongoDB Atlas)
- Cloudinary account (for image uploads)
git clone <repository-url>
cd NexTalkcd Server
npm installcd ../Client
npm installCreate a .env file in the Server directory:
# Server Configuration
PORT=5000
NODE_ENV=development
# Database
MONGODB_URI=mongodb+srv://<username>:<password>@<cluster>.mongodb.net/<database>
# JWT
JWT_SECRET=your_jwt_secret_key_here
# Client URL (for CORS)
CLIENT_URL=http://localhost:5173
# Cloudinary (for image uploads)
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secretCreate a .env.local file in the Client directory:
# Backend API
VITE_API_URL=http://localhost:5000/api
VITE_SOCKET_BACKEND_URI=http://localhost:5000Terminal 1 - Start Backend Server:
cd Server
npm run devServer will run on http://localhost:5000
Terminal 2 - Start Frontend Development Server:
cd Client
npm run devFrontend will run on http://localhost:5173
Backend:
cd Server
npm run build
npm startFrontend:
cd Client
npm run build
npm run preview- Open the application at
http://localhost:5173 - Create an account - Sign up with email and password
- Set a profile picture - Upload your avatar via Cloudinary
- Browse users - View all registered users in the sidebar
- Start chatting - Click on any user to open chat and send messages
- See online status - Online users are highlighted and see real-time updates
- Send messages - Type and hit enter or click send
- View message history - All past messages load when opening a chat
- Real-time notifications - Receive messages instantly with Socket.io
- Auto-scroll - Chat automatically scrolls to latest message
- Responsive layout - Resize the sidebar and chat panels as needed
| Event | Data | Description |
|---|---|---|
join |
userId: string |
User joins their room when logging in |
sendMessage |
{ senderId, receiverId, text, image } |
Send a message to another user |
| Event | Data | Description |
|---|---|---|
onlineUsers |
string[] |
List of currently online user IDs |
receiveMessage |
{ senderId, receiverId, text, image, timestamp, _id } |
Receive a message |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /signup |
Create new user account | โ |
| POST | /signin |
Login with credentials | โ |
| POST | /logout |
Logout user | โ |
| PUT | /update-profile |
Update user profile/avatar | โ |
| GET | /check |
Check authentication status | โ |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | / |
Get all users | โ |
| GET | /:id |
Get specific user details | โ |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /:userId |
Get message history with user | โ |
| POST | /send |
Send a message (REST fallback) | โ |
This project demonstrates:
-
Socket.io Fundamentals
- Creating WebSocket connections
- Emitting and listening to events
- Using rooms for private messaging
-
Real-time Features
- Broadcasting online status to all users
- Sending messages to specific users using rooms
- Handling disconnections gracefully
-
Message Flow
- Client emits
sendMessageโ Server saves to DB โ Server emits to both sender and receiver - User authentication tied to Socket.io connections
- Real-time updates without page refresh
- Client emits
-
Best Practices
- Error handling for disconnections
- CORS configuration for cross-origin requests
- Type-safe Socket.io events with TypeScript declarations
- Ensure backend is running on the correct port
- Check
VITE_SOCKET_BACKEND_URImatches backend URL - Verify CORS is properly configured
- Check browser console for errors
- Verify user is authenticated (JWT token in cookies)
- Ensure Socket.io connection is active
- Verify
MONGODB_URIis correct - Check network access in MongoDB Atlas
- Ensure database username and password are correct
- Verify Cloudinary credentials in
.env - Check image file size limits
- Ensure Cloudinary API key has upload permissions
ISC
This is a learning project. Feel free to fork, modify, and improve!
For questions or issues, please open an issue in the repository or refer to the documentation for Socket.io at socket.io/docs.
Happy Coding! ๐