Message Flow

Understanding how messages flow through RouteMQ from MQTT broker to your application handlers is crucial for building efficient and reliable applications.

Complete Message Flow

┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   MQTT Broker   │───▶│  Route Matcher  │───▶│  Middleware     │───▶│   Controller    │
│                 │    │                 │    │   Pipeline      │    │    Handler      │
│ - Receives Msg  │    │ - Topic Match   │    │ - Auth Check    │    │ - Business      │
│ - Delivers      │    │ - Extract Params│    │ - Rate Limit    │    │   Logic         │
│ - Load Balance  │    │ - Find Route    │    │ - Logging       │    │ - Data Process  │
└─────────────────┘    └─────────────────┘    └─────────────────┘    └─────────────────┘
         │                       │                       │                       │
         │                       │                       │                       ▼
         │                       │                       │              ┌─────────────────┐
         │                       │                       │              │   Response      │
         │                       │                       │              │                 │
         │                       │                       │              │ - Publish Reply │
         │                       │                       │              │ - Update State  │
         │                       │                       │              │ - Log Result    │
         │                       │                       │              └─────────────────┘
         │                       │                       │
         ▼                       ▼                       ▼
┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   Error Case    │    │   No Route      │    │   Middleware    │
│                 │    │                 │    │    Blocked      │
│ - Log Error     │    │ - Log Warning   │    │                 │
│ - Continue      │    │ - Continue      │    │ - Auth Failed   │
│   Operation     │    │   Operation     │    │ - Rate Limited  │
└─────────────────┘    └─────────────────┘    └─────────────────┘

Step-by-Step Flow

1. Message Reception

The MQTT client receives a message from the broker:

2. Route Matching

The router finds a matching route using regex patterns:

Route Matching Example

3. Parameter Extraction

Route parameters are extracted using regex groups:

4. Middleware Pipeline Processing

Messages pass through middleware in order:

Middleware Chain Example

5. Controller Handler Execution

The final handler processes the business logic:

Context Object

The context dictionary carries information through the pipeline:

Error Handling Flow

Route Not Found

Middleware Errors

Handler Errors

Async Processing

Non-Blocking Operations

All processing is async to handle multiple messages concurrently:

Concurrent Message Handling

Shared Subscription Flow

For high-throughput routes with shared subscriptions:

Worker Message Flow

Each worker follows the same message flow:

  1. Receive Message: Worker gets message from shared subscription

  2. Strip Shared Prefix: Remove $share/group/ from topic

  3. Process Normally: Follow standard route matching and middleware

  4. Independent Processing: Each worker processes messages independently

Performance Characteristics

Throughput Optimization

  • Async Processing: Handle multiple messages simultaneously

  • Shared Subscriptions: Distribute load across workers

  • Middleware Caching: Cache expensive operations in middleware

  • Connection Pooling: Reuse database and Redis connections

Latency Optimization

  • Direct Route Matching: O(n) route lookup where n = number of routes

  • Minimal Middleware: Only necessary middleware in chain

  • Async I/O: Non-blocking external operations

  • Memory Efficiency: Reuse context objects

Message Flow Examples

Simple Temperature Reading

API Gateway Flow

Flow Monitoring

Built-in Logging

Custom Monitoring

Add monitoring middleware:

Next Steps

Last updated