Route Definition

Learn how to define routes in RouteMQ using an intuitive, Laravel-inspired syntax for mapping MQTT topics to handler functions.

Basic Route Syntax

Routes are defined using the Router.on() method, which maps MQTT topic patterns to handler functions:

from core.router import Router
from app.controllers.sensor_controller import SensorController

router = Router()

# Basic route definition
router.on("sensors/temperature", SensorController.handle_temperature)

Route Components

Topic Pattern

The topic pattern defines which MQTT topics will trigger your handler:

# Static topic - matches exactly
router.on("devices/status", handler)

# Parameterized topic - matches dynamic values
router.on("devices/{device_id}/status", handler)

# Multi-level topic - matches nested structures
router.on("sensors/{type}/{location}/data", handler)

Handler Function

The handler function receives the parsed parameters and message data:

Route Options

Quality of Service (QoS)

Control message delivery guarantees:

Shared Subscriptions

Enable horizontal scaling with multiple workers:

Middleware

Add processing logic to routes:

Topic Pattern Matching

Wildcards

RouteMQ automatically converts route parameters to MQTT wildcards:

Pattern Examples

Complete Route Example

Handler Function Signature

Your handler functions should follow this signature:

Best Practices

Topic Naming

  • Use lowercase with underscores: device_status

  • Organize hierarchically: buildings/sensors/temperature

  • Keep parameters descriptive: {device_id} not {id}

Route Organization

  • Group related routes in separate files

  • Use consistent parameter naming across routes

  • Document complex topic patterns

Performance Considerations

  • Use appropriate QoS levels (0 for logs, 1 for commands, 2 for critical)

  • Enable shared subscriptions for high-volume topics

  • Apply rate limiting to prevent abuse

Next Steps

Last updated