π₯ Demo https://streamable.com/mu9g50
Watch the full demo video
Key features demonstrated in the video:
Real-time chat interaction Message streaming Dark/Light theme switching Mobile responsiveness WebSocket connection management Authentication flow
- Real-time chat interface with message streaming
- JWT-based authentication with protected routes
- Dark/Light theme support
- WebSocket connection management with auto-reconnection
- Chat session management
- Message history and persistence
- Toast notifications for system messages
- Responsive design for mobile and desktop
- Custom hooks for chat and WebSocket functionality
- State management with Zustand
- Next.js: React framework for the frontend
- Tailwind CSS: Utility-first styling
- Zustand: Lightweight state management
- Lucide React: Icon components
- React Hot Toast: Toast notifications
- ShadcnUI: UI component library
- WebSocket: Real-time communication
src/
βββ app/
β βββ hooks/
β β βββ useAuth.js # Authentication hook
β β βββ useChat.js # Chat functionality
β β βββ useWebSocket.js # WebSocket management
β βββ store/
β β βββ chatStore.js # Chat state management
β β βββ uiStore.js # UI state (theme, sidebar)
β βββ services/
β β βββ websocket.js # WebSocket service
β βββ utils/
β βββ Toast.js # Toast notifications
βββ components/
β βββ chat/
β β βββ ChatBubble.jsx # Message bubbles
β β βββ ChatInput.jsx # Message input
β β βββ ChatList.jsx # Message list
β β βββ StreamingIndicator.jsx # Typing indicator
β βββ shared/
β β βββ Header.jsx # App header
β β βββ Footer.jsx # App footer
β β βββ Sidebar.jsx # Chat sessions sidebar
β β βββ LoadingSpinner.jsx # Loading states
β βββ ui/
β βββ alert.jsx # Alert component
β βββ button.jsx # Button component
The authentication system uses JWT tokens and includes:
- Login/Signup forms with validation
- Token storage in localStorage
- Protected route middleware
- Automatic token verification
- Session management
- Logout functionality
The WebSocket service (websocket.js) handles:
class WebSocketService {
- Connection management
- Auto-reconnection logic
- Message handling
- Event emission
- Connection status tracking
- Error handling
}Using Zustand for state management:
- Messages array
- Current session management
- Stream handling
- Chat history
- Dark/Light mode
- Sidebar state
- Connection status
- Message sending
- Stream handling
- Chat session management
- Connection handling
- Auto-reconnection
- Status management
- Message sending
- Authentication state
- Login/Logout
- Token management
- User/AI message rendering
- Timestamp formatting
- Typing indicators
- Stream visualization
- Message history display
- Auto-scrolling
- Loading states
- Empty states
- Message composition
- Send functionality
- Loading states
- Error handling
-
Message Streaming
- Token-by-token display
- Typing indicators
- Stream management
-
Connection Management
- Status indicators
- Auto-reconnection
- Error handling
-
Session Handling
- Multiple chat sessions
- Session switching
- History persistence
Using Tailwind CSS with:
- Custom theme configuration
- Dark/Light mode support
- Responsive design
- Custom animations
- UI component variants
- Mobile-first approach
- Breakpoint management
- Touch interactions
- Adaptive layouts
- Dynamic components
-
Installation
cd frontend npm install -
Environment Setup Create
.env.local:NEXT_PUBLIC_WS_URL=ws://localhost:8000 NEXT_PUBLIC_API_URL=http://localhost:8000 -
Development
npm run dev
-
Building
npm run build npm start
{
"dependencies": {
"next": "latest",
"react": "^18",
"zustand": "^4",
"lucide-react": "latest",
"react-hot-toast": "^2",
"tailwindcss": "^3"
}
}# Run tests
npm test
# Run with coverage
npm test -- --coverage-
Component Structure
- Functional components
- Custom hooks for logic
- Props validation
- Error boundaries
-
State Management
- Zustand for global state
- Local state when appropriate
- Memoization for performance
-
WebSocket Handling
- Connection management
- Error handling
- Reconnection logic
- Event handling
-
Code Style
- ESLint configuration
- Prettier formatting
- TypeScript (optional)
- Component documentation
A robust FastAPI backend for an AI chat application powered by Groq's LLM API.
- π User authentication with JWT
- π¬ Real-time chat via WebSockets
- π§ Integration with Groq API for LLM responses
- π Chat history persistence
- π Async database operations with SQLAlchemy
- π Scalable architecture with dependency injection
- FastAPI: Modern, high-performance web framework
- SQLAlchemy: Async ORM for database operations
- Groq API: State-of-the-art LLM integration
- WebSockets: Real-time communication
- JWT Authentication: Secure user sessions
- Async I/O: Non-blocking operations throughout
- Python 3.8+
- PostgreSQL or SQLite
- Groq API key
-
Clone the repository:
git clone https://github.com/yourusername/ai-chat-backend.git cd ai-chat-backend -
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Create a
.envfile in the project root:DATABASE_URL=sqlite+aiosqlite:///./chatbot.db # For PostgreSQL: postgresql+asyncpg://user:password@localhost/dbname SECRET_KEY=your_secret_key_here ALGORITHM=HS256 ACCESS_TOKEN_EXPIRE_MINUTES=30 GROQ_API_KEY=your_groq_api_key_here
-
Initialize the database:
python -m app.db.database
-
Start the server:
uvicorn app.main:app --reload --port 8000
-
Access the API documentation:
http://localhost:8000/docs
POST /api/auth/signup: Register a new userPOST /api/auth/login: Authenticate and get JWT tokenGET /api/auth/me: Get current user infoGET /api/auth/verify-token: Verify JWT token validity
GET /api/chats: Get all chat sessionsGET /api/chats/{session_id}/messages: Get messages for a specific chat sessionWebSocket /ws/chat: Real-time chat connection
app/
βββ api/
β βββ routes/
β βββ auth.py
βββ db/
β βββ database.py
β βββ repositories/
β βββ chat_repository.py
β βββ user_repository.py
βββ models/
β βββ chat.py
β βββ user.py
βββ services/
β βββ auth_service.py
β βββ chat_service.py
β βββ groq_service.py
βββ utils/
β βββ security.py
βββ main.py
- User registers with email/password
- Credentials are validated and password is hashed
- JWT token is generated and returned
- Subsequent requests include token in Authorization header
- Protected routes verify token and extract user identity
The application uses WebSockets for real-time chat:
- Client establishes WebSocket connection
- Server assigns a unique session ID
- Messages are streamed token-by-token for real-time responses
- All messages are persisted to the database
- Session history can be retrieved via REST endpoints
Run the tests with:
pytest- Database connections use connection pooling
- Async handlers maximize throughput
- JWT authentication is stateless for horizontal scaling
- Connection manager tracks active WebSockets
MIT
- Balti Mohamed amine - Initial work