KubeMQ
IntegrationsFastStreamReference

API Reference

The kubemq-faststream public API — exported symbols, the pattern and ack enums, StartPosition, subscriber config, and the broker.request() RPC signature.

This page is the API reference for kubemq-faststream: every exported symbol, the messaging-pattern and acknowledgement enums, the Events Store start positions, the subscriber configuration fields, and the broker.request() request-reply signature. For connection options, URL formats, and environment variables, see the configuration reference.

Public API Surface

Everything in the package's __all__ is importable directly from kubemq_faststream. The classes are loaded lazily — the package only checks that the kubemq SDK is installed when one of these symbols is first accessed.

from kubemq_faststream import (
    KubeMQBroker,
    KubeMQRouter,
    KubeMQPublisher,
    KubeMQMessage,
    KubeMQPublishCommand,
    TestKubeMQBroker,
    AckPolicy,
    StartPosition,
    KubeMQPattern,
    FeatureNotSupportedException,
    __version__,
)
SymbolKindPurpose
KubeMQBrokerclassMain broker adapter — lifecycle, publish, request, and subscriber/publisher decorators
KubeMQRouterclassModular handler group with prefix propagation, included on a broker via include_router
KubeMQPublisherclassPublisher object created by @broker.publisher(...) for auto-publishing handler return values
KubeMQMessageclassDecoded message wrapper passed to handlers (headers, body, ack/nack)
KubeMQPublishCommandclassInternal publish command describing destination, pattern, and per-message options
TestKubeMQBrokerclassIn-memory test broker that routes published messages to matching subscribers without a live broker
AckPolicyenumMessage acknowledgement strategy for queue subscribers (re-exported from FastStream)
StartPositionenumEvents Store replay start position
KubeMQPatternenumThe five KubeMQ messaging patterns
FeatureNotSupportedExceptionexceptionRaised when a feature is unsupported for the selected pattern
__version__strInstalled package version

KubeMQPattern

The KubeMQPattern enum names the five KubeMQ messaging patterns. You rarely reference it directly — the @broker.subscriber(...) keyword and the broker.publish(...)/broker.request(...) keyword select the pattern for you — but it appears in subscriber configuration and KubeMQPublishCommand.

ValueStringSubscriber keyword
KubeMQPattern.EVENTS"events"events=
KubeMQPattern.EVENTS_STORE"events_store"events_store=
KubeMQPattern.QUEUES"queues"queues=
KubeMQPattern.COMMANDS"commands"commands=
KubeMQPattern.QUERIES"queries"queries=

KubeMQPattern is a StrEnum, so each member compares equal to its string value.

AckPolicy

AckPolicy is re-exported from faststream.middlewares and controls how queue messages are settled. It applies to the Queues pattern; for Events and Events Store (fire-and-forget) the ack/nack operations are no-ops.

AckPolicyBehavior
AckPolicy.ACKAck on success, nack (requeue) on handler error. Default.
AckPolicy.NACK_ON_ERRORNack (requeue) on handler error.
AckPolicy.REJECT_ON_ERRORReject on handler error (server-configured disposition).
AckPolicy.ACK_FIRSTAck immediately before the handler runs (at-most-once).
AckPolicy.MANUALNo automatic ack — the handler must call msg.ack() / msg.nack().
from kubemq_faststream import KubeMQBroker, AckPolicy

broker = KubeMQBroker("kubemq://localhost:50000")


@broker.subscriber(queues="tasks", ack_policy=AckPolicy.ACK)
async def process_task(task: dict) -> None:
    ...  # auto-acked on success, nacked on exception

See Queues for the full settlement walkthrough.

StartPosition

StartPosition selects where an Events Store subscriber begins reading. Three of the six positions require a companion start_value (passed to @broker.subscriber(events_store=..., start_position=..., start_value=...)). start_value is typed int | float | None and must be a positive number for the positions that require it.

