Magic hidden Solace topic subscription wildcards

Aaron
Aaron Member, Administrator, Moderator, Employee Posts: 496 admin

Hi everyone! Hopefully you know all about Solace's different subscription wildcards that consumer/subscriber applications can use to match multiple topics:

  • The * wildcard is a single-level wildcard, and can be used with a prefix: e.g. hello/wo*
  • The > wildcard is a multi-level wildcard, must occur at the very end of a topic subscription on it's own level, and will match 1-or-more topic levels: e.g. hello/>

This is all covered in the docs: https://docs.solace.com/PubSub-Basics/Wildcard-Charaters-Topic-Subs.htm

Now, if you're familiar at all with MQTT, you might think Solace's wildcards are similar to MQTT's + and # wildcards respectively. Well, they are definitely similar. Except:

  • In MQTT, + cannot be used with a prefix. At least, not according to the spec. BUT Solace supports this, and it depends on your MQTT client whether it will block you subscribing to something like hello/wo+
  • In MQTT, # is the multi-level wildcard, but matches 0-or-more levels, which means that hello/# will match: hello, hello/world and hello/world/from/aaron in MQTT. This is not the case with the Solace > wildcard.

So over the years, I've been asked a bunch of times if there is some way in Solace for a single subscription to match hello, hello/world, and hello/world/from/aaron. Usually you have to subscribe to both hello and hello/>. (Barclays SmartSockets apps, I'm looking at you!). Well, it turns out there is a way to do this. When Solace added support for MQTT, they needed to add the new 0-or-more wildcard. And the way to use it in non-MQTT (i.e. in native SMF APIs) is buried in the documentation here: https://docs.solace.com/Open-APIs-Protocols/MQTT/MQTT-Topics.htm#MQTT. Specifically:

Instead of the MQTT "#" wildcard, the subscription must use the ASCII character 0x03.

This section of the docs are talking about subscription manager capabilities (On-Behalf-Of), but can also be used by regular clients. So, for example, I have some JCSMP code:

session.addSubscription(JCSMPFactory.onlyInstance().createTopic("hello/\u0003"));

And this will have the same 0-or-more multi-level capabilities as the MQTT # wildcard.

Hope you like this "hack"..! No need to have two separate Solace subscriptions for 0-and-1-or-more levels. Enjoy!

Tagged:

Comments

  • marc
    marc Member, Administrator, Moderator, Employee Posts: 898 admin

    I learned something new today! 🤯

  • Tamimi
    Tamimi Member, Administrator, Employee Posts: 489 admin

    Woha NICE! I did not know that either, this is a super cool hack to know. I wana try it with other SMF APIs as well 👌

  • manish
    manish Member Posts: 45 ✭✭✭
    edited February 2021 #4

    Hi @Aaron , Great post , I see some Solace Employee don't know it ,Thanks much for posting I have noted for future requirement.

  • manish
    manish Member Posts: 45 ✭✭✭

    Hi @Aaron ,
    Could you please clarify below understanding for below line :
    In MQTT, # is the multi-level wildcard, but matches 0-or-more levels, which means that hello/# will match: hello, hello/world and hello/world/from/aaron in MQTT. This is not the case with the Solace > wildcard.

    Suppose :smile:
    As per above Example from your post: My Subscription: hello/# will derived to below correct subscription in MQTT

    • hello
    • hello/world
    • hello/world/from/aaron in MQTT
      But In case of Solace, If I consider My subscription: hello/>

    • hello (This will not work in Solace as > expect one or more than 1

    • hello/world ( This is correct)
    • hello/world/from/aaron ( This is also correct in Solace)

    Could you please clarify above understanding is correct or not? If not , Please clarify it as I am newbie to Solace

  • Aaron
    Aaron Member, Administrator, Moderator, Employee Posts: 496 admin

    Totally correct. Yeah, the ">" wildcard expects at least one level at the end, so it won't just match "hello". But since MQTT (technically) supports empty levels, that's why the "#" wildcard can match that.

This Month's Leaders