MQTT topics and uniqueness issue

I'm experimenting a bit with MQTT and I've run into a bit of weirdness. It could be due to my specific setup but I thought I'd ask away anyway.

I'm using Node.JS and mqttjs as a broker. I'm also using the IBM ia92 testing client written in Java.

When I subscribe to a topic like /system/app and I then publish a message to /system/appp my client receives this message although the topic name is not the same. Note that if I publish to /system/ap I do not receive a message. Of course publishing to /system/app works fine.

Does this mean that MQTT "auto fills in" to something similar or is this a mistake in the client/broker? I'm aware of the + and # wildcards but this "feature/bug" is not known to me.

The broker I'm using can be found here:

https://github.com/adamvr/MQTT.js/blob/master/examples/server/orig.js

It has some weird regular expression on line 23 which I've tried to figure out why it's needed and what it does. Perhaps it could be the cause of a bug if this is not a feature? If anyone has any thoughts on this I'd be very grateful.

I don't know anything about MQTT, but the 'bug' seems to be caused by the fact that the regex are not anchored, so if the subscription topic name can be found within the topic name that is tested against it, the match will succeed (at the test on line 40).

If you add the end of string anchor $ by changing line 23 from

 , reg = new RegExp(topic.replace('+', '[^\/]+').replace('#', '.+$'));   

to

 , reg = new RegExp(topic.replace('+', '[^\/]+').replace('#', '.+') + '$');

it may prevent this behaviour.

I recommend that you open it as an issue at the github repo.

Agree that I'd raise an issue on the mqtt.js tracker.

I also recommend asking on the MQTT Google Group about this kind of protocol specification issue, and also testing the same thing against the rsmb and mosquitto brokers which are more-or-less the reference implementations.

FWIW, IA92 is both really really (really) old and outdated, and also has a questionable license. If you want a Java client for MQTT check out Eclipse Paho which is the current reference implementation.