Skip to content

A modern, full-featured realtime chat application built with Next.js 15, TypeScript, Pusher, Clerk Auth, and PostgreSQL. Features both direct messaging and group chat functionality with a polished user experience.

License

Notifications You must be signed in to change notification settings

ateendra24/realtime-chat-nextjs

Repository files navigation

Realtime Chat Application

A modern, full-featured realtime chat application built with Next.js 15, TypeScript, Ably, Clerk Auth, and PostgreSQL. Features both direct messaging and group chat functionality with a polished user experience and production-ready scalability.

Screenshot

Screenshot Screenshot Screenshot Screenshot

Architecture Diagram

graph TB
    subgraph Client ["Client Side"]
        A[React Components] --> B[useChatLogic Hook]
        B --> C[useRealtime Hook]
        C --> D[Ably Client]
        E[User Interface] --> A
    end

    subgraph API ["Next.js API Routes"]
        F[Chats API] --> G[Chat Management]
        H[Messages API] --> I[Message CRUD]
        J[Groups API] --> K[Group Management]
        L[Users API] --> M[User Operations]
        N[Realtime API] --> O[Ably Broadcasts]
    end

    subgraph Database ["Database Layer"]
        P[(PostgreSQL)]
        Q[Drizzle ORM]
        Q --> P
    end

    subgraph External ["External Services"]
        R[Clerk Auth]
        S[Ably Channels]
        T[ImageKit CDN]
    end

    subgraph Realtime ["Real-time Flow"]
        U[User sends message] --> V[API stores in DB]
        V --> W[Ably event triggered]
        W --> X[All clients receive update]
        X --> Y[UI updates instantly]
    end

    %% Connections
    D -.->|Ably Events| S
    G --> Q
    I --> Q
    K --> Q
    M --> Q
    O --> S
    A --> R
    A --> T

    %% Real-time flow connections
    E --> U
    V --> O
    S --> D
    D --> Y

    %% Styling
    classDef client fill:#1e3a8a,stroke:#3b82f6,stroke-width:2px,color:#fff
    classDef api fill:#7c2d12,stroke:#ea580c,stroke-width:2px,color:#fff
    classDef db fill:#14532d,stroke:#22c55e,stroke-width:2px,color:#fff
    classDef external fill:#581c87,stroke:#a855f7,stroke-width:2px,color:#fff
    classDef realtime fill:#991b1b,stroke:#ef4444,stroke-width:2px,color:#fff

    class A,B,C,D,E client
    class F,G,H,I,J,K,L,M,N,O api
    class P,Q db
    class R,S,T external
    class U,V,W,X,Y realtime
Loading

Features Implemented

  • βœ… User Authentication (Clerk)
  • βœ… User Registration with Profile Picture Upload
  • βœ… Real-time Messaging (Ably)
  • βœ… Real-time Message Reactions (Ably Events)
  • βœ… Secure Image Sharing (Upload, Compression, Access Control)
  • βœ… Direct Messages
  • βœ… Group Chats
  • βœ… Group Management (Add/Remove Members)
  • βœ… Group Avatar Upload
  • βœ… Unread Message Counts
  • βœ… User Presence (Online/Offline Status)
  • βœ… Bubble Typing Indicator
  • βœ… Message Search
  • βœ… Responsive Design (Mobile-First)
  • βœ… Dark/Light Theme Toggle
  • βœ… Production-Ready Deployment (Vercel)

Tech Stack

  • Frontend: Next.js 15.5.2 (App Router), React 19, TypeScript
  • UI Components: shadcn/ui with Radix UI primitives
  • Styling: Tailwind CSS with custom theme variables
  • Authentication: Clerk (complete auth flow with SSO callback)
  • Real-time Communication: Ably (enterprise-grade WebSocket service with 99.999% uptime)
  • Database: PostgreSQL with Drizzle ORM (schema with migrations)
  • File Storage: Vercel Blob (secure image storage with access control)
  • Image Processing: Browser-based compression with emoji picker
  • State Management: Custom React hooks with optimistic updates
  • Development: Next.js with Turbopack for fast rebuilds
  • Deployment: Vercel-ready serverless architecture
  • Type Safety: Full TypeScript coverage with proper interfaces

