async def main(broker_host, broker_port, token): # create client instance, kwargs (session expiry interval and maximum packet size) # will be send as properties in connect packet sub_client = gmqtt.Client("clientgonnasub", session_expiry_interval=600, maximum_packet_size=65535) assign_callbacks_to_client(sub_client) sub_client.set_auth_credentials(token, None) await sub_client.connect(broker_host, broker_port) # two overlapping subscriptions with different subscription identifiers sub_client.subscribe('TEST/PROPS/#', qos=1, subscription_identifier=1) sub_client.subscribe([ gmqtt.Subscription('TEST/+', qos=1), gmqtt.Subscription('TEST', qos=0) ], subscription_identifier=2) pub_client = gmqtt.Client("clientgonnapub") assign_callbacks_to_client(pub_client) pub_client.set_auth_credentials(token, None) await pub_client.connect(broker_host, broker_port) # this message received by sub_client will have two subscription identifiers pub_client.publish('TEST/PROPS/42', '42 is the answer', qos=1, content_type='utf-8', message_expiry_interval=60, user_property=('time', str(time.time()))) pub_client.publish('TEST', 'Test 42', qos=1, content_type='utf-8', message_expiry_interval=60, user_property=('time', str(time.time()))) # just another way to publish same message msg = gmqtt.Message('TEST/PROPS/42', '42 is the answer', qos=1, content_type='utf-8', message_expiry_interval=60, user_property=('time', str(time.time()))) pub_client.publish(msg) pub_client.publish('TEST/42', {42: 'is the answer'}, qos=1, content_type='json', message_expiry_interval=60, user_property=('time', str(time.time()))) await STOP.wait() await pub_client.disconnect() await sub_client.disconnect(session_expiry_interval=0)
async def test_basic_subscriptions(init_clients): aclient, callback, bclient, callback2 = init_clients await aclient.connect(host=host, port=port) await bclient.connect(host=host, port=port) subscriptions = [ gmqtt.Subscription(TOPICS[1], qos=1), gmqtt.Subscription(TOPICS[2], qos=2), ] bclient.subscribe(subscriptions, user_property=('key', 'value'), subscription_identifier=1) bclient.subscribe(gmqtt.Subscription(TOPICS[3], qos=1), user_property=('key', 'value'), subscription_identifier=2) await asyncio.sleep(1) aclient.publish(TOPICS[3], b"qos 0") aclient.publish(TOPICS[1], b"qos 1", 1) aclient.publish(TOPICS[2], b"qos 2", 2) await asyncio.sleep(1) assert len(callback2.messages) == 3
def subscribe_to_topics(self): subscriptions = [] for topic in self.topics: subscriptions.append(gmqtt.Subscription(topic, qos=1)) self.client.subscribe(subscriptions, subscription_identifier=2) logging.info('Waiting for raised alerts...')
def subscribe_to_topics(self): subscriptions = [] for topic in self.topics: subscriptions.append(gmqtt.Subscription(topic, qos=2)) self.client.subscribe(subscriptions, subscription_identifier=1) logging.info('Waiting message from Gateway')
def subscribe_to_topics(self): subscriptions = [] for topic in self.topics: subscriptions.append(gmqtt.Subscription(topic, qos=0)) self.client.subscribe(subscriptions, subscription_identifier=3) logging.info('Waiting message from Southbound Dispatcher')