MQTT Client for iOS written in Swift.
This framework is implemented as a wrapper of Mosquitto library.
It uses Mosquitto version 1.3.5.
Mosquitto is an open source message broker that implements the MQ Telemetry Transport protocol versions 3.1 and 3.1.1. MQTT provides a lightweight method of carrying out messaging using a publish/subscribe model. Mosquitto is written in C language.
The framework depends on OpenSSL. Before building it, you must checkout the submodule.
$ git submodule update --init
Here is a basic sample.
import Moscapsule
// set MQTT Client Configuration
let mqttConfig = MQTTConfig(clientId: "cid", host: "test.mosquitto.org", port: 1883, keepAlive: 60)
mqttConfig.onPublishCallback = { messageId in
NSLog("published (mid=\(messageId))")
}
mqttConfig.onMessageCallback = { mqttMessage in
NSLog("MQTT Message received: payload=\(mqttMessage.payloadString)")
}
// create new MQTT Connection
let mqttClient = MQTT.newConnection(mqttConfig)
// publish and subscribe
mqttClient.publishString("message", topic: "publish/topic", qos: 2, retain: false)
mqttClient.subscribe("subscribe/topic", qos: 2)
// disconnect
mqttClient.disconnect()
The framework supports TLS_PSK, Server and/or Client certification.
Here is a sample for server certificate.
import Moscapsule
// Note that you must initialize framework only once after launch application
// in case that it uses SSL/TLS functions.
moscapsule_init()
let mqttConfig = MQTTConfig(clientId: "server_cert_test", host: "test.mosquitto.org", port: 8883, keepAlive: 60)
let bundlePath = NSBundle(forClass: self.dynamicType).bundlePath.stringByAppendingPathComponent("cert.bundle")
let certFile = bundlePath.stringByAppendingPathComponent("mosquitto.org.crt")
mqttConfig.mqttServerCert = MQTTServerCert(cafile: certFile, capath: nil)
let mqttClient = MQTT.newConnection(mqttConfig)
The MIT License (MIT)
tonary <jetBeaver@gmail.com>