Setup Instructions

1. Environment Variables

Create a .env.local file with the following credentials:

# Clerk Authentication (Required)
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=your_clerk_publishable_key_here
CLERK_SECRET_KEY=your_clerk_secret_key_here

# Database (Required for full functionality)
DATABASE_URL=your_postgresql_connection_string_here

# Ably Configuration (Required for real-time features)
# Get your API key from: https://ably.com/dashboard
# Format: <app-id>.<key-id>:<key-secret>
ABLY_API_KEY=your_ably_api_key_here

# Vercel Blob Storage (Required for image sharing)
BLOB_READ_WRITE_TOKEN=your_vercel_blob_token_here

# Optional: ImageKit for avatar uploads
NEXT_PUBLIC_IMAGEKIT_PUBLIC_KEY=your_imagekit_public_key
IMAGEKIT_PRIVATE_KEY=your_imagekit_private_key
NEXT_PUBLIC_IMAGEKIT_URL_ENDPOINT=your_imagekit_url_endpoint

2. Get Clerk Credentials

  1. Visit Clerk.com and create an account
  2. Create a new application
  3. Copy the publishable key and secret key to your .env.local file
  4. Configure sign-in/sign-up options in Clerk dashboard

3. Get Ably Credentials

  1. Visit Ably.com and create an account (Free tier: 6M messages/month!)
  2. Create a new app in the Ably dashboard
  3. Go to the "API Keys" tab
  4. Copy the Root API Key (format: appId.keyId:keySecret)
  5. Add it to your .env.local file as ABLY_API_KEY

4. Database Setup (Required)

  1. Create a PostgreSQL database (recommended: Neon or Supabase)
  2. Add the connection string to your .env.local file
  3. Run database migrations:
npm run db:generate
npm run db:push

5. Install Dependencies & Start Development

# Install all dependencies
npm install

# Start the development server
npm run dev

This will start the Next.js app on http://localhost:3000 with real-time features powered by Ably.

6. Vercel Blob Storage Setup (Required for Image Sharing)

  1. Visit Vercel Dashboard
  2. Go to your project β†’ Storage β†’ Create Database β†’ Blob
  3. Copy the BLOB_READ_WRITE_TOKEN to your .env.local file
  4. The storage will be automatically configured for your deployment

7. Production Deployment

The application is ready for production deployment on Vercel:

# Build for production
npm run build

# Or deploy to Vercel
vercel --prod

Make sure to add all environment variables to your Vercel project settings.

Scripts

  • npm run dev - Start development server with Ably real-time features
  • npm run build - Build for production with Turbopack
  • npm run start - Start production server
  • npm run lint - Run ESLint for code quality
  • npm run db:generate - Generate database migrations with Drizzle
  • npm run db:push - Push database schema changes to PostgreSQL

Project Structure

