SmartLink WebSocket Server is a real-time communication server built in Rust using the Tokio and Tungstenite libraries. It supports features like user status updates, private and group messaging, WebRTC signaling, typing indicators, and social media-style moment interactions (e.g., likes, comments, updates, and deletions). The server is designed for scalability and thread safety, utilizing asynchronous programming and a connection management system to handle user connections efficiently.
- User Status Management: Tracks online/offline status and broadcasts updates to relevant users.
- Real-Time Messaging: Supports private and group conversations with message status updates (e.g., read receipts).
- WebRTC Signaling: Facilitates peer-to-peer video/audio call setup.
- Typing Indicators: Notifies users in real-time when someone is typing.
- Moments System: Allows users to publish, like, comment, update, or delete moments, with notifications sent to relevant friends or viewers.
- Connection Cleanup: Automatically removes inactive connections after a timeout period.
- Thread-Safe Design: Uses
ArcandRwLockfor safe concurrent access to shared state.
- Rust (stable, latest version recommended)
- Cargo (Rust's package manager)
- A compatible MongoDB instance (if integrating with a database, not included in this code)
- Environment variable
PORT(optional, defaults to 8080)
-
Clone the repository:
git clone https://github.com/Erio-Harrison/smartlink-websocket-server.git cd smartlink-websocket-server -
Ensure Rust is installed:
rustup update
-
Build the project:
cargo build --release
-
Run the server:
cargo run --release
Alternatively, set a custom port:
PORT=8080 cargo run --release
- The server listens for WebSocket connections at
ws://0.0.0.0:8080(or the specifiedPORT). - Clients must send an initial
UserStatusmessage with asender_idto authenticate and establish a connection. - Supported message types include
NewMessage,MessageStatusUpdate,WebRTCSignal,TypingIndicator,NewMoment,MomentLike,MomentComment,MomentUpdate, andMomentDelete. - Messages are serialized in JSON with camelCase naming conventions (enforced by
serde).
- Port: Set via the
PORTenvironment variable (default: 8080). - Logging: Uses the
tracingcrate for structured logging. Configure viatracing_subscriberin the code. - Timeout: Inactive connections are cleaned up every 30 seconds if no ping is received within 60 seconds.
src/main.rs: Core server logic, including WebSocket handling, connection management, and message processing.- Dependencies (in
Cargo.toml):tokio = { version = "1.25.0", features = ["full"] }: Asynchronous runtimetokio-tungstenite = "0.19.0": WebSocket supportfutures-util = "0.3.28": Utilities for asynchronous streamsserde = { version = "1.0.163", features = ["derive"] }: JSON serialization/deserializationserde_json = "1.0.96": JSON parsing and serializationchrono = { version = "0.4.24", features = ["serde"] }: Timestamp handlingtracing = "0.1.37": Logging frameworktracing-subscriber = "0.3.17": Logging subscriber for formatting
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/your-feature). - Commit your changes (
git commit -m "Add your feature"). - Push to the branch (
git push origin feature/your-feature). - Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
- Name: Harrison
- Email: u7541840@gmail.com
- GitHub: Erio-Harrison
- Built with Rust and the Tokio ecosystem*.
- Inspired by real-time communication needs for the SmartLink project.