philodi-dev/rust-chat-websocket
A real-time chat application built with Rust, featuring a WebSocket-based backend and a modern web frontend.
Rust Chat WebSocket
A real-time chat application built with Rust, featuring a WebSocket-based backend and a modern web frontend.
๐ Features
- Real-time messaging - Instant message delivery via WebSocket connections
- User management - Dynamic username changes and user list updates
- Modern UI - Clean, responsive interface built with Yew (Rust frontend framework)
- System notifications - Automatic notifications for user events (username changes, etc.)
- Scalable architecture - Modular design with separate backend, frontend, and common libraries
๐๏ธ Architecture
This project is organized as a Rust workspace with three main components:
Backend (backend/)
- Framework: Rocket with WebSocket support
- Features:
- WebSocket connection management
- Real-time message broadcasting
- User session handling
- System notifications
Frontend (frontend/)
- Framework: Yew (Rust frontend framework)
- Features:
- Real-time message display
- User list management
- Username change functionality
- Responsive Bootstrap-based UI
Common (common/)
- Purpose: Shared data structures and types
- Features:
- WebSocket message definitions
- Chat message structures
- Serialization/deserialization types
๐ Prerequisites
- Rust 1.70+ (latest stable version recommended)
- Cargo (comes with Rust)
- Web browser with WebSocket support
๐ ๏ธ Installation & Setup
-
Clone the repository
git clone https://github.com/philodi-dev/rust-chat-websocket cd rust-chat-websocket -
Build the project
cargo build
-
Run the backend server
cargo run -p backend
The server will start on
http://127.0.0.1:8000 -
Run the frontend (in a separate terminal)
cargo run -p frontend
The frontend will be available at the URL shown in the terminal (typically
http://127.0.0.1:8000)
๐ฏ Usage
- Connect to the chat - Open your browser and navigate to the frontend URL
- Send messages - Type your message in the input field and press Enter
- Change username - Use the username field to set your display name
- View users - See the current list of connected users in the sidebar
๐ง Development
Project Structure
rust-chat/
โโโ backend/ # WebSocket server implementation
โโโ frontend/ # Yew-based web frontend
โโโ common/ # Shared types and structures
โโโ Cargo.toml # Workspace configuration
โโโ README.md # This file
Key Dependencies
Backend:
rocket- Web frameworkrocket_ws- WebSocket supportserde- Serializationchrono- Date/time handling
Frontend:
yew- Rust frontend frameworkyew-hooks- React-like hooks for Yewweb-sys- Web API bindings
Common:
serde- Serializationchrono- Date/time handling
Building Individual Components
# Build backend only
cargo build -p backend
# Build frontend only
cargo build -p frontend
# Build common library only
cargo build -p commonRunning Tests
# Run all tests
cargo test
# Run tests for specific component
cargo test -p backend
cargo test -p frontend
cargo test -p common๐ Features in Detail
Real-time Messaging
- Messages are instantly broadcast to all connected users
- System notifications for user events
- Persistent message history during session
User Management
- Dynamic username changes
- Real-time user list updates
- Automatic user assignment on connection
WebSocket Protocol
The application uses a custom WebSocket message protocol:
#[derive(Serialize, Deserialize)]
pub enum WebSocketMessageType {
NewMessage, // Chat message
UsersList, // Updated user list
UsernameChange, // Username change request
}๐ค Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐จโ๐ป Author
Philodi
- Email: me@philodi.com
- Website: https://philodi.com
๐ Acknowledgments
- Built with Rocket web framework
- Frontend powered by Yew Rust framework
- Real-time communication via WebSocket protocol
Note: This is a demonstration project showcasing Rust's capabilities for building full-stack web applications with real-time features.