Skip to content

Streaming over NATS

doppler.stream moves sample blocks between processes (or hosts) over NATS, in three subject-based patterns that share one wire format — a dp_header_t (sample rate, centre frequency, sample type, an auto timestamp_ns, and a per-socket sequence counter) followed by the raw sample bytes. Because the header is shared, a C transmitter and a Python receiver interoperate freely.

Every pattern needs a nats-server reachable at the endpoint's host:port — plain nats-server is enough for PUB/SUB and REQ/REP; PUSH/PULL rides the JetStream work-queue tier, so start it with nats-server -js (NATS Broker installs and runs one). Endpoints are nats://host:port/subject.

Source: src/doppler/stream/__init__.py

For the C side and a runnable end-to-end walk-through, see the Streaming examples.

Socket patterns

Pattern Sender Receiver NATS tier Use
PUB / SUB Publisher Subscriber Core NATS fan-out broadcast; subscribers may drop
PUSH/PULL Push Pull JetStream work-queue load-balanced pipeline; durable, acked
REQ / REP Requester Replier NATS request/reply request/response (lock-step)

Every socket is a context manager and releases the GIL while blocked waiting on NATS, so receive loops thread cleanly. The sender's sample_type fixes the NumPy dtype on both ends:

Constant BLUE code dtype layout
CF32 "CF" numpy.complex64 complex I/Q
CF64 "CD" numpy.complex128 complex I/Q
CI8 "CB" numpy.int8 interleaved I/Q, length 2n
CI16 "CI" numpy.int16 interleaved I/Q, length 2n
CI32 "CL" numpy.int32 interleaved I/Q, length 2n

A format's value is its two-character Midas BLUE code, packed little-endian — the same characters the same samples get in a BLUE file, so nothing translates between the wire and the file. TLM16 is the exception in the same argument slot: it is a frame kind, not a sample format (telemetry records, Publisher only — see Telemetry), because BLUE has no code for a record stream.

recv() returns (samples, header) where header is a dict of the decoded dp_header_t fields: format, kind, version, flags, data_rep, payload_bytes, sequence, timestamp_ns, sample_rate, center_freq and num_samples. The full byte layout, and what a receiver validates before handing anything back, is in Design: Streaming.

PUB / SUB — broadcast

The publisher fans out to every subscriber on the subject; a slow subscriber drops frames rather than back-pressuring the sender. Core NATS has no backlog, so a subscriber must already be listening before the publish — start the Subscriber first (or add a brief warm-up sleep).

import time
import numpy as np
from doppler.stream import Publisher, Subscriber, CF64

# receiver: subscribe first. Core NATS drops a frame sent before the
# subscription reaches the broker, so give it a moment.
with Subscriber("nats://127.0.0.1:4222/iq") as sub:
    time.sleep(0.5)

    # transmitter
    with Publisher("nats://127.0.0.1:4222/iq", CF64) as pub:
        iq = np.exp(2j * np.pi * 1e3 * np.arange(1000) / 1e6)   # complex128
        pub.send(iq, sample_rate=1e6, center_freq=2.4e9)
        # A send returns once the client holds the frame; close() then
        # flushes for at most 500 ms and reports nothing. drain() waits
        # for the server and raises if the tail could not get there.
        pub.drain()

    samples, header = sub.recv(timeout_ms=1000)
    assert len(samples) == 1000
    assert header["sample_rate"] == 1e6
    print(header["sample_rate"], header["sequence"], len(samples))

PUSH / PULL — pipeline

PUSH publishes onto a durable JetStream work-queue subject; JetStream load-balances each frame to exactly one connected PULL worker, giving a back-pressured, at-least-once work queue. Call Pull.ack() once a frame has been fully processed. Requires a JetStream-enabled broker (nats-server -js).

import numpy as np
from doppler.stream import Push, Pull, CF64

with Push("nats://127.0.0.1:4222/work", CF64) as push:
    push.send(np.zeros(4096, dtype=np.complex128), sample_rate=1e6)

with Pull("nats://127.0.0.1:4222/work") as pull:
    samples, header = pull.recv()       # blocks until a frame arrives
    pull.ack(samples)

A worker may hold WORK_QUEUE_MAX_ACK_PENDING frames delivered and not yet acked. Past that, the server hands it no new frames, only redeliveries of the pending ones, each an AckWait after the last, so a worker that never acks stalls at exactly that count. An ack is fire-and-forget, so a lost one is a redelivery too: the header's sequence, which counts each producer's frames from 0, is how a worker can tell one from a new frame.

A pipeline of blocks

The doppler compose CLI builds a pipeline from exactly this pattern: each stage runs as its own process, joined to the next by a PUSH/PULL subject. A stage is a Block, its settings a BlockConfig (a pydantic model with defaults), and register makes it reachable by name. CLI & Pipelines walks through writing one, and python -m doppler.examples.custom_block_demo runs one.

REQ / REP — request/response

A Requester sends a sample block and blocks for the Replier's response; the two alternate strictly, over NATS's native request/reply.

import numpy as np
from doppler.stream import Requester, Replier, CF64

# server: receive a request, send a reply
with Replier("nats://127.0.0.1:4222/ctrl", CF64) as rep:
    req, header = rep.recv()
    rep.send(req * 2)                   # echo-and-scale, say

# client: send, then receive the reply
with Requester("nats://127.0.0.1:4222/ctrl", CF64) as req:
    req.send(np.ones(256, dtype=np.complex128), sample_rate=1e6)
    resp, header = req.recv()

Timestamps

get_timestamp_ns() returns the same CLOCK_REALTIME nanosecond stamp the senders embed in each header, so a receiver can compute end-to-end latency against header["timestamp_ns"].

Stopping a blocked receive

The interrupt is no longer part of this module. It was never a streaming feature — the same flag ends a ring wait and a capture read — so it now lives on its own, as an object rather than as six free functions:

import numpy as np

from doppler.interrupt import Interrupt

it = Interrupt(np.array([], dtype=np.int32))

# Requested from another thread, or from a SIGINT handler this guard
# installed. A Subscriber.recv() blocked in the NATS client raises
# KeyboardInterrupt; so does a ring wait, and so does a capture read.
it.interrupt()
assert it.interrupted()
it.resume()

doppler.stream.interrupt_on_sigint(), interrupt(), resume(), interrupted(), set_interrupt_latency_ms() and interrupt_latency_ms() are gone, not deprecated. See Stopping a Run for the object that replaces them and Ending a wait for the contract it serves on all three transports.