Prerequisites
Before you begin, ensure you have:- Rust 1.70 or higher
- A basic understanding of async Rust with Tokio
Installation
Add the required crates to yourCargo.toml:
Cargo.toml
Cargo.toml
Your First MoQ Application
Let’s create a simple chat application that publishes messages to a MoQ relay.use moq_lite::Origin;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Create an origin that we can publish to
let origin = Origin::produce();
// Run broadcast and session concurrently
tokio::select! {
res = run_session(origin.consume()) => res,
res = run_broadcast(origin) => res,
}
}
use moq_native::ClientConfig;
use url::Url;
async fn run_session(origin: moq_lite::OriginConsumer) -> anyhow::Result<()> {
// Configure the QUIC client
let client = ClientConfig::default().init()?;
// Connect to relay (use localhost for development)
let url = Url::parse("https://localhost:4443/anon/my-chat")?;
// Establish connection and MoQ handshake
let session = client.with_publish(origin).connect(url).await?;
// Wait until session is closed
session.closed().await.map_err(Into::into)
}
use moq_lite::{Broadcast, Track};
use bytes::Bytes;
async fn run_broadcast(origin: moq_lite::OriginProducer) -> anyhow::Result<()> {
// Create a broadcast
let mut broadcast = Broadcast::produce();
// Create a track for chat messages
let mut track = broadcast.create_track(Track {
name: "chat".to_string(),
priority: 0,
})?;
// Publish broadcast to origin
origin.publish_broadcast("", broadcast.consume());
// Create a group and write frames
let mut group = track.append_group()?;
group.write_frame(Bytes::from_static(b"Hello"))?;
group.write_frame(Bytes::from_static(b"World"))?;
group.finish()?;
// Or use the helper to write a single frame
track.write_frame(Bytes::from_static(b"Hello MoQ!"))?;
Ok(())
}
Complete Example
Running a Local Relay
For local development, you’ll need a relay server:https://localhost:4443 with authentication disabled for the /anon path.
Next Steps
moq-lite
Learn about the core transport API
hang
Work with media codecs and containers
moq-relay
Configure and deploy a relay
API Reference
Browse the full API documentation
Resources
- Full example source
- docs.rs/moq-lite - Complete API reference
- GitHub repository