src/
β”œβ”€β”€ app/                    # Next.js 15 App Router
β”‚   β”œβ”€β”€ (auth)/            # Authentication routes
β”‚   β”‚   β”œβ”€β”€ sign-in/       # Clerk sign-in page
β”‚   β”‚   └── sign-up/       # Clerk sign-up page
β”‚   β”œβ”€β”€ api/               # API routes
β”‚   β”‚   β”œβ”€β”€ chats/         # Chat management endpoints
β”‚   β”‚   β”‚   β”œβ”€β”€ route.ts   # List user chats
β”‚   β”‚   β”‚   β”œβ”€β”€ [chatId]/  # Individual chat operations
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ messages/ # Message CRUD operations
β”‚   β”‚   β”‚   β”‚   └── unread/   # Unread message management
β”‚   β”‚   β”‚   └── direct/    # Direct chat creation
β”‚   β”‚   β”œβ”€β”€ groups/        # Group management endpoints
β”‚   β”‚   β”œβ”€β”€ users/         # User operations (search, sync)
β”‚   β”‚   β”œβ”€β”€ upload/        # File upload endpoints
β”‚   β”‚   β”‚   └── image/     # Secure image upload with compression
β”‚   β”‚   β”œβ”€β”€ images/        # Secure image serving
β”‚   β”‚   β”‚   └── [imageId]/ # Protected image access with auth
β”‚   β”‚   └── realtime/      # Real-time event triggers
β”‚   β”œβ”€β”€ chat/              # Main chat interface
β”‚   β”‚   └── page.tsx       # Chat application UI
β”‚   β”œβ”€β”€ home/              # Home page redirect
β”‚   β”œβ”€β”€ sso-callback/      # Clerk SSO callback
β”‚   β”œβ”€β”€ layout.tsx         # Root layout with providers
β”‚   └── page.tsx           # Landing page
β”œβ”€β”€ components/            # Reusable React components
β”‚   β”œβ”€β”€ ui/                # shadcn/ui base components
β”‚   β”‚   └── visually-hidden.tsx # Accessibility utility component
β”‚   β”œβ”€β”€ ChatList.tsx       # Chat sidebar with real-time updates
β”‚   β”œβ”€β”€ Messages.tsx       # Message display with reactions
β”‚   β”œβ”€β”€ MessageInput.tsx   # Message composition with image upload
β”‚   β”œβ”€β”€ ImageMessage.tsx   # Secure image display with viewer
β”‚   β”œβ”€β”€ ChatHeader.tsx     # Chat info and actions
β”‚   β”œβ”€β”€ CreateGroup.tsx    # Group creation dialog
β”‚   β”œβ”€β”€ UserSearch.tsx     # User discovery and selection
β”‚   β”œβ”€β”€ GroupInfoSheet.tsx # Group management interface
β”‚   └── ...               # Additional UI components
β”œβ”€β”€ hooks/                 # Custom React hooks
β”‚   β”œβ”€β”€ useChatLogic.ts    # Core chat state management
β”‚   β”œβ”€β”€ useRealtime.ts     # Ably client connection
β”‚   β”œβ”€β”€ useImageUpload.ts  # Image compression and upload logic
β”‚   └── ...               # Additional utility hooks
β”œβ”€β”€ db/                    # Database layer
β”‚   β”œβ”€β”€ index.ts           # Database connection setup
β”‚   β”œβ”€β”€ schema.ts          # Drizzle ORM schema definitions
β”‚   └── schema-new.ts      # Updated schema with latest fields
β”œβ”€β”€ lib/                   # Utility libraries
β”‚   β”œβ”€β”€ utils.ts           # Common utility functions
β”‚   β”œβ”€β”€ ably.ts          # Ably server configuration
β”‚   └── imagekit.ts        # Image upload configuration
└── types/                 # TypeScript type definitions
    └── global.d.ts        # Global type declarations

How It Works

System Flow Diagram

sequenceDiagram
    participant U as User A
    participant UI as React UI
    participant API as Next.js API
    participant DB as PostgreSQL
    participant P as Ably
    participant U2 as User B

    Note over U,U2: Real-time Message Flow

    U->>UI: Types and sends message
    UI->>API: POST /api/chats/[id]/messages
    API->>DB: Store message in database
    DB-->>API: Message saved with ID
    API->>P: Broadcast via Ably channel
    API-->>UI: Return success response
    UI->>UI: Update local state (optimistic)

    P->>U2: Real-time delivery to chat channel
    U2->>U2: Receive guaranteed update
    U2->>U2: Update UI instantly

    Note over U,U2: Group Management Flow

    U->>UI: Create new group
    UI->>API: POST /api/groups
    API->>DB: Create group & participants
    API->>P: Broadcast group events
    P->>U2: Notify group members
    U2->>U2: Update chat list

    Note over U,P: Authentication Flow

    U->>UI: Sign in/Sign up
    UI->>API: Clerk authentication
    API->>DB: Sync user data
    UI->>P: Connect to Ably
    P-->>UI: Real-time connection established
Loading

