Topic Alias support?
Closed this issue · 6 comments
First of all, congrats to the developers on this great project, gmqtt is a joy a to use. Thanks!
I had a quick look and it seems that Topic Alias are not supported by gmqtt. Is that the case? I will happily send a PR improving the docs if they are already supported, just couldn't see how.
Hi @pablogamboa
Thanks, happy to hear this!
Actually there is topic alias support, like any others properties. But you should manage yourself set of topic aliases you use.
So here is quick example:
pub_client.publish('TEST/PROPS/42', '42 is the answer', qos=1, content_type='utf-8',
message_expiry_interval=60, topic_alias=42, user_property=('time', str(time.time())))
pub_client.publish('', '42 is still the answer', qos=1, content_type='utf-8',
message_expiry_interval=60, topic_alias=42, user_property=('time', str(time.time())))
First client publishes messages with topic string and kwarg topic_alias=int
. After this initial message client can publish message with empty string topic and same topic_alias
kwarg.
Sure, you can send a PR with example of how you use topic aliases or any extended support of topic aliases management.
Hi @Lenka42 thanks for the fast reply. Awesome to hear that's supported! At least this question will serve as documentation, but for sure we could add a wee section in the docs around it. It's a great MQTT5 addition and I'm sure other users will wonder about it. Closing the issue, thanks again!
gmqtt seems to not work with brokers that use topic aliases on outgoing PUBLISHs.
I have tried to use gmqtt with the Paho testing broker (https://github.com/eclipse/paho.mqtt.testing) which does this. When the PUBLISH packets with empty topic and topic alias property reach the client, it logs an error message:
[MQTT ERR PROTO] topic name is empty
A PUBLISH with non-empty topic and topic alias property was sent before this.
In my quick check of the code I did not find any handling of incoming topic aliases (setting up a mapping). Do you plan to add support for this?
Hi @veracl, thanks for your notice. You are right, at this moment gmqtt didn't support if SERVER (broker) send an PUBLISH package without topic. You will help us if send a little snippet of your code, which reproducing this error;
We will fix this in the next version (0.6.7)
Here's my code which produced the error message:
subscriber.py
# MQTTv5 client
import asyncio
from gmqtt import Client as MQTTClient
import time
import signal
STOP = asyncio.Event()
def ask_exit(*args):
STOP.set()
# Handle incoming MQTT messages
def handle_mqtt_message(client, topic, payload, qos, properties):
print("Received message '" + str(payload.decode()) + "' on topic '" + topic + "'.")
# Set up MQTTv5 client
async def main():
client = MQTTClient("subscriber", topic_alias_maximum=10)
client.on_message = handle_mqtt_message
await client.connect('127.0.0.1')
client.subscribe('test')
await STOP.wait()
await client.disconnect()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.add_signal_handler(signal.SIGINT, ask_exit)
loop.add_signal_handler(signal.SIGTERM, ask_exit)
loop.run_until_complete(main())
publisher.py:
# MQTTv5 client
import asyncio
from gmqtt import Client as MQTTClient
import time
import signal
client = None
STOP = asyncio.Event()
def ask_exit(*args):
STOP.set()
async def publish():
global client
alias_set = False
for _ in range(0, 2):
await asyncio.sleep(1)
client.publish('' if alias_set else 'test', str(time.time()), topic_alias=1)
if not alias_set:
alias_set = True
# Set up MQTTv5 client
async def main():
global client
client = MQTTClient("publisher", topic_alias_maximum=10)
await client.connect('127.0.0.1')
asyncio.ensure_future(publish())
await STOP.wait()
await client.disconnect()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.add_signal_handler(signal.SIGINT, ask_exit)
loop.add_signal_handler(signal.SIGTERM, ask_exit)
loop.run_until_complete(main())