Coder Social home page Coder Social logo

Comments (6)

algsupport avatar algsupport commented on July 3, 2024 3

Thank you this helped a lot.

from gmqtt.

dexif avatar dexif commented on July 3, 2024 2

As I can see, you subscribed to the "test/#" wildcard topic.
Then you send a message from this client and receive it from the broker by this client. After that, you send another message from another mqtt client, and also receive a message using this client. It works correctly.

If you do not want to receive messages sent by this client, you can use the β€œno_local = true” flag when subscribing to a topic. After that, you will begin to receive only those messages that are sent only by other clients. But it works only with mqtt5 also it depends on broker.

from gmqtt.

algsupport avatar algsupport commented on July 3, 2024 1
import ssl
import asyncio

from gmqtt import Client as MQTTClient
from gmqtt import Subscription


STOP = asyncio.Event()

def on_connect(client, flags, rc, properties):
	print('Connected')
	# This is OK
	client.subscribe('test/a', qos=0, no_local=True)
	# This is also OK
	b=[Subscription("test/b",qos=0, no_local=True)]
	client.subscribe(b)
	#This will not disable local messages
	c=[Subscription("test/c",qos=0)]
	client.subscribe(c, no_local=True)


def on_message(client, topic, payload, qos, properties):
	print('[RECV MSG {}] TOPIC: {} PAYLOAD: {} QOS: {} PROPERTIES: {}'.format(client._client_id, topic, payload, qos, properties))

def on_disconnect(client, packet, exc=None):
	print('Disconnected')

def on_subscribe(client, mid, qos, properties):
	print('SUBSCRIBED')

async def main(broker_host):
	client = MQTTClient("code_client-id")

	client.on_connect = on_connect
	client.on_message = on_message
	client.on_disconnect = on_disconnect
	client.on_subscribe = on_subscribe

	ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
	ssl_context.verify_mode = ssl.CERT_NONE

	client.set_auth_credentials("username", "password")
	await client.connect(broker_host,8883,ssl_context)

	client.publish('test/a', "testA", qos=0)
	client.publish('test/b', "testB", qos=0)
	client.publish('test/c', "testC", qos=0)

	await STOP.wait()
	await client.disconnect()


if __name__ == '__main__':
	loop = asyncio.get_event_loop()
	host = 'mqtt.myhost.com'
	loop.run_until_complete(main(host))

There you go.

from gmqtt.

algsupport avatar algsupport commented on July 3, 2024

One note on this matter.
I noticed that if I create subscriptions with the subscription class, if I don't set the no_local = true at the time of creation and set it when I actually subscribe, the no_local = true is ignored.

from gmqtt.

Lenka42 avatar Lenka42 commented on July 3, 2024

@teucrus could you please provide example of code, how are you doing the subscription?

from gmqtt.

Lenka42 avatar Lenka42 commented on July 3, 2024

@teucrus yeeah, my idea was if you use Subscription object, you should pass all the arguments into this object, and all the arguments from subscribe method is ignored. I guess I should do this 'ignoring' more explicit, or update subscriptions with this arguments, I'll think about it, thanks for noticing πŸ‘

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.