Architecture Overview

  1. Authentication: Users sign in through Clerk's secure authentication system with full session management
  2. Real-time Connection: Ably provides enterprise-grade WebSocket connections with 99.999% uptime and guaranteed message delivery
  3. Chat Management: Users can create direct chats or group chats with full CRUD operations
  4. Message Flow: Messages are stored in PostgreSQL and broadcasted via Ably channels with automatic retries for instant delivery
  5. State Synchronization: Real-time updates for chat lists, unread counts, and participant changes with connection recovery

Component Architecture

graph TD
    subgraph Page ["Page Level"]
        CP[Chat Page] --> CL[ChatList]
        CP --> M[Messages]
        CP --> MI[MessageInput]
        CP --> CH[ChatHeader]
    end

    subgraph Shared ["Shared Components"]
        CL --> UCL[useChatLogic]
        M --> UCL
        MI --> UCL
        CH --> GIS[GroupInfoSheet]
        CL --> CG[CreateGroup]
        CL --> US[UserSearch]
    end

    subgraph Hooks ["Custom Hooks"]
        UCL --> UR[useRealtime]
        UCL --> UD[useDebounce]
        UCL --> UP[usePresence]
        UR --> PC[Ably Client]
    end

    subgraph Data ["Data Layer"]
        UCL --> API[API Routes]
        API --> DB[(Database)]
        API --> PS[Ably Server]
    end

    subgraph Services ["External Services"]
        API --> CK[Clerk Auth]
        API --> IK[ImageKit]
        PS --> PCH[Ably Channels]
        PC -.->|Ably Events| PCH
    end

    %% Styling
    classDef page fill:#1e40af,stroke:#3b82f6,stroke-width:2px,color:#fff
    classDef component fill:#059669,stroke:#10b981,stroke-width:2px,color:#fff
    classDef hook fill:#d97706,stroke:#f59e0b,stroke-width:2px,color:#fff
    classDef data fill:#dc2626,stroke:#ef4444,stroke-width:2px,color:#fff
    classDef external fill:#7c3aed,stroke:#a855f7,stroke-width:2px,color:#fff

    class CP page
    class CL,M,MI,CH,GIS,CG,US component
    class UCL,UR,UD,UP,PC hook
    class API,DB,PS data
    class CK,IK,PCH external
Loading

Key Features in Detail

Real-time Messaging

  • Guaranteed delivery via Ably's enterprise-grade WebSocket connections with automatic retries
  • 99.999% uptime SLA ensuring reliable message delivery
  • Message persistence in PostgreSQL with full message history and Ably's 24-hour history
  • Connection recovery - seamless reconnection without message loss
  • Duplicate prevention using message IDs and client-side deduplication
  • Optimistic UI updates with server confirmation and error handling

Chat Types

  • Direct Chats: Private 1-on-1 conversations between users
  • Group Chats: Multi-participant conversations with admin controls
  • Auto-joining: Users automatically join all their chat rooms on connection

User Experience

  • Unread indicators with real-time count updates (hidden for active chats)
  • Real-time Typing Indicators showing when other users are typing
  • Smart timestamps with relative time formatting
  • Auto-scrolling to latest messages with smooth animations
  • Responsive design that works perfectly on desktop and mobile
  • Theme support with light/dark mode toggle

Secure Image Sharing

  • Upload with Compression: Images automatically compressed before upload to reduce bandwidth
  • Progress Indicators: Real-time upload progress with visual feedback
  • Drag & Drop Support: Intuitive file selection with drag-and-drop interface
  • Image Previews: Thumbnail previews before sending with caption support
  • Full-Screen Viewer: Click to expand images with download functionality
  • Security First: Enterprise-grade access control with authentication and chat membership verification
  • Real-time Updates: Images appear instantly for all chat participants
  • Optimistic UI: Immediate feedback with loading states and error handling

Technical Implementation

  • Type-safe end-to-end with TypeScript interfaces
  • Error handling with user-friendly fallbacks and automatic retry mechanisms
  • Performance optimized with efficient database queries and minimal re-renders
  • Ably channel management for targeted message delivery with guaranteed ordering
  • Secure image streaming through authenticated API endpoints
  • Access control with chat membership verification for every image request
  • Connection state recovery - automatic reconnection with message queue

