Your First Route

Learn how to create your first MQTT route in RouteMQ.

Creating a Simple Route

1. Create a Router File

Create a new file app/routers/my_first_router.py:

from core.router import Router
from core.controller import Controller

router = Router()

class MyController(Controller):
    @staticmethod
    async def handle_hello(name, payload, client):
        """Handle hello messages"""
        message = payload.get('message', 'Hello')
        response = f"{message}, {name}!"
        
        print(f"Received: {response}")
        return {"response": response, "status": "success"}

# Define your route
router.on("hello/{name}", MyController.handle_hello, qos=1)

2. Test Your Route

Start the application:

Publish a test message to your route using an MQTT client:

You should see the output: "Received: Hi there, world!"

Route with Parameters

Routes can extract parameters from MQTT topics using {parameter} syntax:

Adding Middleware

Add middleware to process messages before they reach your handler:

Route Groups

Organize related routes using groups:

Next Steps

Last updated