Complete API reference for the RouteMQ Router class and related components.
The Router class is the core routing component that manages MQTT topic patterns and dispatches messages to appropriate handlers.
Copy from core . router import Router
router = Router () Creates a new router instance with an empty route collection.
on(topic, handler, qos=0, middleware=None, shared=False, worker_count=1)
Register a route handler for a specific topic pattern.
Parameters:
topic (str): MQTT topic pattern with optional parameters (e.g., "devices/{device_id}/status")
handler (callable): Async function to handle matching messages
qos (int, optional): Quality of Service level (0, 1, or 2). Default: 0
middleware (List[Middleware], optional): Middleware to apply to this route. Default: None
shared (bool, optional): Enable shared subscription for load balancing. Default: False
worker_count (int, optional): Number of workers for shared subscriptions. Default: 1
Returns: None
Example:
group(prefix="", middleware=None)
Create a route group with shared prefix and middleware.
Parameters:
prefix (str, optional): Common topic prefix for all routes in the group. Default: ""
middleware (List[Middleware], optional): Middleware to apply to all routes in the group. Default: None
Returns: RouterGroup instance
Example:
dispatch(topic, payload, client)
Find a matching route and dispatch the message through middleware to the handler.
Parameters:
topic (str): The actual MQTT topic that received a message
payload (Any): Message payload (parsed JSON or raw bytes/string)
client: MQTT client instance
Returns: Any (the return value from the handler)
Raises: ValueError if no matching route is found
Example:
get_total_workers_needed()
Calculate the total number of workers needed for all shared routes.
Returns: int - Maximum worker count across all shared routes
Example:
List of all registered Route objects.
Type: List[Route]
Example:
Represents an individual route with its pattern, handler, and configuration.
Note: Routes are typically created internally by Router.on(). Direct instantiation is rarely needed.
The original topic pattern with parameters. Type: str Example: "devices/{device_id}/status"
The async function that handles matching messages. Type: Callable
Quality of Service level for this route. Type: int
List of middleware applied to this route. Type: List[Middleware]
Whether this route uses shared subscriptions. Type: bool
Number of workers for shared subscriptions. Type: int
Compiled regex pattern for topic matching. Type: re.Pattern
MQTT subscription topic with wildcards. Type: str Example: "devices/+/status" (for pattern "devices/{device_id}/status")
Check if a topic matches this route and extract parameters.
Parameters:
topic (str): Actual MQTT topic
Returns: dict[str, str] | None - Extracted parameters or None if no match
Example:
get_subscription_topic(group_name=None)
Get the MQTT subscription topic, with shared prefix if needed.
Parameters:
group_name (str, optional): Group name for shared subscriptions
Returns: str - MQTT subscription topic
Example:
RouterGroup Class
Context manager for grouping routes with shared prefixes and middleware.
Note: RouterGroup instances are created by Router.group(). Direct instantiation is not recommended.
on(topic, handler, qos=0, middleware=None, shared=False, worker_count=1)
Register a route handler within this group.
Parameters: Same as Router.on(), but topic is relative to the group's prefix
Example:
Reference to the parent Router instance. Type: Router
Topic prefix for this group. Type: str
Middleware applied to all routes in this group. Type: List[Middleware]
Topic Pattern Syntax
RouteMQ uses Laravel-style route parameters in topic patterns:
Parameter Syntax
{parameter_name} - Captures a single topic level
Parameters can contain letters, numbers, underscores, and hyphens
Parameters cannot contain forward slashes
MQTT Wildcard Conversion
Route parameters are automatically converted to MQTT wildcards:
Common Exceptions
Raised by dispatch() when no route matches the topic.
Route Order : Routes are matched in registration order. More specific routes should be registered first.
Parameter Validation : Validate parameters in your handlers:
Middleware Usage : Use middleware for cross-cutting concerns:
Shared Subscriptions : Use for high-throughput scenarios:
Complete Example
Last updated 4 months ago