Database Schema

The application uses a comprehensive PostgreSQL schema with the following key tables:

  • users - User profiles with Clerk integration, avatars, and online status
  • chats - Chat containers (direct or group) with metadata
  • chatParticipants - Many-to-many relationship between users and chats
  • messages - All messages with content, timestamps, type, and edit/delete status
  • messageAttachments - Secure image attachments with metadata and blob references
  • reactions - Message reactions with emoji and user tracking
  • readReceipts - Track read status for unread count functionality

All tables include proper foreign key relationships, indexes for performance, and timestamps for auditing.

πŸ”’ Secure Image Sharing Architecture

Security Features

  • πŸ” Authentication Required: All image access requires valid user authentication
  • πŸ‘₯ Chat Membership Verification: Users can only access images from chats they belong to
  • 🚫 No Public URLs: Direct blob URLs are never exposed to the client
  • 🌊 Server-Side Streaming: Images are proxied through secure API endpoints
  • πŸ›‘οΈ Access Control: Automatic cleanup when users leave chats
  • πŸ“± Cross-Platform: Works seamlessly across all devices and browsers

Image Upload Flow

sequenceDiagram
    participant U as User
    participant UI as React UI
    participant API as Upload API
    participant VB as Vercel Blob
    participant DB as Database
    participant P as Ably

    U->>UI: Select image file
    UI->>UI: Compress image (browser-side)
    UI->>API: POST /api/upload/image
    API->>VB: Store compressed image
    VB-->>API: Return blob URL
    API->>DB: Store attachment metadata + ID
    DB-->>API: Return attachment record
    API->>P: Broadcast image message via Ably
    API-->>UI: Return secure attachment ID
    UI->>UI: Display image via /api/images/[id]

    Note over U,P: Image is now securely accessible only to chat members
Loading

Image Viewing Flow

sequenceDiagram
    participant U as User
    participant UI as React UI
    participant API as Image API
    participant DB as Database
    participant VB as Vercel Blob

    U->>UI: Click to view image
    UI->>API: GET /api/images/[attachmentId]
    API->>API: Verify user authentication
    API->>DB: Check chat membership
    DB-->>API: Confirm user access
    API->>VB: Fetch image data
    VB-->>API: Return image buffer
    API-->>UI: Stream image with headers
    UI->>UI: Display secure image

    Note over U,VB: Blob URL never exposed to client
Loading

Key Security Benefits

  1. Enterprise-Grade Access Control: Images are protected by the same authentication system as your chat messages
  2. Automatic Permission Management: When users leave chats, they automatically lose access to images
  3. No URL Leakage: Even if someone intercepts network traffic, blob URLs are never transmitted to clients
  4. Audit Trail: All image access is logged and can be monitored
  5. GDPR Compliance: Users can be completely removed from the system with all their data

Recent Updates & Improvements

Performance & UX Enhancements

  • βœ… Ultra-fast message sending - Optimistic UI updates with instant message display (~10ms vs 800-1000ms)
  • βœ… Optimized database operations - Combined queries and parallel operations for ~60% faster API responses
  • βœ… Fire-and-forget real-time events - Non-blocking Ably broadcasts for maximum throughput with guaranteed delivery
  • βœ… Per-message loading indicators - Individual message status with opacity and spinner effects
  • βœ… Rapid-fire messaging capability - Removed global loading states to enable continuous message sending
  • βœ… Eliminated visual flicker when sending messages (unread counts, chat styling)
  • βœ… Duplicate message prevention with robust client-side and server-side deduplication
  • βœ… Enhanced error handling for undefined user data and network failures with automatic retries
  • βœ… Optimized Ably events with targeted channel-based message delivery and connection recovery
  • βœ… Improved message flow - server handles all broadcasting to prevent race conditions
  • βœ… Enterprise-grade reliability - 99.999% uptime with automatic connection recovery