StartPositionStringRequires start_valueDescription
START_FROM_NEW"start_from_new"NoOnly messages published after subscribe. Default.
START_FROM_FIRST"start_from_first"NoReplay from the first stored message.
START_FROM_LAST"start_from_last"NoStart from the last stored message.
START_AT_SEQUENCE"start_at_sequence"YesStart at a specific sequence number.
START_AT_TIME"start_at_time"YesStart at a Unix timestamp (seconds).
START_AT_TIME_DELTA"start_at_time_delta"YesStart from N seconds ago.
from kubemq_faststream import KubeMQBroker, StartPosition

broker = KubeMQBroker("kubemq://localhost:50000")


@broker.subscriber(
    events_store="audit-log",
    start_position=StartPosition.START_FROM_FIRST,
)
async def on_audit(msg: dict) -> None:
    print(f"Audit event: {msg}")

See Events Store for replay walkthroughs.

Subscriber Configuration

The @broker.subscriber(...) decorator accepts a pattern keyword (events=, events_store=, queues=, commands=, or queries=) plus the fields below. The pattern keyword's value is the channel name. The configuration is backed by KubeMQSubscriberConfig.

FieldTypeDefaultApplies toDescription
channelstrAllChannel name (the value of the pattern keyword).
patternKubeMQPatternEVENTSAllResolved messaging pattern (set by the keyword used).
groupstr | NoneNoneEvents, QueuesLoad-balancing group — only one member of a group receives each message.
max_messagesint1QueuesMaximum messages pulled per receive batch.
wait_timeoutint60QueuesSeconds to wait for queue messages per poll.
ack_policyAckPolicyACKQueuesAcknowledgement strategy (see AckPolicy).

For Events Store subscribers, also pass start_position and (where required) start_value as described in StartPosition.

broker.request() — RPC Reference

broker.request(...) performs request-reply RPC. It is valid only for the Commands and Queries patterns — calling it without commands= or queries= raises FeatureNotSupportedException. The call blocks until the handler responds or the timeout elapses, and returns the handler's response.

async def request(
    message,
    /,
    *,
    commands: str | None = None,
    queries: str | None = None,
    timeout: int | None = None,
    cache_key: str | None = None,
    cache_ttl: int | None = None,
    headers: dict[str, str] | None = None,
    metadata: str = "",
    correlation_id: str | None = None,
) -> Any: ...
ArgumentTypeApplies toDescription
commands / queriesstr | NoneTarget channel. Exactly one selects the pattern.
timeoutint | NoneCommands, QueriesSeconds to wait for the response. Falls back to default_cq_timeout (30) when unset.
cache_keystr | NoneQueriesServer-side cache key. A second request with the same key returns the cached response without invoking the handler.
cache_ttlint | NoneQueriesCache lifetime in seconds.
headersdict[str, str] | NoneCommands, QueriesMessage tags.
metadatastrCommands, QueriesFree-form metadata string.
correlation_idstr | NoneCommands, QueriesCorrelation identifier.
basic_command.py
@broker.subscriber(commands="example.commands.basic")
async def handle_command(msg: dict) -> dict:
    return {"status": "executed", "action": msg["action"], "success": True}

# caller
response = await broker.request(
    {"action": "create_user", "name": "Alice"},
    commands="example.commands.basic",
)

For fire-and-forget commands or queries where no reply is expected, subscribe with no_reply=True and send with broker.publish(commands=...) / broker.publish(queries=...) instead of broker.request(...). The handler runs, but no response is returned to the caller.

See Commands & Queries for the full walkthrough, including caching and timeout handling.

Internal Architecture

On connect, KubeMQBroker creates three KubeMQ SDK async clients, each on its own gRPC channel, sharing one set of connection settings (URL, client ID, auth token, TLS, size limits, keepalive):

Each publish, request, and @broker.subscriber call is routed to the matching client by its pattern keyword.

The clients are created when the broker connects (via start() or async with broker:) and closed on stop(). See Concepts for the lifecycle and the message pipeline.

Was this page helpful?

On this page