Skip to content

MSyedJubair/NexTalk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

24 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

NexTalk - Real-time Chat Application

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

๐Ÿ“‹ Table of Contents


โœจ Features

Core Features

  • ๐Ÿ” 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

Technical Highlights

  • 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

๐Ÿ›  Tech Stack

Frontend

  • 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

Backend

  • 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

๐Ÿ“ Project Structure

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

๐Ÿš€ Installation & Setup

Prerequisites

  • Node.js (v18 or higher)
  • npm or yarn
  • MongoDB instance (local or cloud - MongoDB Atlas)
  • Cloudinary account (for image uploads)

Step 1: Clone the Repository

git clone <repository-url>
cd NexTalk

Step 2: Install Backend Dependencies

cd Server
npm install

Step 3: Install Frontend Dependencies

cd ../Client
npm install

๐Ÿ” Environment Variables

Backend (.env)

Create 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_secret

Frontend (.env.local)

Create a .env.local file in the Client directory:

# Backend API
VITE_API_URL=http://localhost:5000/api
VITE_SOCKET_BACKEND_URI=http://localhost:5000

โ–ถ๏ธ Running the Application

Development Mode

Terminal 1 - Start Backend Server:

cd Server
npm run dev

Server will run on http://localhost:5000

Terminal 2 - Start Frontend Development Server:

cd Client
npm run dev

Frontend will run on http://localhost:5173

Production Build

Backend:

cd Server
npm run build
npm start

Frontend:

cd Client
npm run build
npm run preview

๐Ÿ“ฑ Usage

  1. Open the application at http://localhost:5173
  2. Create an account - Sign up with email and password
  3. Set a profile picture - Upload your avatar via Cloudinary
  4. Browse users - View all registered users in the sidebar
  5. Start chatting - Click on any user to open chat and send messages
  6. See online status - Online users are highlighted and see real-time updates

Chat Features

  • 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

๐Ÿ”Œ Socket Events

Client โ†’ Server

Event Data Description
join userId: string User joins their room when logging in
sendMessage { senderId, receiverId, text, image } Send a message to another user

Server โ†’ Client

Event Data Description
onlineUsers string[] List of currently online user IDs
receiveMessage { senderId, receiverId, text, image, timestamp, _id } Receive a message

๐Ÿ“ก API Endpoints

Authentication Routes (/api/auth)

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 โœ…

User Routes (/api/users)

Method Endpoint Description Auth Required
GET / Get all users โœ…
GET /:id Get specific user details โœ…

Message Routes (/api/message)

Method Endpoint Description Auth Required
GET /:userId Get message history with user โœ…
POST /send Send a message (REST fallback) โœ…

๐ŸŽฏ Key Learning Points (Socket.io)

This project demonstrates:

  1. Socket.io Fundamentals

    • Creating WebSocket connections
    • Emitting and listening to events
    • Using rooms for private messaging
  2. Real-time Features

    • Broadcasting online status to all users
    • Sending messages to specific users using rooms
    • Handling disconnections gracefully
  3. 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
  4. Best Practices

    • Error handling for disconnections
    • CORS configuration for cross-origin requests
    • Type-safe Socket.io events with TypeScript declarations

๐Ÿ› Troubleshooting

Connection refused / Cannot connect to server

  • Ensure backend is running on the correct port
  • Check VITE_SOCKET_BACKEND_URI matches backend URL
  • Verify CORS is properly configured

Messages not sending

  • Check browser console for errors
  • Verify user is authenticated (JWT token in cookies)
  • Ensure Socket.io connection is active

MongoDB connection errors

  • Verify MONGODB_URI is correct
  • Check network access in MongoDB Atlas
  • Ensure database username and password are correct

Image upload issues

  • Verify Cloudinary credentials in .env
  • Check image file size limits
  • Ensure Cloudinary API key has upload permissions

๐Ÿ“ License

ISC


๐Ÿค Contributing

This is a learning project. Feel free to fork, modify, and improve!


๐Ÿ“ž Support

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! ๐Ÿš€

About

A modern, full-stack real-time chat application built with React, Express.js, and Socket.io.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

โšก