π¬ Live Chat Support Demo
This repo is a template β click Use this template to create your own real-time chat support app instantly!
A real-time support chat app built with Next.js (App Router), Socket.IO, Zustand, and Tailwind CSS. Visitors can instantly chat with an admin, while both sides see messages update in real time. All chats are locally persisted per role.
Featured on Dev.to π Live Chat Support Starter Kit β Open Source!
β¨ Features
- β Real-time chat with Socket.IO
- β¨οΈ Typing indicators (visitor β admin)
- π§βπ» Separate interfaces for visitor and admin
- π§ Role-based persistence:
- Visitor chats β
sessionStorage - Admin chats β
localStorage
- Visitor chats β
- π» Deployed on Vercel (frontend) + Railway (Socket.IO backend)
- π Styled with Tailwind CSS
π Project Structure
.
βββ frontend/ β Next.js frontend application
β βββ src/
β β βββ app/ β Admin & visitor views
β β βββ components β Shared components
β β βββ store/ β Zustand state
β β βββ lib/ β Socket.IO client
β β βββ types/ β Type definitions
β β βββ config/ β Configuration files
β β βββ styles/ β Tailwind styles
β βββ public/ β Static assets
β βββ package.json β Frontend dependencies
β βββ [config files] β Next.js, TypeScript, etc.
βββ backend/ β Socket.IO server
βββ server.js β Main server entry point
βββ controllers/ β Socket.IO event handlers
βββ services/ β Business logic
βββ routes/ β HTTP routes (if needed)
βββ utils/ β Utility functions
βββ package.json β Backend dependencies
βοΈ Configuration
The application uses a centralized configuration system located in frontend/src/config/:
Chat Configuration (chat.ts)
export const chatConfig = {
welcomeMessage: 'Hi π How can we help you?', // Initial greeting message
defaultAgentName: 'Support Team', // Default name for support agents
};
To modify the chat appearance or behavior:
- Update the values in
chatConfig - The changes will automatically reflect across all components using these settings
Environment Configuration
-
Frontend:
.env.localNEXT_PUBLIC_API_URL=http://localhost:3001 -
Backend:
.envFRONTEND_URL=http://localhost:3000 FRONTEND_URL_2=https://staging.your-app.com # Optional: Additional frontend URL FRONTEND_URL_3=https://dev.your-app.com # Optional: Additional frontend URL PORT=3001π‘ Multiple Frontend URLs: You can configure multiple frontend URLs by setting additional environment variables (
FRONTEND_URL_2,FRONTEND_URL_3, etc.). This is useful when you have multiple environments (staging, development, etc.) that need to connect to the same backend. The backend will automatically handle CORS for all configured URLs.
π§ State Management
The application uses Zustand for state management with the following key stores:
Chat Store (store/chatStore.ts)
interface ChatState {
messages: Message[];
user: User | null;
conversations: Record<string, Message[]>;
onlineVisitors: Set<string>;
isChatFocused: boolean;
selectedVisitorId: string | null;
role: UserRole | null;
// ... actions
}
Key features:
- Role-based persistence (admin/visitor)
- Real-time message synchronization
- Online status tracking
- Conversation management
Usage Example
const { messages, user, sendMessage } = useChatStore();
// Send a message
sendMessage({
conversationId: '123',
senderId: user.id,
content: 'Hello!'
});
π§© Component Architecture
The application follows a modular component structure:
Visitor Components
ChatWidget: Floating chat button and window containerChatWindow: Main chat interface for visitorsMessageBubble: Individual message displayMessageInput: Message composition and sending
Admin Components
ChatHeader: Conversation header with visitor infoVisitorList: List of active visitorsChatContainer: Main admin chat interface
Shared Components
Avatar: User avatar displayDarkModeToggle: Theme switchingErrorToast: Error notificationsMessageInput: Sends messages and emits Socket.IO typing start/stop (debounced idle)
Component Communication
- Components communicate through Zustand store
- Real-time updates via Socket.IO
- Props for component-specific configuration
π§ͺ Dev Tips
- Open admin and visitor tabs to simulate both roles
- Test real-time message syncing
- Test persistence:
- Reload the tab
- Visitor chat clears after closing the tab
- Admin chat stays saved across reloads (localStorage)
π Deployment Notes
Frontend β Vercel
-
Create a new project in Vercel and connect it to your repository
-
Configure the following settings in Vercel:
- Framework Preset: Next.js
- Root Directory: frontend
- Build Command:
npm run build - Output Directory:
.next - Install Command:
npm install
-
Set the following environment variables in Vercel:
NEXT_PUBLIC_API_URL=https://your-railway-backend.up.railway.app -
Deploy using the Vercel CLI:
# Install Vercel CLI if you haven't already npm i -g vercel # Deploy vercel --prod
Backend β Railway
- Set start command:
node server.js - Set environment variables:
FRONTEND_URL=https://your-vercel-project.vercel.app
- Make sure ports are not hardcoded β use
process.env.PORT
π§ Technologies Used
π‘ Future Improvements
- π Export/download chat history
- π Add backend persistence (DB)
- π Auth for both roles
- π₯ Multi-agent support
- π Support for parallel chats with multiple users
π License
MIT β use freely and modify as you wish!
π Author
Made with β€οΈ by Sanja Malovic