Skip to content

Instantly share code, notes, and snippets.

@fauve-
Created August 12, 2016 18:57
Show Gist options
  • Save fauve-/a62a6d6d47dd29bb180a4a2074d06983 to your computer and use it in GitHub Desktop.
Save fauve-/a62a6d6d47dd29bb180a4a2074d06983 to your computer and use it in GitHub Desktop.
Stomp to MQTT pipe
#python 2.7
import time
import sys
import json
import sys
#pip install stomp.py
import stomp
#clone from repo and install
from clearblade import auth
from clearblade import Client
from clearblade import Messaging
#presuming a userclient
#if we were fancy, we'd do args
SystemKey = "REDACTED"
SystemSecret = "REDACTED"
Email = "REDACTED"
Password = "REDACTED"
ClearBladeUrl = "http://localhost:9000"
MqttRelayTopic = "/clearblade/and/stomp/4/ever"
StompUsername = "admin"
StompPass = "password"
StompPath = "/queue/test"
class StompPipe(stomp.ConnectionListener):
def __init__(self,CBMessagingClient):
self.mqttClient = CBMessagingClient
def on_error(self, headers, message):
print "something should be done about this error that we have received: %s" % message
def on_message(self, headers, message):
out = {"headers":headers,"message":message}
jsonified = json.dumps(out)
self.mqttClient.publishMessage(MqttRelayTopic,jsonified,0)
def ClearBladeSetup():
userClient = Client.UserClient(SystemKey, SystemSecret, Email, Password,ClearBladeUrl)
a = auth.Auth()
a.Authenticate(userClient)
messagingClient = Messaging.Messaging(userClient)
status = messagingClient.InitializeMQTT(keep_alive=30)
if status is not None:
raise Exception('Mqtt connection did not go as planned %d' % status)
return (userClient,messagingClient)
#we probably wouldn't subscribe from the same client in real life
def DemoSubscribe(CBMessagingClient):
def onMessageCallback(cli,obj,msg):
out = json.loads(msg.payload)
print "got " + str(out) + " from stomp"
CBMessagingClient.subscribe(MqttRelayTopic,0,onMessageCallback)
def StompSetup(CBMessagingClient):
conn = stomp.Connection()
conn.set_listener('', StompPipe(CBMessagingClient))
conn.start()
conn.connect(StompUsername, StompPass, wait=True)
conn.subscribe(destination=StompPath, id=1, ack='auto')
(Cbcli,MqttMsgCli) = ClearBladeSetup()
DemoSubscribe(MqttMsgCli)
StompSetup(MqttMsgCli)
#need some more better waiter
#send EOF char to end program
# ctrl+d in linux,osx etc
# I do not know how to send eof in windows
for line in sys.stdin:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment