|
| 1 | +--- |
| 2 | +title: HTTP Publish of MQTT messages with Azure Event Grid |
| 3 | +description: Publish MQTT messages via HTTP with Azure Event Grid for scalable server-to-device communication. Learn how to use the HTTP Publish API effectively. |
| 4 | +#customer intent: As a backend developer, I want to publish MQTT messages via HTTP so that I can integrate with Azure Event Grid without maintaining persistent MQTT sessions. |
| 5 | +author: spelluru |
| 6 | +contributors: null |
| 7 | +ms.topic: concept-article |
| 8 | +ms.date: 07/30/2025 |
| 9 | +ms.author: spelluru |
| 10 | +ms.reviewer: spelluru |
| 11 | +ms.custom: |
| 12 | + - ai-gen-docs-bap |
| 13 | + - ai-gen-title |
| 14 | + - ai-seo-date:07/30/2025 |
| 15 | + - ai-gen-description |
| 16 | +--- |
| 17 | + |
| 18 | +# HTTP Publish of MQTT messages with Azure Event Grid (preview) |
| 19 | + |
| 20 | +The Azure Event Grid MQTT Broker HTTP Publish API empowers customers to publish MQTT messages using standard HTTP requests. This complements direct MQTT client connections, providing a simple and scalable option for server-side systems that prefer HTTP for server-to-device command-and-control, updates, or retained message management. |
| 21 | + |
| 22 | +> [!NOTE] |
| 23 | +> This feature is currently in preview. |
| 24 | +
|
| 25 | +Key benefits: |
| 26 | + |
| 27 | +- Allows backend services to send MQTT messages without keeping persistent MQTT sessions open. |
| 28 | +- Helps protect broker stability by limiting per-client MQTT sessions. |
| 29 | +- Ensures consistent message processing for both MQTT and HTTP-originated messages. |
| 30 | + |
| 31 | +## When to Use HTTP Publish |
| 32 | + |
| 33 | +Consider HTTP Publish when: |
| 34 | + |
| 35 | +- Your backend services are HTTP-native and need to send device commands or updates over MQTT. |
| 36 | +- You want to manage retained messages without opening an MQTT connection. |
| 37 | +- You need to scale up publish capacity without exhausting session limits. |
| 38 | + |
| 39 | +## How It Works |
| 40 | + |
| 41 | +1. HTTP clients issue an HTTP POST request with MQTT publish details. |
| 42 | +1. Event Grid maps HTTP request parts to standard MQTT PUBLISH packet properties. |
| 43 | +1. Messages flow through the Event Grid routing and enrichment pipeline, ensuring delivery guarantees and applying any enrichment or transformation. |
| 44 | + |
| 45 | +## Example: MQTT publish equivalent |
| 46 | + |
| 47 | +```http |
| 48 | +PUBLISH Topic Name: devices/CXa-23112/prompt |
| 49 | +QoS: 1 |
| 50 | +RETAIN: 0 |
| 51 | +Response Topic: devices/CXa-23112/reply |
| 52 | +Correlation Data: >U±¶¶»/ |
| 53 | +User Property: Urgency = alert |
| 54 | +User Property: RequestId = 55f4a7ee-b0b4-4d7f-8eb5-2edba2ced5d7 |
| 55 | +Payload: Please accept terms of licensing and agreement |
| 56 | +``` |
| 57 | + |
| 58 | +## Example: HTTP publish request |
| 59 | + |
| 60 | +```http |
| 61 | +POST /mqtt/messages?topic=devices%2FCXa-23112%2Fprompt&api-version=2025-02-15-preview HTTP/1.1 |
| 62 | +Host: nsname.westus3-1.ts.eventgrid.azure.net |
| 63 | +Authorization: Bearer <ENTRA_TOKEN_HERE> |
| 64 | +mqtt-qos: 1 |
| 65 | +mqtt-retain: 0 |
| 66 | +mqtt-response-topic: devices%2FCXa-23112%2Freply |
| 67 | +mqtt-correlation-data: PlXCscK2wrbCuy8= |
| 68 | +mqtt-user-properties: W3siVXJnZW5jeSI6ImFsZXJ0In0seyJSZXF1ZXN0SWQiOiI1NWY0YTdlZS1iMGI0LTRkN2YtOGViNS0yZWRiYTJjZWQ1ZDcifV0= |
| 69 | +Content-Type: text/plain;charset=UTF-8 |
| 70 | +Date: Sun, 06 Nov 1994 08:49:37 GMT |
| 71 | +Content-Length: 46 |
| 72 | +
|
| 73 | +Please accept terms of licensing and agreement |
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +## Request parameters |
| 79 | + |
| 80 | +This table describes how HTTP request parts map to MQTT PUBLISH packet properties. Refer to the original documentation for full details. |
| 81 | + |
| 82 | +| **MQTT PUBLISH Part** | **Type/Values** | **Location** | **Required** | **Description** | |
| 83 | +|--------------------------|------------------------|--------------------------------------------|--------------------|--------------------------------| |
| 84 | +| Topic name | Percent-encoded string | Query `topic` | Yes | MQTT topic to publish to. | |
| 85 | +| QoS | 0 or 1 | Query `qos` or header `mqtt-qos` | No [default = 1] | Quality of Service level. | |
| 86 | +| RETAIN flag | 0 or 1 | Query `retain` or header `mqtt-retain` | No [default = 0] | Whether to retain the message. | |
| 87 | +| Response Topic | Percent-encoded string | Header `mqtt-response-topic` | No | Response topic if needed. | |
| 88 | +| Correlation Data | Base64 string | Header `mqtt-correlation-data` | No | Extra data for tracking. | |
| 89 | +| User Properties | Base64 JSON Array | Header `mqtt-user-properties` | No | Custom user properties. | |
| 90 | +| Content Type | String | Header `content-type` | No | Payload type. | |
| 91 | +| Message Expiry Interval | Unsigned integer | Header `mqtt-message-expiry` | No | Retention period in seconds. | |
| 92 | +| Payload Format Indicator | 0 or 1 | Header `mqtt-payload-format-indicator` | No [default = 0] | Format indicator. | |
| 93 | +| Payload | bytes | HTTP body | No | Message body. | |
| 94 | + |
| 95 | +**Notes:** |
| 96 | + |
| 97 | +- Query parameter values override header values if both are present. |
| 98 | +- Percent-encoding is required for topic and response topic. |
| 99 | +- Correlation Data must be base64-encoded. |
| 100 | + |
| 101 | +## High-level steps for using HTTP Publish |
| 102 | + |
| 103 | +- Step 1: Prepare your Entra ID bearer token for authentication. |
| 104 | +- Step 2: Construct your HTTP POST request to your Event Grid MQTT broker endpoint. |
| 105 | +- Step 3: Include required query parameters like topic. |
| 106 | +- Step 4: Add optional headers for QoS, RETAIN, response topics, user properties, etc. |
| 107 | +- Step 5: Add your payload as the HTTP body. |
| 108 | +- Step 6: Send the request. |
| 109 | +- Step 7: Confirm delivery via logs and metrics in the Event Grid portal. |
| 110 | + |
| 111 | +## Authentication & authorization |
| 112 | + |
| 113 | +- HTTP Publish uses Microsoft Entra ID for authentication. |
| 114 | +- Add a Bearer token to the Authorization header. |
| 115 | +- The Microsoft Entra Object ID becomes the MQTT clientId. |
| 116 | +- The AuthN/AuthZ model aligns with standard MQTT connections. |
| 117 | + |
| 118 | +## Routing and observability |
| 119 | + |
| 120 | +- Metrics and logs include: |
| 121 | + - Protocol: http-publish |
| 122 | + - Request ID |
| 123 | + - Topic |
| 124 | + - Source IP |
| 125 | + - Auth principal |
| 126 | + |
| 127 | +## Best practices |
| 128 | + |
| 129 | +- Use lowercase header keys where possible. HTTP/2 header keys are case-insensitive. |
| 130 | +- Monitor throughput — HTTP messages tend to be larger than direct MQTT messages. |
| 131 | +- HTTP Publish shares throughput limits with direct MQTT publishes. |
| 132 | + |
| 133 | +## Throttling |
| 134 | + |
| 135 | +Be aware that HTTP Publish counts towards your overall MQTT throughput quota. Monitor your usage to avoid exceeding limits. |
| 136 | + |
| 137 | +## Next step |
| 138 | +See [How to publish MQTT messages using HTTP with Azure Event Grid](mqtt-how-to-http-publish.md). |
0 commit comments