how to use paho.mqtt library in google app engine to publish as well as subscribe for messages

1,455 views
Skip to first unread message

krishna g

unread,
Apr 1, 2015, 3:15:02 AM4/1/15
to google-a...@googlegroups.com
import paho.mqtt.client as mqtt
print("hello")
mqttc = mqtt.Client("python_pub")
mqttc.connect("test.mosquitto.org", 1883)
mqttc.publish("hello/krishna", "temp =28")
mqttc.loop(2) #timeout = 2s

Vinny P

unread,
Apr 6, 2015, 2:46:11 AM4/6/15
to google-a...@googlegroups.com
What problems are you having? Does this code work in your development environment?

I skimmed the code for Paho ( download link here ) and it looks like it uses threads to listen for messages. Due to the way App Engine works and your module type, you may not be able to use threads (and therefore this library) in your application. It also looks like this library attempts to bind a listener socket, which is not allowed by the Sockets service: https://cloud.google.com/appengine/docs/python/sockets/

The best alternative would be to run this within a Managed VM service, or set it up within a Compute Engine (GCE) VM and build your own way to communicate from your app to the GCE box.
 
 
-----------------
-Vinny P
Technology & Media Consultant
Chicago, IL

App Engine Code Samples: http://www.learntogoogleit.com

 

krishna g

unread,
Apr 15, 2015, 2:45:34 AM4/15/15
to google-a...@googlegroups.com
the problem i am facing is deploying in google app engine, i have seen many forums indicating that add paho library as thrid party library and i have followed the alternatives provided even though in google console log i am getting the same import name error:paho. As you are saying that listener socket is not supported in google app engine environment can you provide me any other alternative other than what you provided.

Vinny P

unread,
Apr 15, 2015, 6:14:56 PM4/15/15
to google-a...@googlegroups.com
On Wed, Apr 15, 2015 at 1:45 AM, krishna g <krishn...@gmail.com> wrote:
As you are saying that listener socket is not supported in google app engine environment can you provide me any other alternative other than what you provided.


 You can see it noted in the documentation as well: go to https://cloud.google.com/appengine/docs/python/sockets/#limitations_and_restrictions and look at the part about listen sockets.

As for alternatives, using a Managed VM or a Compute Engine machine are probably the best alternatives you're going to get. If you're looking for a free hosted tier, it looks like CloudMQTT ( http://www.cloudmqtt.com/ ) has a free trial available on their site.

Ravi K

unread,
Apr 2, 2017, 1:29:12 PM4/2/17
to Google App Engine
How about just running the client in a backend (service)? I have not been able to get data from a public broker


def on_connect(client, userdata, flags, rc):
 logging
.info("Connected with result code "+str(rc))
 client
.subscribe("#")             # wildcard for all topics


def on_message(client, userdata, msg):
 logging
.info(msg.topic+" "+str(msg.payload))


def work(self):
 client
= mqtt.Client()
 client
.on_connect = on_connect
 client
.on_message = on_message
 client
.connect("broker.hivemq.com", 1883, 2)
 client
.subscribe("#")
 client
.loop(10)


in the dev environment it throws no errors but seems to fail silently as on_connect is never called. in production i see the following error:


File "/base/data/home/apps/s~xxxxxx/worker:v0.400234758085693169/worker.py", line 20, inwork client = mqtt.Client() File "/base/data/home/apps/s~sensorswarms/worker:v0.400234758085693169/lib/paho/mqtt/client.py", line 494, in __init__ self._sockpairR,self._sockpairW = _socketpair_compat() File "/base/data/home/apps/s~xxxxxxx/worker:v0.400234758085693169/lib/paho/mqtt/client.py", line 269, in _socketpair_compat listensock.bind(("127.0.0.1", 0)) File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/socket.py", line 222, in meth returngetattr(self._sock,name)(*args) File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/remote_socket/_remote_socket.py", line 676, in bind raise_SystemExceptionFromAppError(e) error: [Errno 22] Invalid argument


anyone get the paho lib to work as a client ?

Jordan (Cloud Platform Support)

unread,
Apr 3, 2017, 10:07:55 AM4/3/17
to Google App Engine
Note, Google Groups is meant for general product discussions and not for technical support. It is recommended you post technical questions on Stack Overflow using the Google Cloud tags.

The Google Cloud service that is recommended as a link to MQTT is Google Cloud Pub/Sub. You can use the gcp-iot-adapter that connects any AMQP-compliant broker to Cloud Pub/Sub which is then easily accessible by App Engine. 

Ward Vanwalleghem

unread,
Feb 15, 2020, 5:04:25 AM2/15/20
to Google App Engine
 gcp-iot-adapter is not an option if you don't own the broker... What does Google Cloud service recommends if we want to subscribe to a public available MQTT broker ?

Jinjun (Cloud Platform Support)

unread,
Feb 19, 2020, 8:25:18 AM2/19/20
to Google App Engine
Google Cloud doesn't recommend any publicly available MQTT broker. Some people are using Mosquitto http://www.bytesofgigabytes.com/googlecloud/installing-mqtt-broker-on-google-cloud/ for your reference.

Ward Vanwalleghem

unread,
Feb 19, 2020, 8:40:53 AM2/19/20
to Google App Engine
sorry, maybe I wasn't clear.  I'm not asking to recommend a public available broker.  I have several exiting MQTT brokers I would like to subscribe to from my app engine.  
As I understand, I can't install gcp-iot-adapter on my app engine.  That won't work.  And as I don't own the MQTT brokers I cannot install it on the machine where the broker runs...
So, how should I connect to those MQTT brokers from my app engine ?
Reply all
Reply to author
Forward
0 new messages