Chat App’s frontend is a React 18 single-page application built with Vite. Global state is split across two React context providers — one for authentication, one for the live socket connection — and a single Zustand store that tracks the active conversation and its messages. Every network call and socket subscription is encapsulated in a custom hook, keeping components thin and easy to reason about. Tailwind CSS and DaisyUI handle styling, whileDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/khushboodaryani/Chat-App/llms.txt
Use this file to discover all available pages before exploring further.
react-hot-toast surfaces transient feedback without cluttering component state.
Source Directory Structure
Routing
React Router v6 powers client-side navigation. There are three routes, and the root route is guarded by the presence ofauthUser from AuthContext.
/login and /signup automatically. Unauthenticated users who try to reach / are sent to /login.
State Management
Chat App composes three independent state primitives, each suited to a different concern.AuthContext
AuthContextProvider wraps the entire application and exposes { authUser, setAuthUser }. Initial state is hydrated from localStorage under the key chat-user, so sessions survive a page refresh without an extra round-trip to the server.
useAuthContext() receives the current user object and a setter. Hooks like useLogin and useLogout call setAuthUser (and update localStorage) after a successful API response.
SocketContext
SocketContextProvider watches authUser via a useEffect. When a user logs in, it opens a socket.io-client connection and passes userId as a handshake query parameter so the server can map the user to their socket ID.
{ socket, onlineUsers }. When authUser becomes null (logout), the effect’s cleanup function closes the socket immediately.
useConversation (Zustand)
The Zustand store is the single source of truth for whichever conversation is currently open in the message pane.| Field | Type | Purpose |
|---|---|---|
selectedConversation | object | null | The user object of the person being chatted with |
setSelectedConversation | function | Updates the active conversation; triggers message fetch |
messages | array | All messages for the selected conversation |
setMessages | function | Replaces or appends to the message list |
useConversation() without prop drilling or an extra context provider.
Custom Hooks
All data-fetching, mutation, and socket-subscription logic lives in dedicated hooks undersrc/hooks/.
| Hook | Description |
|---|---|
useSignup | POSTs to /api/auth/signup, sets authUser on success |
useLogin | POSTs to /api/auth/login, sets authUser on success |
useLogout | POSTs to /api/auth/logout, clears authUser and localStorage |
useSendMessage | POSTs a new message to /api/messages/:id, appends it to the Zustand store |
useGetMessages | GETs the message history for selectedConversation from /api/messages/:id |
useGetConversations | GETs the full contact list from /api/users for the sidebar |
useListenMessages | Subscribes to the newMessage socket event and appends incoming messages to the Zustand store |
Component Structure
TheHome page composes two top-level components side by side.
Sidebar
SearchInput— filters the contact list by name in real timeConversations— renders oneConversationitem per contact returned byuseGetConversations; highlights contacts that appear inonlineUsersLogoutButton— callsuseLogoutand clears session state
Messages— maps overmessagesfrom the Zustand store and renders each bubble with timestampMessageInput— controlled input that callsuseSendMessageon submit
UI Stack
| Library | Version | Purpose |
|---|---|---|
| Tailwind CSS | ^3.4.1 | Utility-first CSS; layout, spacing, and colour |
| DaisyUI | ^4.6.1 | Tailwind component library; buttons, inputs, avatars, chat bubbles |
| react-icons | ^5.0.1 | Icon set used for send button, search, and logout |
| react-hot-toast | ^2.4.1 | Non-blocking toast notifications for errors and confirmations |
DaisyUI is a
devDependency — its classes are purged by Tailwind’s content scanner at build time, so no DaisyUI JavaScript ships to the browser.