How to use MQTT module in swift for chat
Posted By : Manu Gupta | 30-Sep-2015
In this blog i will be discussing about implementation of chat in swift using MQTT module which is a very lightweighted publish/subscribe module.
We have a MQTT broker with which the client interacts by connecting to the broker
You have to import the Moscapsule framework into you project which is a wrapper for MQTT in made in swift Here is a link to download the framework and steps mentioned to embed into your project
import Moscapsule
var mqttConfig: MQTTConfig!
var mqttClient: MQTTClient!
Import Moscapsule and then create the instance of type MQTTConfig and MQTTClient
mqttConfig = MQTTConfig(clientId: "Unique Client ID", host: "HOST url on which broker is setup", port: 1883, keepAlive: 60)
mqttClient = MQTT.newConnection(mqttConfig)
Initializing the mqttConfig instance with your broker parameters and then connecting it with the broker
mqttClient.subscribe("foo", qos: 2)
mqttConfig.onMessageCallback = { mqttMessage in
NSLog("MQTT Message received: payload=\(mqttMessage.payloadString)")
NSLog("MQTT Message received: mqttMessage=\(mqttMessage.messageId)")
}
After That you have to subscribe to a channel/Topic from which you will recieve the messages. You will also get a callback method from which you can come to about that your message has been recieved by the broker
mqttClient.publishString("", topic: "bar", qos: 2, retain: true)
mqttConfig.onPublishCallback = { messageId in
NSLog("published (mid=\(messageId))")
}
Then create a publish topic on which a client can publish its messages. Publish also has a callback method.
mqttClient.unsubscribe("foo", requestCompletion: nil)
mqttClient.disconnect()
This is how you unsubscibe from the topic/channel and disconnect from the broker after this user will not send you any ping request to check wheather you are online or not
var messageString: NSString = "I:Have:Left:The:Chat"
var messageInData: NSData = messageString.dataUsingEncoding(NSUTF8StringEncoding)!
var tempMqttWillOps:MQTTWillOpts = MQTTWillOpts(topic: "bar", payload: messageInData, qos: 2, retain: true)
mqttConfig.mqttWillOpts = tempMqttWillOps
Last Will Message is used if the user disconnects abruptly from the broker without informing the broker then the broker will publish this message on the publish channel of the client so that the other end users also know about unavailability of the client
One more thing which is most important if we want retain the message which were sent to the channel then we have to set clearSessaion to false
Thanks and for any further queries feel free to contact me @[email protected]
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Manu Gupta
Manu is a Native iOS (Swift) and Titanium lead developer . Manu has good experience working with Swift and Alloy framework as well. He loves watching movies and travelling.