Skip to content

Publisher Wiki

Kota Yatagai edited this page Nov 7, 2026 · 1 revision

This document walks through the happy path of a Moqtopus Publisher, from establishing a connection with a relay to registering a track, accepting a subscription, and publishing objects. Each description comes with a diagram, which shows how application API calls and incoming relay messages flow through the main classes, and how those classes interact with MsQuic to send and receive data.

Connecting to a relay

An application starts by calling Publisher::connect(), a static factory method that returns a std::unique_ptr<Publisher>. The Publisher owns a SendDataPlane, which manages registered tracks, subscription send state, and outgoing object serialization. Its base class, Session, handles connection setup and session lifecycle.

image

Publisher::connect() creates the Publisher and calls Session::start(). This initializes an MsQuicTransportAdapter and starts the QUIC connection through MsQuic. Once the QUIC connection is established, MsQuic delivers a connection event to MsQuicTransportAdapter::connection_callback(). The adapter invokes callbacks_.connected, which was installed by Session::start(). This callback opens a unidirectional stream and sends the local SETUP message. Opening the stream involves calling MsQuic’s StreamOpen, creating a StreamContext, associating it with the stream callback through SetCallbackHandler, and calling StreamStart. The SETUP bytes are then passed to StreamContext::send(), which calls MsQuic’s StreamSend.

At this point, the QUIC connection is established and the local SETUP has been sent. The MoQ session becomes ready once the peer’s SETUP has also been received.

Receiving the peer’s SETUP

Receiving SETUP involves two stages: accepting the peer-initiated stream and processing the bytes that arrive on it.

Accepting a peer-initiated stream

image

When the relay opens a stream, MsQuic reports it through MsQuicTransportAdapter::connection_callback(). The adapter creates a StreamContext for the incoming stream and passes it to Session::on_peer_stream_started(). The Session creates a PeerStreamGate and installs it as the stream’s sink using StreamContext::set_sink(). A sink is the object that handles incoming stream data and stream lifecycle notifications. PeerStreamGate examines the initial bytes to determine how the stream should be handled. It also buffers incoming data when processing must wait for session setup to complete. It is basically a temporary StreamSink until the stream's purpose is identified.

Completing session setup

image

When SETUP bytes arrive, MsQuic invokes StreamContext::stream_callback(). The StreamContext forwards the received data to PeerStreamGate::on_receive(), which recognizes and validates SETUP before calling Session::handle_peer_setup().

handle_peer_setup() records that the peer’s SETUP has been received and calls maybe_ready(). Once both the local and peer SETUP conditions are satisfied, maybe_ready() transitions the session to Ready and resolves the futures returned by ready(). It then invokes on_ready() and replays callbacks that were deferred while setup was in progress. These are separate actions performed by maybe_ready(). Replaying a buffered stream callback allows its pending bytes to be processed through PeerStreamGate::on_receive() (Thanks kixelated for raising an issue about this).

Applications can wait on the ready() future before registering tracks and publishing objects. Incoming stream creation and SETUP parsing are handled internally by Moqtopus.

Registering a track

Once the session is ready, the application calls Publisher::register_track() to register a track for publication.

image

Track registration has two roles. First, the Publisher registers the track with SendDataPlane, making its name and properties available when processing subscription requests and publishing objects. Second, it announces the track’s namespace to the relay through PUBLISH_NAMESPACE. For a namespace that needs announcing, Publisher::announce_namespace() opens a bidirectional request stream through MsQuicTransportAdapter::open_stream(). It creates a NamespaceAnnouncementFSM, installs it as the stream’s sink, and sends the encoded PUBLISH_NAMESPACE message through StreamContext::send(). The announcement applies to the namespace, rather than an individual track. Registering another track in an already announced namespace does not require a separate announcement for that track. register_track() prepares the track and announces its namespace. The application supplies the actual objects later through publish().

Accepting a subscription

The relay can request objects from a registered track by sending SUBSCRIBE on a new bidirectional request stream.

image

The incoming stream first follows the peer-stream initialization described above: the adapter creates a StreamContext, and the Session installs a PeerStreamGate. When the initial SUBSCRIBE message arrives, the gate passes it to Publisher::handle_peer_request(), which dispatches it to accept_subscribe(). The Publisher looks up the requested track in SendDataPlane and attaches the subscription with its requested delivery options. For an accepted subscription, the Publisher creates a Publisher::SubscriptionRequest and installs it as the stream’s sink, replacing PeerStreamGate. It then sends SUBSCRIBE_OK on the same stream, providing the Track Alias used to identify the subscription’s outgoing objects.

The request stream remains associated with this subscription. Subsequent REQUEST_UPDATE messages are handled by SubscriptionRequest, which updates the corresponding send state in SendDataPlane. This processing happens inside Moqtopus. The application publishes objects through the Publisher API, while SendDataPlane determines which subscriptions should receive them.

Publishing objects

The application calls Publisher::publish() with a PublishedObject. The Publisher checks that the session is ready and forwards the object to SendDataPlane::publish(). SendDataPlane finds the registered track and examines its subscriptions. For each subscription whose forwarding state and filter permit delivery, it selects the sending path according to the object’s delivery kind. The diagram below shows subgroup stream delivery on the left and datagram delivery on the right.

image

For subgroup delivery, SendDataPlane::publish() calls send_on_subgroup_stream(), which manages outgoing streams by Group ID and Subgroup ID within each subscription. When a new stream is needed, it invokes callbacks_.open_data_stream(). This calls Publisher::open_data_stream(), which asks MsQuicTransportAdapter to open a unidirectional stream and return its StreamContext.

SendDataPlane serializes the subgroup header followed by the first object and passes the bytes to StreamContext::send(), which calls MsQuic’s StreamSend. Further objects for the same open subgroup reuse the stream without repeating the header. When an object marks the end of the subgroup or group, the send includes FIN and the stream is removed from the set of reusable subgroup streams.

For datagram delivery, SendDataPlane::publish() calls send_datagram_object(). This serializes the object using encode_object_datagram(), including its Track Alias and object metadata, then invokes callbacks_.send_datagram(bytes). The callback forwards the bytes to MsQuicTransportAdapter::send_datagram(), which calls MsQuic’s DatagramSend. Each object is sent as an individual datagram over the existing QUIC connection.