A production-ready real-time chat application built with the Ignitia web framework, featuring WebSocket support, JWT authentication, PostgreSQL database, and a modern web interface.
- β Real-time Messaging - WebSocket-based instant messaging
- β User Authentication - JWT-based secure authentication with Argon2 password hashing
- β Database Persistence - PostgreSQL with message history
- β User Search - Find users by username or full name
- β Message Status - Track read/unread messages
- β Connection Status - Real-time connection indicators
- β Auto Reconnection - Automatic WebSocket reconnection on disconnection
- β Responsive UI - Modern, mobile-friendly interface
- β Type Safety - Comprehensive type system with Serde serialization
- β Production Ready - Error handling, logging, and security features
chat-app/
βββ Cargo.toml
βββ .env
βββ README.md
βββ migrations/
β βββ 001_initial_schema.sql
βββ static/
β βββ index.html # Landing page
β βββ login.html # User login
β βββ register.html # User registration
β βββ chat.html # Main chat interface
β βββ search.html # User search page
βββ src/
βββ main.rs # Application entry point
βββ config.rs # Configuration management
βββ db/
β βββ mod.rs
β βββ pool.rs # Database connection pool
β βββ models.rs # Database models
βββ handlers/
β βββ mod.rs
β βββ auth.rs # Authentication handlers
β βββ user.rs # User management
β βββ chat.rs # Chat & WebSocket handlers
β βββ static_files.rs # Static file serving
βββ middleware/
β βββ mod.rs
β βββ jwt_auth.rs # JWT authentication middleware
βββ error/
β βββ mod.rs
β βββ app_error.rs # Custom error types
βββ utils/
β βββ mod.rs
β βββ password.rs # Password hashing utilities
β βββ token.rs # JWT token utilities
βββ types/
βββ mod.rs
βββ auth.rs # Auth request/response types
βββ chat.rs # Chat message types
βββ user.rs # User types
- Rust 1.70 or higher
- PostgreSQL 12 or higher
- Cargo (comes with Rust)
git clone https://github.com/AarambhDevHub/ignitia-chat-app.git
cd ignitia-chat-app
# Create database
createdb chatapp
# Or using psql
psql -U postgres
CREATE DATABASE chatapp;
\q
Create a .env file in the project root:
DATABASE_URL=postgres://postgres:password@localhost/chatapp
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production
JWT_EXPIRATION_HOURS=24
SERVER_HOST=127.0.0.1
SERVER_PORT=8080
RUST_LOG=info
cargo build --release
cargo run
cargo run --release
The server will start at http://localhost:8080
[dependencies]
ignitia = { version = "0.2.4", features = ["websocket"] }
tokio = { version = "1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "postgres", "uuid", "chrono"] }
argon2 = "0.5"
jsonwebtoken = "9.0"
uuid = { version = "1.0", features = ["v4", "serde"] }
chrono = { version = "0.4", features = ["serde"] }
dotenvy = "0.15"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
async-trait = "0.1"
once_cell = "1.19"
thiserror = "1.0"
bytes = "1.7"
dashmap = "6.0"
mime_guess = "2.0"
-- Users table
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
fullname VARCHAR(100),
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Messages table
CREATE TABLE messages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
sender_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
receiver_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
content TEXT NOT NULL,
is_read BOOLEAN DEFAULT FALSE,
created_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT different_users CHECK (sender_id != receiver_id)
);
-- Indexes for performance
CREATE INDEX idx_messages_receiver ON messages(receiver_id, created_at DESC);
CREATE INDEX idx_messages_sender ON messages(sender_id, created_at DESC);
CREATE INDEX idx_messages_conversation ON messages(sender_id, receiver_id, created_at DESC);
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/auth/register |
Register new user | No |
| POST | /api/auth/login |
Login user | No |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /api/users/search?q=query |
Search users | Yes |
| GET | /api/users/profile |
Get current user profile | Yes |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/messages/send |
Send message | Yes |
| GET | /api/messages/:user_id |
Get message history | Yes |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| WS | /ws/chat?token=JWT |
WebSocket connection | Yes (via query param) |
{
"type": "message",
"from_user_id": "uuid",
"from_username": "string",
"to_user_id": "uuid",
"content": "string",
"timestamp": "ISO8601"
}
{
"type": "message",
"from_user_id": "uuid",
"from_username": "string",
"to_user_id": "uuid",
"content": "string",
"timestamp": "ISO8601"
}
{
"type": "online",
"user_id": "uuid",
"username": "string"
}
{
"type": "offline",
"user_id": "uuid"
}
- Navigate to
http://localhost:8080 - Click "Register"
- Fill in username, email, and password
- Submit the form
- Enter your credentials
- You'll be redirected to the chat page
- Select a user from the sidebar
- Type your message in the input box
- Press Enter or click Send
- Messages are delivered instantly via WebSocket
- Use the search box in the sidebar
- Or click "Find Users" to see all available users
- Click on any user to start a conversation
- Connection: Client connects to
/ws/chat?token=JWT - Authentication: Server validates JWT token from query parameter
- Registration: User connection stored in
DashMap<Uuid, Sender> - Broadcasting: Messages broadcast to recipient if online
- Persistence: All messages saved to PostgreSQL database
- Cleanup: On disconnect, user removed from active connections
Client β Middleware (JWT Auth) β Handler β Database β Response
Client β WebSocket Handler β Verify Token β Store Connection
β
Message β Parse JSON β Validate β Save to DB β Broadcast to Recipient
- Argon2 Password Hashing - Industry-standard password security
- JWT Authentication - Stateless authentication with expiration
- SQL Injection Prevention - Parameterized queries with SQLx
- XSS Protection - HTML escaping in frontend
- Path Traversal Prevention - Static file serving validation
- HTTPS Ready - TLS support via Ignitia framework
- Connection Pooling - 5-20 database connections
- Async Runtime - Tokio for high concurrency
- Zero-copy - Efficient WebSocket message handling
- Database Indexes - Optimized query performance
- Release Builds - LTO and optimizations enabled
# Run tests
cargo test
# Run with logging
RUST_LOG=debug cargo run
# Check PostgreSQL is running
pg_isready
# Test connection
psql -U postgres -d chatapp
- Check browser console for errors
- Verify JWT token is valid
- Ensure WebSocket feature is enabled in Ignitia
# Change SERVER_PORT in .env file
SERVER_PORT=8081
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
Built with β€οΈ using Ignitia - A blazing fast Rust web framework
- Ignitia Framework - Modern Rust web framework with WebSocket support
- Tokio - Async runtime for Rust
- SQLx - Compile-time checked SQL queries
- Argon2 - Password hashing library
- Ignitia Framework - The underlying web framework
Note: This is a demonstration project showcasing Ignitia's WebSocket capabilities with database integration. For production use, consider adding additional features like:
- Rate limiting
- Message encryption
- File/image sharing
- Group chats
- Typing indicators
- Message reactions
- Push notifications