| Crates.io | ubiquity-transport |
| lib.rs | ubiquity-transport |
| version | 0.1.1 |
| created_at | 2025-07-23 22:35:28.552216+00 |
| updated_at | 2025-07-23 23:07:17.357103+00 |
| description | Transport abstraction for WASM and native consciousness mesh |
| homepage | |
| repository | https://github.com/ubiquity/ubiquity-rs |
| max_upload_size | |
| id | 1765335 |
| size | 120,507 |
A platform-agnostic transport layer for the Ubiquity consciousness mesh, enabling seamless communication across native and WASM environments.
use ubiquity_transport::prelude::*;
use ubiquity_core::MeshMessage;
#[tokio::main]
async fn main() -> Result<()> {
// Automatically select best transport for platform
let transport = TransportFactory::create(TransportType::Auto)?;
// Start listening
let mut listener = transport.listen("/tmp/my-service.sock").await?;
// Accept connections
let mut conn = listener.accept().await?;
// Send/receive messages
let msg = MeshMessage::Heartbeat {
agent_id: "my-agent".to_string(),
timestamp: chrono::Utc::now(),
};
conn.send(&msg).await?;
Ok(())
}
// Native Unix sockets
#[cfg(not(target_arch = "wasm32"))]
let transport = TransportFactory::create(TransportType::UnixSocket)?;
// WASM MessageChannel
#[cfg(target_arch = "wasm32")]
let transport = TransportFactory::create(TransportType::MessageChannel)?;
// WebSocket (works everywhere)
let transport = TransportFactory::create(TransportType::WebSocket)?;
let transport = TransportBuilder::new(TransportType::Auto)
.max_message_size(2 * 1024 * 1024) // 2MB
.connect_timeout_ms(10000) // 10 seconds
.build()?;
The transport layer follows a simple trait-based design:
#[async_trait]
pub trait Transport: Send + Sync {
async fn connect(&self, target: &str) -> Result<Box<dyn Connection>>;
async fn listen(&self, addr: &str) -> Result<Box<dyn Listener>>;
fn transport_type(&self) -> &'static str;
}
#[async_trait]
pub trait Connection: Send + Sync {
async fn send(&mut self, msg: &MeshMessage) -> Result<()>;
async fn recv(&mut self) -> Result<Option<MeshMessage>>;
async fn close(&mut self) -> Result<()>;
}
#[async_trait]
pub trait Listener: Send + Sync {
async fn accept(&mut self) -> Result<Box<dyn Connection>>;
fn addr(&self) -> String;
}
Run tests for your platform:
# Native tests
cargo test
# WASM tests (requires wasm-pack)
wasm-pack test --browser
# Specific transport tests
cargo test --features native
cargo test --features websocket
Check out the examples directory:
# Run consciousness mesh example
cargo run --example consciousness_mesh
native: Enable Unix socket transport (default on native platforms)websocket: Enable WebSocket transportwasm: Enable WASM-specific transports (MessageChannel, BroadcastChannel)See LICENSE file in the root of the repository.