From 70747ff558d68f5053fdab2d84d268bc9ca4c1f7 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Fri, 24 Mar 2017 14:21:59 -0400 Subject: [PATCH] Start firehose doc section for client on port 1883 This commit starts a new section in the firehose docs for client examples on how to print all messages from the firehose to STDOUT. All the other examples in the doc either use ssl or websockets. It's good to have an example of how to just connect to the firehose on vanilla mqtt. Change-Id: Iba9fb10e75eae10efcd59b70b6b83589a077610f --- doc/source/firehose.rst | 54 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/doc/source/firehose.rst b/doc/source/firehose.rst index e94244af69..1a3a656e4d 100644 --- a/doc/source/firehose.rst +++ b/doc/source/firehose.rst @@ -119,12 +119,15 @@ to more specific. MQTT Protocol Example --------------------- -You can also use the paho-mqtt python library to subscribe to MQTT messages -fairly easily. For example this script will subscribe to all topics on the -firehose and print it to STDOUT +Interacting with firehose on the unecrpyted MQTT port is normally pretty easy in +most language bindings. Here are some examples that will have the same behavior +as the CLI example above and will subscribe to all topics on the firehose and +print it to STDOUT. + +Python +'''''' .. code-block:: python - :emphasize-lines: 12,17 import paho.mqtt.client as mqtt @@ -146,6 +149,49 @@ firehose and print it to STDOUT # Listen forever client.loop_forever() +Haskell +''''''' +This requires the `mqtt-hs`_ library to be installed. + +.. _mqtt-hs: https://hackage.haskell.org/package/mqtt-hs + +.. code-block:: haskell + + + {-# Language DataKinds, OverloadedStrings #-} + + module Subscribe where + + import Control.Concurrent + import Control.Concurrent.STM + import Control.Monad (unless, forever) + import System.Exit (exitFailure) + import System.IO (hPutStrLn, stderr) + + import qualified Network.MQTT as MQTT + + topic :: MQTT.Topic + topic = "#" + + handleMsg :: MQTT.Message MQTT.PUBLISH -> IO () + handleMsg msg = do + let t = MQTT.topic $ MQTT.body msg + p = MQTT.payload $ MQTT.body msg + print t + print p + + main :: IO () + main = do + cmds <- MQTT.mkCommands + pubChan <- newTChanIO + let conf = (MQTT.defaultConfig cmds pubChan) + { MQTT.cHost = "firehose.openstack.org" } + _ <- forkIO $ do + qosGranted <- MQTT.subscribe conf [(topic, MQTT.Handshake)] + forever $ atomically (readTChan pubChan) >>= handleMsg + terminated <- MQTT.run conf + print terminated + Websocket Example ----------------- In addition to using the raw MQTT protocol firehose.o.o provides a websocket