| Copyright | Lukas Braun 2014 |
|---|---|
| License | GPL-3 |
| Maintainer | koomi+mqtt@hackerspace-bamberg.de |
| Safe Haskell | None |
| Language | Haskell2010 |
MQTT
Contents
Description
A MQTT client library.
A simple example, assuming a broker is running on localhost (needs -XOverloadedStrings):
>>>import MQTT>>>import MQTT.Logger>>>Just mqtt <- connect defaultConfig { cLogger = warnings stdLogger }>>>let f t payload = putStrLn $ "A message was published to " ++ show t ++ ": " ++ show pyload>>>subscribe mqtt NoConfirm "#" fNoConfirm>>>publish mqtt Handshake False "some random/topic" "Some content!"A message was published to "some random/topic": "Some content!"
- connect :: MQTTConfig -> IO (Maybe MQTT)
- data MQTT
- disconnect :: MQTT -> IO ()
- reconnect :: MQTT -> Int -> IO ()
- onReconnect :: MQTT -> IO () -> IO ()
- resubscribe :: MQTT -> IO [QoS]
- data MQTTConfig
- defaultConfig :: MQTTConfig
- cHost :: MQTTConfig -> HostName
- cPort :: MQTTConfig -> PortNumber
- cClean :: MQTTConfig -> Bool
- cWill :: MQTTConfig -> Maybe Will
- cUsername :: MQTTConfig -> Maybe Text
- cPassword :: MQTTConfig -> Maybe Text
- cKeepAlive :: MQTTConfig -> Maybe Int
- cClientID :: MQTTConfig -> Text
- cConnectTimeout :: MQTTConfig -> Maybe Int
- cReconnPeriod :: MQTTConfig -> Maybe Int
- cLogger :: MQTTConfig -> Logger
- subscribe :: MQTT -> QoS -> Topic -> (Topic -> ByteString -> IO ()) -> IO QoS
- unsubscribe :: MQTT -> Topic -> IO ()
- publish :: MQTT -> QoS -> Bool -> Topic -> ByteString -> IO ()
- send :: MQTT -> Message t -> IO ()
- addHandler :: SingI t => MQTT -> (Message t -> IO ()) -> IO Unique
- removeHandler :: MQTT -> Unique -> IO ()
- awaitMsg :: SingI t => MQTT -> SMsgType t -> Maybe MsgID -> IO (Message t)
- awaitMsg' :: SingI t => MQTT -> Maybe MsgID -> IO (Message t)
- module MQTT.Types
Creating connections
connect :: MQTTConfig -> IO (Maybe MQTT)
Establish a connection. This might fail with an IOException or
return Nothing if the server did not accept the connection.
data MQTT
Abstract type representing a connection to a broker.
disconnect :: MQTT -> IO ()
Close the connection to the server.
reconnect :: MQTT -> Int -> IO ()
Try creating a new connection with the same config (retrying after the
specified amount of seconds has passed) and invoke the callback that is
set with onReconnect once a new connection has been established.
Does not terminate the old connection.
onReconnect :: MQTT -> IO () -> IO ()
Register a callback that will be invoked when a reconnect has happened.
resubscribe :: MQTT -> IO [QoS]
Resubscribe to all topics. Returns the new list of granted QoS.
Connection settings
data MQTTConfig
The various options when establishing a connection.
Defaults for MQTTConfig, connects to a server running on
localhost.
Field accessors
cHost :: MQTTConfig -> HostName
cPort :: MQTTConfig -> PortNumber
cClean :: MQTTConfig -> Bool
cWill :: MQTTConfig -> Maybe Will
cUsername :: MQTTConfig -> Maybe Text
cPassword :: MQTTConfig -> Maybe Text
cKeepAlive :: MQTTConfig -> Maybe Int
cClientID :: MQTTConfig -> Text
cConnectTimeout :: MQTTConfig -> Maybe Int
cReconnPeriod :: MQTTConfig -> Maybe Int
cLogger :: MQTTConfig -> Logger
Subscribing and publishing
subscribe :: MQTT -> QoS -> Topic -> (Topic -> ByteString -> IO ()) -> IO QoS
Subscribe to a Topic with the given QoS and invoke the callback
whenever something is published to the Topic. Returns the QoS that
was granted by the broker (lower or equal to the one requested).
The Topic may contain
wildcars.
The Topic passed to the callback is the fully expanded version where
the message was actually published.
unsubscribe :: MQTT -> Topic -> IO ()
Unsubscribe from the given Topic and remove any handlers.
Sending and receiving Messages
addHandler :: SingI t => MQTT -> (Message t -> IO ()) -> IO Unique
Register a callback that gets invoked whenever a Message of the
expected MsgType is received. Returns the ID of the handler which can be
passed to removeHandler.
removeHandler :: MQTT -> Unique -> IO ()
Remove the handler with the given ID.
Reexports
module MQTT.Types