This is the frontend for the Chat Application, built with Next.js 16 (App Router), React 19, and TailwindCSS. It features a responsive UI, real-time updates via Socket.IO, and a robust state management system.
- Framework: Next.js 16 (App Router), React 19
- Language: TypeScript
- Styling: TailwindCSS, Shadcn/UI, Lucide React (Icons), TW Animate CSS
- State Management: React Context
- Real-time: Socket.IO Client
- Validation: Zod
- Utilities: Cookies-next, Class Variance Authority, CLSX, Tailwind Merge
- Linting/Formatting: ESLint, Prettier
The project follows the Next.js App Router structure:
-
app/: Contains the application routes and layouts.
- (auth)/: Authentication routes (login, signup, etc.).
- chat/: The main chat interface.
-
components/: Reusable UI components.
- ui/: Shadcn/UI primitives.
- Other/: Other components.
-
context/: React Context providers (e.g.,
ChatProvider). -
hooks/: Custom React hooks.
-
middlewares/: Custom middleware logic for route protection.
-
types/: TypeScript type definitions.
-
utils/: Utility functions.
-
validation/: Zod schemas for form validation.
-
socket.ts: Socket.IO client initialization.
- Prerequisites: Node.js installed.
- Clone the Repository:
git clone https://github.com/Eng1Mahmoud/frontend-chat-app.git cd frontend-chat-app - Install Dependencies:
npm install
- Environment Variables: Create a
.envfile in the root directory:NEXT_PUBLIC_API_URL=http://localhost:4000/api NEXT_PUBLIC_SOCKET_URL=http://localhost:4000
- Run Development Server:
npm run dev
- Build for Production:
npm run build npm start
ChatContent.tsx: The core component that orchestrates the chat view. It manages the layout between the header, message list, and input area.ChatHeader.tsx: Displays the active user's details (avatar, name, status) and typing indicators.MessagesList.tsx: Renders the scrollable list of messages, grouping them by date.SendMessage.tsx: The input component for composing and sending messages. Handles typing events.FindFriends.tsx: A mobile-only toggle to open the user sidebar.ChatWelcom.tsx: The landing view shown when no conversation is selected.
UserListUI.tsx: The main sidebar container that handles user search and list rendering.UserList.tsx: Renders individual user items in the list, displaying unread message counts.Avatar.tsx: (components/Avatar.tsx) A reusable avatar component that displays user initials or profile pictures.
AuthInput.tsx: Reusable input field with validation error display.AuthButton.tsx: Submit button with loading state support.AuthHeader.tsx: Header component for auth forms (Login/Signup titles).AuthFooter.tsx: Footer component for auth forms (links to other auth pages).AuthAlert.tsx: Displays error or success messages.
SocketController.tsx: A headless component that manages global socket event listeners (e.g.,receive_message,user_online,user_typing) and updates the global state accordingly.ChatProvider.tsx: A React Context provider that exposes global state likelogedinUser,selectedUserForChat, andonlineUsers.
Used for application-wide state that needs to be accessible by many components:
logedinUser: The currently authenticated user.selectedUserForChat: The user currently selected in the sidebar.onlineUsers: A Set of user IDs representing currently online users.unreadCounts: Tracks the number of unread messages for each user.
The application uses Zod for schema validation, primarily for authentication forms.
signup.ts: Validates registration data (username, email, password).login.ts: Validates login credentials.forgotPassword.ts: Validates email for password reset requests.resetPassword.ts: Validates new password input.
The application uses a custom middleware implementation in middlewares/ to protect routes.
authMiddleware.ts: Checks for the presence of atokencookie.- Role-Based Access: Routes are defined with allowed roles (
guest,user).guestroutes (e.g., login, signup) are accessible without a token.userroutes (e.g.,/chat) require a valid token.- Unauthorized access redirects the user to the appropriate page (e.g., home or login).
useChatMessages: Manages fetching and storing messages for the active conversation. It handles loading states and appending new real-time messages.useChatScroll: Automatically scrolls the chat container to the bottom when new messages arrive.use-mobile: Detects if the user is on a mobile device to adjust the UI (e.g., sidebar behavior).
The application uses Next.js App Router:
/login: User login page./signup: User registration page./verify-account: Email verification page./forgot-password: Request password reset./reset-password: Reset password page./chat: The main protected route for the chat application.
The socket connection is initialized in socket.ts as a singleton.
- Connection: Auto-connects when a user is authenticated (token present).
| Event | Payload | Description |
|---|---|---|
send_message |
{ receiverId, text } |
Send a new message. |
user_typing |
{ receiverId } |
Notify that user is typing. |
user_stopped_typing |
{ receiverId } |
Notify that user stopped typing. |
mark_as_read |
{ senderId } |
Mark messages from a sender as read. |
| Event | Payload | Description |
|---|---|---|
user_online |
userId |
Receive notification when a user comes online. |
online_users |
[userIds] |
Receive list of currently online users upon connection. |
unread_counts |
{ userId: count } |
Receive initial unread message counts upon connection. |
receive_message |
Message object |
Receive a new message. |
user_typing |
{ userId } |
Receive notification that a user is typing. |
user_stopped_typing |
{ userId } |
Receive notification that a user stopped typing. |
messages_read_update |
{ receiverId, status: 'read' } |
Receive notification that sent messages were read. |
unread_count_update |
{ userId, count } |
Receive update for unread message count. |
user_offline |
userId |
Receive notification when a user goes offline. |
apiFetch.ts: A wrapper around thefetchAPI to handle headers and base URLs consistently.generateAvatarColor.ts: Generates consistent colors for user avatars based on their name.