Skip to content

Mqtt Topic Wildcard

DodaTech 1 min read

In this tutorial, you'll learn about MQTT Wildcard Subscription Not Matching. We cover key concepts, practical examples, and best practices.

The Problem

MQTT wildcard subscription does not match expected topics.

Quick Fix

Wrong

client.subscribe('sensors/#')  # Matches sensors/temp, sensors/+/temp
Subscribe matches correctly but unexpected topics are also received.
// Single-level wildcard: +
client.subscribe('sensors/+/temp')
// Matches: sensors/room1/temp, sensors/room2/temp
// Does NOT match: sensors/temp, sensors/room1/humidity

// Multi-level wildcard: # (must be last)
client.subscribe('sensors/#')
// Matches: sensors/temp, sensors/room1/temp, sensors/room1/humidity
// Does NOT match: data/sensors/temp

// No wildcard — exact match only
client.subscribe('sensors/room1/temp')
// Matches: sensors/room1/temp only
Subscriptions match only topics within the expected hierarchy.

Prevention

  • matches exactly one level (between slashes). # matches zero or more levels (must be last). Topics are case-sensitive (Sensor/Temp != sensor/temp). $SYS topics are reserved for broker stats. Do not publish to $SYS topics. Use + and # carefully to avoid receiving unintended messages.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### Can # be in the middle of a topic?

No. # must be the last character in the filter. sensors/#/temp is invalid. Use sensors/+/temp instead.

What is the difference between + and #?

  • matches one level. sensors/+/temp matches sensors/a/temp but not sensors/a/b/temp. # matches all remaining levels.

Do wildcards work in publish?

No. Wildcards are only for subscriptions. You must publish to an exact topic.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro