I want to build a real-time chat web application. Please provide the code using React,…
- User
I want to build a real-time chat web application. Please provide the code using React, Tailwind CSS, and Firebase (for authentication and real-time database).
The application must include:
Authentication: A simple login page where the user enters a display name.
UI Design: A clean, modern chat interface with a sidebar for contacts and a main chat area. Ensure it is fully responsive for mobile devices.
Functionality:
Real-time messaging capability using Firebase Firestore.
Online/Offline status indicator for users.
Auto-scroll to the latest message.
Project Structure: Provide the code in a clean structure with clear instructions on how to set up the Firebase project and run the app locally.
Please write the code in a way that is easy to understand and maintain."
لماذا هذا البرومت أفضل لـ Arena AI؟ تحديد الأدوات (Tech Stack): ذكرك لـ React و Firebase هو المفتاح؛ لأن Firebase سيوفر لك "قاعدة بيانات حقيقية" (Real-time Database) دون الحاجة لبرمجة سيرفر معقد، مما يجعل الموقع يعمل فعلياً "أونلاين" وليس مجرد شكل خارجي.
تحديد الميزات التقنية: طلب "Auto-scroll" و "Status indicator" يوجه النموذج لبرمجة منطق (Logic) حقيقي للمحادثة.
اللغة: التعامل بالإنجليزية مع نماذج البرمجة غالباً ما يعطي نتائج أدق وأكواد أكثر استقراراً، ولكن يمكنك دائماً طلب الشرح باللغة العربية بعد الحصول على الكود.
Assistant
I'll build this step by step. Let me start by installing the required packages.
Now I'll create all the files. Let me start with the Firebase configuration and work outward.
Now let me update the main application files.
I see the leftover code from the old App.tsx. Let me fix it.
Now update the entry point to wrap with the AuthProvider, update the HTML title, and add the animation CSS.
Now let me build the project to verify everything compiles cleanly.
The build succeeds! Here's a summary of everything that was built.
✅ ChatHub — Real-Time Chat Application
Project Structure
src/ ├── firebase/ │ └── config.ts # Firebase initialization ├── context/ │ └── AuthContext.tsx # Auth state management (Context API) ├── components/ │ ├── Login.tsx # Display-name login page │ ├── ChatLayout.tsx # Main layout (sidebar + chat) + heartbeat │ ├── Sidebar.tsx # Online user list with status dots │ ├── ChatArea.tsx # Messages + auto-scroll + send input │ └── StatusIndicator.tsx # Green/gray online dot with ping ├── App.tsx # Root: loading → login → chat ├── main.tsx # Entry point (wraps AuthProvider) └── index.css # Tailwind + animations + scrollbarHow to Set Up Firebase
- Create a Firebase project at console.firebase.google.com
- Go to Project Settings → General → Your apps and click Add app → Web
- Copy the
firebaseConfigvalues - Paste them into
src/firebase/config.ts(replace the placeholders) - In the Firebase Console, enable these services:
- Authentication → Sign-in method → Anonymous (enable it)
- Firestore Database → Create database (start in test mode for development)
How to Run Locally
npm install # already done npm run dev # start dev serverKey Features Implemented
Feature How it Works Display-name login Firebase anonymous auth + Firestore user document Real-time messaging onSnapshotlistener on FirestoremessagescollectionOnline/Offline status 30s heartbeat writes isOnline: trueto Firestore;beforeunloadmarks offlineAuto-scroll useEffect+scrollIntoView({ behavior: "smooth" })on a bottom anchordivResponsive sidebar Sidebar is always visible on desktop (lg+), slides in as an overlay drawer on mobile Unread-friendly bubbles Sent messages appear on the right (indigo), received on the left (gray) with sender name Firestore Data Model
users/{uid}—{ uid, displayName, isOnline, lastSeen }messages/{autoId}—{ senderId, senderName, text, createdAt }
Important: Edit
src/firebase/config.tswith your actual Firebase config before running the app!Files built:
src/firebase/config.ts,src/context/AuthContext.tsx,src/components/Login.tsx,src/components/StatusIndicator.tsx,src/components/Sidebar.tsx,src/components/ChatArea.tsx,src/components/ChatLayout.tsx,src/App.tsx,src/main.tsx,index.html,src/index.css
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.