Code Monkey home page Code Monkey logo

Comments (6)

vera avatar vera commented on June 17, 2024 1

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())

from gmqtt.

Lenka42 avatar Lenka42 commented on June 17, 2024

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.

from gmqtt.

pablogamboa avatar pablogamboa commented on June 17, 2024

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!

from gmqtt.

vera avatar vera commented on June 17, 2024

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?

from gmqtt.

Mixser avatar Mixser commented on June 17, 2024

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)

from gmqtt.

Mixser avatar Mixser commented on June 17, 2024

Hi, @veracl

Thanks for your code snippet it helped us to test and release this feature faster. Please checkout our new version of gmqtt 0.6.7;

from gmqtt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.