Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [paho-dev] Simple subscribing with Python client

That looks really handy, Thanks!
Kind regards,
 
James Sutton
Software Engineer - IoT Foundation - MQTT Open Source Projects
Technical Trojan - Wimbledon Project

Phone: 01962 815438 | Extension: x372454
E-mail: 
Personal Website: www.jsutton.co.uk
Find me on:      
IBM

Hursley Park
HursleySO212JN
United Kingdom
 
IBM United Kingdom Limited Registered in England and Wales with number 741598 Registered office: PO Box 41, North Harbour, Portsmouth, Hants. PO6 3AU
 
 
----- Original message -----
From: Paul Fremantle <paul.fremantle@xxxxxxxxxx>
Sent by: paho-dev-bounces@xxxxxxxxxxx
To: General development discussions for paho project <paho-dev@xxxxxxxxxxx>
Cc:
Subject: Re: [paho-dev] Simple subscribing with Python client
Date: Sat, Jan 23, 2016 12:39 AM
 
Roger
 
I think this is very cool. Thanks
Paul
 
On 22 January 2016 at 22:05, Roger Light <roger@xxxxxxxxxx> wrote:
Hi all,

I thought there might be interest in a new feature I've just been
adding to the Paho Python client (see the develop branch).

You may already be familiar with the paho.mqtt.publish module which
has the single() and multiple() functions, designed to give a very
simple way of publishing messages reliably and without much code. They
connect, publish, then disconnect cleanly.

paho.mqtt.publish.single("sensor/temperature", "36")

I've put together a complementary module called subscribe. This gives
you two helper functions:

simple() connects to a broker, subscribes to a set of topics and once
a user defined number of messages has been received disconnects
cleanly and returns those messages. It can be told to ignore stale
retained messages or not. So for example:

# Retrieve the first "fresh" (without retained bit) message
msg = paho.mqtt.subscribe.simple("sensor/temperature", retained=False)
print(msg.payload)

# Retrieve the first 5 messages
msgs = paho.mqtt.subscribe.simple("sensor/+", msg_count=5)
for m in msgs:
    print("%s %s" % (m.topic, m.payload))

The second function, callback() connects a broker, subscribes to a set
of topics and then passes any incoming messages to a user defined
callback. So a quick program to print out incoming messages would
become

def msg_print(client, userdata, msg):
    print("%s %s" % (msg.topic, msg.payload))

paho.mqtt.client.subscribe.callback(msg_print, "#")

I think that both functions together remove the need for a lot of
quick custom code.

Cheers,

Roger
_______________________________________________
paho-dev mailing list
paho-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/paho-dev
 
 
--
Paul Fremantle
Part-time PhD student - School of Computing
twitter: pzfreo / skype: paulfremantle / blog: http://pzf.fremantle.org
Co-Founder, WSO2
Apache Member and Committer
07740 199 729
_______________________________________________
paho-dev mailing list
paho-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/paho-dev
 
Unless stated otherwise above:
IBM United Kingdom Limited - Registered in England and Wales with number 741598.
Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU


Back to the top