| Crates.io | wsforge-core |
| lib.rs | wsforge-core |
| version | 0.1.1 |
| created_at | 2025-10-15 11:06:05.662392+00 |
| updated_at | 2025-10-17 04:00:52.267236+00 |
| description | Core library for WsForge WebSocket framework |
| homepage | https://github.com/aarambhdevhub/wsforge |
| repository | https://github.com/aarambhdevhub/wsforge |
| max_upload_size | |
| id | 1884190 |
| size | 238,261 |
Build real-time applications with ease and exceptional performance
Add WsForge to your Cargo.toml:
[dependencies]
wsforge = "0.1.0"
tokio = { version = "1.40", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
use wsforge::prelude::*;
async fn echo(msg: Message) -> Result<Message> {
Ok(msg)
}
#[tokio::main]
async fn main() -> Result<()> {
let router = Router::new()
.default_handler(handler(echo));
println!("🚀 WebSocket server running on ws://127.0.0.1:8080");
router.listen("127.0.0.1:8080").await?;
Ok(())
}
use wsforge::prelude::*;
use std::sync::Arc;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
struct ChatMessage {
username: String,
text: String,
}
async fn chat_handler(
Json(msg): Json<ChatMessage>,
conn: Connection,
State(manager): State<Arc<ConnectionManager>>,
) -> Result<()> {
println!("{}: {}", msg.username, msg.text);
// Broadcast to everyone except sender
let response = serde_json::to_string(&msg)?;
manager.broadcast_except(conn.id(), Message::text(response));
Ok(())
}
#[tokio::main]
async fn main() -> Result<()> {
let router = Router::new()
.default_handler(handler(chat_handler))
.on_connect(|manager, conn_id| {
println!("✅ {} joined (Total: {})", conn_id, manager.count());
})
.on_disconnect(|manager, conn_id| {
println!("❌ {} left (Total: {})", conn_id, manager.count());
});
router.listen("127.0.0.1:8080").await?;
Ok(())
}
wsforge/
├── wsforge-core/ # Core framework implementation
│ ├── src/
│ │ ├── connection.rs # Connection management
│ │ ├── message.rs # Message types
│ │ ├── handler.rs # Handler traits
│ │ ├── extractor.rs # Type extractors
│ │ ├── router.rs # Routing logic
│ │ ├── state.rs # State management
│ │ ├── error.rs # Error types
│ │ └── static_files.rs # Static file serving
│ └── Cargo.toml
├── wsforge-macros/ # Procedural macros
│ ├── src/
│ │ └── lib.rs
│ └── Cargo.toml
├── wsforge/ # Main crate (re-exports)
│ ├── src/
│ │ └── lib.rs
│ └── Cargo.toml
├── examples/ # Example applications
│ ├── echo/ # Simple echo server
│ ├── chat/ # CLI chat application
│ ├── chat-web/ # Web-based chat with UI
│ └── realtime-game/ # Real-time game example
├── docs/ # Documentation
├── CONTRIBUTING.md # Contribution guidelines
├── LICENSE # MIT License
└── README.md # This file
# Simple echo server
cargo run --example echo
# CLI chat application
cargo run --example chat
# Web chat with beautiful UI
cargo run --example chat-web
# Open http://127.0.0.1:8080 in your browser
# Real-time game server
cargo run --example realtime-game
Web Chat (examples/chat-web):
Handlers are async functions that process WebSocket messages:
// Simple handler
async fn simple() -> Result<String> {
Ok("Hello!".to_string())
}
// Handler with extractors
async fn complex(
Json(data): Json<MyStruct>,
conn: Connection,
State(manager): State<Arc<ConnectionManager>>,
) -> Result<JsonResponse<Response>> {
// Your logic here
}
Type-safe data extraction from messages and context:
| Extractor | Description |
|---|---|
Message |
Raw WebSocket message |
Json<T> |
Deserialize JSON automatically |
Connection |
Access to active connection |
State<T> |
Shared application state |
ConnectInfo |
Connection metadata |
Data |
Raw binary data |
Send messages to multiple connections efficiently:
// Broadcast to all
manager.broadcast(msg);
// Broadcast except sender
manager.broadcast_except(conn.id(), msg);
// Broadcast to specific connections
manager.broadcast_to(&["conn_1", "conn_2"], msg);
WsForge is designed for high performance:
We welcome contributions! Please see our Contributing Guide for details.
git checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)# Clone the repository
git clone https://github.com/aarambhdevhub/wsforge.git
cd wsforge
# Build the project
cargo build --all
# Run tests
cargo test --all
# Run examples
cargo run --example echo
This project is licensed under the MIT License - see the LICENSE file for details.
If you find WsForge useful, please consider giving it a ⭐ on GitHub! It helps others discover the project.
Made with ❤️ by Aarambh Dev Hub
Building the future of real-time applications in Rust