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]

 

 

About Author

Author Image
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.

Request for Proposal

Name is required

Comment is required

Sending message..