A full-stack web application that provides an intelligent chat interface with real-time communication, internet search capabilities, and multi-model AI support.
Perplexity is a modern chat application that combines:
- Real-time messaging using WebSocket technology
- Integration with multiple AI models (Google Gemini, Mistral AI)
- Internet search capabilities for context-aware responses
- User authentication and session management
- Chat history persistence
- Dark/Light theme support
- Runtime: Node.js with ES Modules
- Framework: Express.js
- Database: MongoDB with Mongoose ODM
- Real-time Communication: Socket.IO
- AI Integration: LangChain with Google Generative AI and Mistral AI
- Search Integration: Tavily Search API
- Authentication: JWT (JSON Web Tokens)
- Security: bcryptjs for password hashing
- Framework: React 19
- State Management: Redux Toolkit
- Styling: Tailwind CSS
- Build Tool: Vite
- Routing: React Router DOM v7
- HTTP Client: Axios
- Real-time Communication: Socket.IO Client
- Markdown Rendering: React Markdown with GFM support
- Syntax Highlighting: Highlight.js
- UI Icons: Lucide React
graph TB
subgraph Frontend["Frontend - React + Vite"]
Auth["Auth Module"]
Chat["Chat Module"]
Theme["Theme Module"]
API["Socket.IO Client"]
UI["UI Components"]
end
subgraph Backend["Backend - Node.js + Express"]
AuthCtrl["Auth Controller"]
ChatCtrl["Chat Controller"]
AIService["AI Service"]
InternetService["Internet Service"]
MailService["Mail Service"]
SocketServer["Socket.IO Server"]
DB[("MongoDB")]
end
subgraph External["External Services"]
Google["Google Gemini API"]
Mistral["Mistral AI API"]
Tavily["Tavily Search API"]
end
Auth -->|HTTP| AuthCtrl
Chat -->|HTTP/WebSocket| ChatCtrl
Chat -->|WebSocket| SocketServer
Theme -->|Local Storage| UI
UI -->|Axios| API
AuthCtrl --> DB
ChatCtrl --> DB
AIService --> Google
AIService --> Mistral
InternetService --> Tavily
SocketServer --> AIService
SocketServer --> InternetService
SocketServer --> DB
MailService -.->|Email| Backend
SocketServer -->|WebSocket| API
- User registration and login
- JWT-based session management
- Secure password hashing with bcryptjs
- Protected routes and API endpoints
- Real-time messaging with WebSocket
- Multi-turn conversations
- Chat history persistence
- Create, read, and delete chat threads
- Automatic chat title generation
- Support for multiple AI models:
- Google Gemini (via @langchain/google-genai)
- Mistral AI (via @langchain/mistralai)
- Real-time streaming responses
- Context-aware conversations
- Integration with Tavily Search API
- Retrieval augmented generation (RAG)
- Context-aware responses with web information
- Dark/Light theme switching
- Responsive design for mobile and desktop
- Markdown rendering with syntax highlighting
- Real-time message delivery
Perplexity/
├── Backend/
│ ├── src/
│ │ ├── app.js (Express app setup)
│ │ ├── config/
│ │ │ └── db.js (MongoDB connection)
│ │ ├── controllers/
│ │ │ ├── auth.controller.js (Auth logic)
│ │ │ └── chat.controller.js (Chat logic)
│ │ ├── middleware/
│ │ │ ├── auth.middleware.js (JWT verification)
│ │ │ ├── errorHandler.middleware.js
│ │ │ └── validate.middleware.js
│ │ ├── models/
│ │ │ ├── user.model.js
│ │ │ ├── chat.model.js
│ │ │ └── message.model.js
│ │ ├── routes/
│ │ │ ├── auth.routes.js
│ │ │ └── chat.routes.js
│ │ ├── services/
│ │ │ ├── ai.service.js (AI integration)
│ │ │ ├── internet.service.js (Search integration)
│ │ │ └── mail.service.js
│ │ ├── sockets/
│ │ │ └── server.socket.js (WebSocket handlers)
│ │ └── validator/
│ │ └── auth.validator.js
│ ├── package.json
│ ├── server.js (Entry point)
│ └── plan.txt
│
├── Frontend/
│ ├── src/
│ │ ├── main.jsx (App entry point)
│ │ ├── app/
│ │ │ ├── App.jsx
│ │ │ ├── App.routes.jsx (Route definitions)
│ │ │ ├── app.store.js (Redux store)
│ │ │ └── theme.slice.js
│ │ ├── features/
│ │ │ ├── Auth/
│ │ │ │ ├── auth.slice.js
│ │ │ │ ├── components/
│ │ │ │ ├── hooks/
│ │ │ │ ├── pages/
│ │ │ │ ├── services/
│ │ │ │ └── validator/
│ │ │ ├── Chat/
│ │ │ │ ├── chat.slice.js
│ │ │ │ ├── components/
│ │ │ │ ├── hooks/
│ │ │ │ ├── pages/
│ │ │ │ └── services/
│ │ │ ├── Theme/
│ │ │ └── API/
│ │ ├── hooks/
│ │ │ └── useIsMobile.jsx
│ │ └── utils/
│ └── package.json
│
└── README.md (this file)
graph TD
A["User Input"] --> B["Auth Form Submission"]
B --> C["API Call via Axios"]
C --> D["Backend Auth Controller"]
D --> E["Validate Input"]
E --> F{Valid Input?}
F -->|No| G["Return Error"]
F -->|Yes| H["Hash Password with bcryptjs"]
H --> I["Check if User Exists"]
I --> J{User Exists?}
J -->|Yes| G
J -->|No| K["Create User in MongoDB"]
K --> L["Generate JWT Token"]
L --> M["Return Token to Client"]
M --> N["Store Token in Redux"]
N --> O["Set Authorization Header"]
O --> P["Redirect to Dashboard"]
graph TD
A["User Types Message"] --> B["Click Send Button"]
B --> C["Emit via Socket.IO"]
C --> D["Backend Socket Handler"]
D --> E["Validate User & Chat"]
E --> F["Save User Message to DB"]
F --> G["Emit: message:user-added"]
G --> H["Display User Message in UI"]
H --> I["Call AI Service"]
I --> J{Internet Search Needed?}
J -->|Yes| K["Query Tavily API"]
K --> L["Format Search Results"]
J -->|No| M["Skip Search"]
L --> N["Create LLM Chain"]
M --> N
N --> O["Call AI Model"]
O --> P["Stream Response Chunks"]
P --> Q["Emit: ai:streaming"]
Q --> R["Update UI in Real-time"]
R --> S["Emit: ai:message-complete"]
S --> T["Save AI Message to DB"]
T --> U["Generate Chat Title"]
U --> V["Update Chat in UI"]
- Node.js (v16 or higher)
- MongoDB (local or Atlas)
- Environment variables configured
cd Backend
npm install
# Create .env file with required variables
npm run devcd Frontend
npm install
npm run devThe application will be available at http://localhost:5173 (frontend) with backend at http://localhost:3000.
MONGODB_URI=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret_key
GOOGLE_API_KEY=your_google_api_key
MISTRAL_API_KEY=your_mistral_api_key
TAVILY_API_KEY=your_tavily_api_key
SMTP_HOST=your_email_host
SMTP_PORT=your_email_port
SMTP_USER=your_email
SMTP_PASS=your_email_password
VITE_API_URL=http://localhost:3000
POST /api/auth/register- Register new userPOST /api/auth/login- Login userGET /api/auth/me- Get current user
GET /api/chat/chats- Get all chats for userGET /api/chat/:chatId/messages- Get messages in a chatDELETE /api/chat/:chatId- Delete a chat
message:send- Send new messagemessage:user-added- User message addedai:streaming- AI response streamingai:message-complete- AI response complete
Uses Socket.IO for bidirectional communication between client and server, enabling real-time message delivery and streaming AI responses.
Leverages LangChain to abstract over multiple AI providers, allowing seamless switching between different AI models.
Integrates Tavily Search API to enhance AI responses with current information from the web.
Redux Toolkit manages application state including:
- Authentication state
- Chat history
- Active chat messages
- Theme preferences
-
Backend Development
- Create new routes in
/routes - Implement controllers in
/controllers - Add business logic in
/services - Use middleware for validation and authentication
- Create new routes in
-
Frontend Development
- Create feature modules in
/features - Use Redux slices for state management
- Create reusable components
- Implement custom hooks for logic
- Create feature modules in
-
Testing
- Test API endpoints with Postman/Thunder Client
- Test UI components in browser
- Verify WebSocket communication
- Database indexing on frequently queried fields
- Pagination for chat history
- Real-time message streaming instead of full batch responses
- Client-side caching with Redux
- Responsive images and lazy loading
- JWT-based authentication
- Password hashing with bcryptjs
- CORS whitelist configuration
- Input validation and sanitization
- Protected API endpoints with auth middleware
- Secure WebSocket connections
- Check MongoDB connection string
- Verify all environment variables are set
- Check that port 3000 is not in use
- Verify backend is running on port 3000
- Check VITE_API_URL environment variable
- Check browser console for CORS errors
- Verify Socket.IO connection in browser DevTools
- Check backend socket handlers are registered
- Verify JWT token is valid
- Multi-user chat rooms
- File upload support
- Voice-to-text integration
- Chat export functionality
- Advanced filtering and search
- User profiles with preferences
- Admin dashboard for monitoring
ISC
For issues and questions, please check the Backend and Frontend README files for more detailed information about each component.