Image Sharing Features

  • βœ… Secure Image Upload - Enterprise-grade security with authentication and access control
  • βœ… Smart Compression - Automatic image compression to reduce bandwidth usage
  • βœ… Real-time Image Sharing - Images appear instantly for all chat participants
  • βœ… Drag & Drop Interface - Intuitive file selection with visual feedback
  • βœ… Progress Indicators - Real-time upload progress with loading states
  • βœ… Image Viewer - Full-screen image viewing with download functionality
  • βœ… Optimistic UI - Immediate image preview with server confirmation
  • βœ… Caption Support - Add captions to images before sending
  • βœ… Accessibility - Screen reader compatible with proper ARIA labels
  • βœ… Mobile Optimized - Touch-friendly interface for all devices

Real-time Features

  • βœ… Smart chat list updates - automatic refresh when new messages arrive with guaranteed delivery
  • βœ… Global state synchronization - all participants see updates instantly with Ably's reliable messaging
  • βœ… Proper room management - users automatically join/leave appropriate chat rooms with presence tracking
  • βœ… Unread count accuracy - real-time updates with proper read receipt tracking
  • βœ… Connection recovery - automatic reconnection with message queue to prevent data loss
  • βœ… Message history - 24-hour message persistence for seamless experience

Code Quality

  • βœ… Full TypeScript coverage with proper interfaces and type safety
  • βœ… Modular architecture with reusable hooks and components
  • βœ… Comprehensive error boundaries and graceful degradation
  • βœ… Clean separation of concerns between UI, business logic, and data layers

Future Enhancements

Potential Features (Not Currently Implemented)

  • File/image sharing - Upload and share multimedia content
  • Message search - Search through chat history across all conversations
  • Push notifications - Browser notifications for new messages when app is minimized
  • Message threading - Reply to specific messages with threaded conversations
  • Voice messages - Record and send audio messages
  • Video calls - Integrate WebRTC for video/audio calling
  • Message encryption - End-to-end encryption for enhanced privacy
  • Admin controls - Advanced group management features
  • Image editing - Basic crop, rotate, and filter capabilities before sending

Technical Improvements

  • Message pagination - Load older messages on demand for better performance
  • Offline support - Queue messages when connection is lost
  • Message delivery status - Sent/delivered/read indicators
  • Custom themes - User-selectable color schemes and customization
  • Mobile app - React Native version for iOS/Android

Getting Started

Prerequisites

  • Node.js 18+ and npm
  • PostgreSQL database (local or cloud)
  • Clerk account for authentication
  • Ably account for real-time messaging (free tier available)

Quick Start

  1. Clone the repository
  2. Install dependencies: npm install
  3. Set up environment variables (see setup instructions above) - Don't forget your Ably API key!
  4. Run database migrations: npm run db:push
  5. Start development server: npm run dev
  6. Visit http://localhost:3000

Usage

  1. Sign up/Sign in using the authentication pages
  2. Create a direct chat by searching for users and starting a conversation
  3. Create a group using the sidebar controls and invite multiple users
  4. Send messages which appear instantly for all participants
  5. Manage groups by adding/removing members or updating group info

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes with proper TypeScript types
  4. Test thoroughly with both direct and group chats
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

Development Guidelines

  • Maintain TypeScript type safety throughout
  • Test real-time features with multiple browser windows to verify Ably connection
  • Test connection recovery by toggling network on/off
  • Ensure responsive design works on all screen sizes
  • Follow the existing code patterns and component structure
  • Monitor Ably dashboard for real-time message statistics

License

MIT License - see LICENSE file for details


Built with ❀️ using Next.js 15, Ably, and modern web technologies.

Why Ably?

We migrated from Pusher to Ably for:

  • 99.999% uptime SLA vs 99.9%
  • Guaranteed message delivery with automatic retries
  • 30x more free messages (6M/month vs 200K)
  • Message persistence for up to 24 hours
  • Connection recovery - no messages lost on reconnect
  • Better monitoring and debugging tools
  • Perfect for E2EE - reliable delivery is critical for encrypted messages

About

A modern, full-featured realtime chat application built with Next.js 15, TypeScript, Pusher, Clerk Auth, and PostgreSQL. Features both direct messaging and group chat functionality with a polished user experience.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •