| Crates.io | wasmcloud-provider-messaging-websocket |
| lib.rs | wasmcloud-provider-messaging-websocket |
| version | 0.1.0 |
| created_at | 2025-11-20 17:36:41.387756+00 |
| updated_at | 2025-11-20 17:36:41.387756+00 |
| description | A capability provider that satisfies the 'wasmcloud:messaging' contract using WebSocket as a backend. |
| homepage | |
| repository | https://github.com/64BitAsura/wasm-cloud-websocket-provider |
| max_upload_size | |
| id | 1942304 |
| size | 224,194 |
A wasmCloud capability provider that implements the wasmcloud:messaging contract using WebSocket as the transport backend. This provider supports both WebSocket client and WebSocket server modes with comprehensive session management capabilities.
The provider can be configured using the following settings when establishing a link:
| Property | Description | Default | Applies To |
|---|---|---|---|
MODE |
Operation mode: "client" or "server" | client |
Both |
URI |
WebSocket server URI (client mode) or bind address (server mode) Examples: "ws://localhost:8080" or "0.0.0.0:8080" |
ws://127.0.0.1:8080 |
Both |
AUTH_TOKEN |
Optional authentication token | None | Client |
CONNECT_TIMEOUT_SEC |
Connection timeout in seconds | 30 |
Client |
ENABLE_SESSION_TRACKING |
Enable session tracking for targeted messaging | true |
Both |
HEADER_<name> |
Custom headers (e.g., HEADER_Authorization) |
None | Client |
Connect to an external WebSocket server and enable bidirectional communication:
let mut config = HashMap::new();
config.insert("MODE".to_string(), "client".to_string());
config.insert("URI".to_string(), "ws://example.com:8080".to_string());
let provider = WebSocketMessagingProvider::from_config(config)?;
// Link consumer component (to send messages)
provider.receive_link_config_as_target("consumer-id", HashMap::new()).await?;
// Link handler component (to receive messages from remote server) 🆕
provider.receive_link_config_as_source("handler-id", HashMap::new()).await?;
// Consumer can send messages to remote server
provider.publish("consumer-id", message).await?;
// Handler components automatically receive messages from remote server
// Components can reply back using session ID from reply-to field
Start a WebSocket server to accept incoming connections:
let mut config = HashMap::new();
config.insert("MODE".to_string(), "server".to_string());
config.insert("URI".to_string(), "0.0.0.0:8080".to_string());
let mut provider = WebSocketMessagingProvider::from_config(config)?;
provider.start_server_if_needed().await?;
// Server is now listening for WebSocket connections at ws://0.0.0.0:8080/ws
cargo build --release
cargo run --example basic_usage
cargo run --example client_broadcast
cargo run --example server_mode
Run the test suite:
cargo test
To run tests that require network access (ignored by default in CI):
cargo test -- --ignored
Note: Some integration tests connect to external WebSocket servers and are marked as #[ignore] to prevent CI failures. These tests validate real-world network scenarios but are not required for standard development.
When session tracking is enabled (default), the provider maintains a mapping of session IDs to component IDs for WebSocket client connections.
In client mode, when the provider connects to a remote WebSocket server, it can broadcast incoming messages from that server to all registered handler components:
// Provider connects to remote WebSocket server
let provider = WebSocketMessagingProvider::from_config(config)?;
// Link consumer (sends messages to remote server)
provider.receive_link_config_as_target("consumer-id", HashMap::new()).await?;
// Link handler(s) (receive messages from remote server)
provider.receive_link_config_as_source("handler-1", HashMap::new()).await?;
provider.receive_link_config_as_source("handler-2", HashMap::new()).await?;
// Consumer sends message to remote server
provider.publish("consumer-id", message).await?;
// Remote server responds → message is broadcast to ALL handler components
// handler-1 and handler-2 both receive the message
// Handlers can reply back to remote server using session ID
let sessions = provider.list_sessions().await;
if let Some((session_id, _)) = sessions.first() {
provider.send_to_session(session_id, reply_message).await?;
}
Key Features:
In server mode, the provider tracks all connected WebSocket clients:
// List all connected WebSocket clients
let clients = provider.list_ws_clients().await?;
// Send message to a specific client
provider.send_to_session("client-session-id", BrokerMessage {
subject: "notification".to_string(),
body: Bytes::from("Hello!"),
reply_to: None,
}).await?;
// Broadcast message to all clients
provider.broadcast_to_clients(BrokerMessage {
subject: "announcement".to_string(),
body: Bytes::from("System update"),
reply_to: None,
}).await?;
The provider automatically includes reply-to fields in messages to enable request-response patterns:
Client → Server:
{
"subject": "request.data",
"body": "...",
"reply_to": "session-abc-123"
}
Component can reply back using the session ID:
provider.send_to_session("session-abc-123", BrokerMessage {
subject: "response.data",
body: Bytes::from("Response payload"),
reply_to: None,
}).await?;
This provider implements the wasmCloud messaging interface with WebSocket as the transport:
Unlike the NATS messaging provider:
See SECURITY.md for security audit information and best practices.
Both issues are in dependencies of wasmcloud-provider-sdk and do not affect this provider's functionality. See SECURITY.md for details.
Apache-2.0