How to build message delivery status in iOS using Pusher

build-message-delivery-status-in-ios-using-pusher-header.png

In this tutorial, explore how to create an iOS chat application with a message delivery status message after the message is sent to other users.

Introduction

In this tutorial, we will learn how to add message delivery status feature to an iOS mobile application using Swift and Pusher.

When building mobile chat applications, it is not uncommon to see developers adding a delivery status feature that lets you know when the message you sent has been delivered to the recipient. Instant Messaging applications like WhatsApp, Messenger, BBM, Skype and the like all provide this feature.

Some of the tools that we will need to build our application are:

  • Xcode– The application will be built using Apple’s Swift programming language.
  • NodeJS (Express) – The backend application will be written in NodeJS.
  • Pusher – Pusher will provide realtime reporting when the sent messages are delivered. You will need a Pusher application ID, key and secret. Create your free account at pusher.com, then grab your app ID, key and secret from the Pusher dashboard.

Below is a screen recording of what we’ll be building. As you can see, when a message is sent, it is marked as sent, and the moment it hits the recipient’s phone, it is marked as delivered.

Getting started with our iOS application

Launch Xcode and create a new project. We are calling ours Anonchat. Once it has loaded the workspace, close Xcode and then cd to the root of your project and run the command pod init. This should generate a Podfile for you. Change the contents of the Podfile:

1# Uncomment the next line to define a global platform for your project
2    platform :ios, '9.0'
3
4    target 'anonchat' do
5      # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
6      use_frameworks!
7
8      # Pods for anonchat
9      pod 'Alamofire'
10      pod 'PusherSwift'
11      pod 'JSQMessagesViewController'
12    end

Now run the command pod install so the Cocoapods package manager can pull in the necessary dependencies. When this is complete, close Xcode (if open) and then open the .xcworkspace file that is in the root of your project folder.

Creating the views for our iOS application

We are going to be creating a couple of views that we will need for the chat application to function properly. The views will look something like the screenshot below:

What we have done above is create the first ViewController which will serve as our welcome ViewController, and we have added a button which triggers navigation to the next controller which is a Navigation Controller. This Navigation Controller in turn has a View Controller set as the root controller.

Coding the message delivery status for our iOS application

Now that we have set up the views using the interface builder on the MainStoryboard, let’s add some functionality. The first thing we will do is create a WelcomeViewController and associate it with the first view on the left. This will be the logic house for that view; we won’t add much to it for now though:

1import UIKit
2
3    class WelcomeViewController: UIViewController {
4        override func viewDidLoad() {
5            super.viewDidLoad()
6        }
7    }

Next, we create another controller called the ChatViewController, which will be the main power house and where everything will be happening. The controller will extend the JSQMessagesViewController so that we automatically get a nice chat interface to work with out of the box, then we have to work on customizing this chat interface to work for us.

1import UIKit
2    import Alamofire
3    import PusherSwift
4    import JSQMessagesViewController
5
6    class ChatViewController: JSQMessagesViewController {
7        override func viewDidLoad() {
8            super.viewDidLoad()
9
10            let n = Int(arc4random_uniform(1000))
11
12            senderId = "anonymous" + String(n)
13            senderDisplayName = senderId
14        }
15    }

If you notice on the viewDidLoad method, we are generating a random username and setting that to be the senderId and senderDisplayName on the controller. This extends the properties set in the parent controller and is required.

Before we continue working on the chat controller, we want to create a last class called the AnonMessage class. This will extend the JSQMessage class and we will be using this to extend the default functionality of the class.

1import UIKit
2    import JSQMessagesViewController
3
4    enum AnonMessageStatus {
5        case sending
6        case sent
7        case delivered
8    }
9
10    class AnonMessage: JSQMessage {
11        var status : AnonMessageStatus
12        var id : Int
13
14        public init!(senderId: String, status: AnonMessageStatus, displayName: String, text: String, id: Int?) {
15            self.status = status
16
17            if (id != nil) {
18                self.id = id!
19            } else {
20                self.id = 0
21            }
22
23
24            super.init(senderId: senderId, senderDisplayName: displayName, date: Date.init(), text: text)
25        }
26
27        public required init?(coder aDecoder: NSCoder) {
28            fatalError("init(coder:) has not been implemented")
29        }
30    }

In the class above we have extended the JSQMessage class and we have also added some new properties to track: the id and the status. We also added an initialisation method so we can specify the new properties before instantiating the JSQMessage class properly. We also added an enum that contains all the statuses the message could possibly have.

Returning to the ChatViewController, let’s add a few properties to the class that we will need:

1static let API_ENDPOINT = "http://localhost:4000";
2
3    var messages = [AnonMessage]()
4    var pusher: Pusher!
5
6    var incomingBubble: JSQMessagesBubbleImage!
7    var outgoingBubble: JSQMessagesBubbleImage!

Now that’s done, let’s start customizing the controller to suit our needs. First, we will add some logic to the viewDidLoad method:

1override func viewDidLoad() {
2        super.viewDidLoad()
3
4        let n = Int(arc4random_uniform(1000))
5
6        senderId = "anonymous" + String(n)
7        senderDisplayName = senderId
8
9        inputToolbar.contentView.leftBarButtonItem = nil
10
11        incomingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor.jsq_messageBubbleBlue())
12        outgoingBubble = JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImage(with: UIColor.jsq_messageBubbleGreen())
13
14        collectionView!.collectionViewLayout.incomingAvatarViewSize = CGSize.zero
15        collectionView!.collectionViewLayout.outgoingAvatarViewSize = CGSize.zero
16
17        automaticallyScrollsToMostRecentMessage = true
18
19        collectionView?.reloadData()
20        collectionView?.layoutIfNeeded()
21    }

In the above code, we started customizing the way our chat interface will look, using the parent class that has these properties already set. For instance, we are setting the incomingBubble to blue, and the outgoingBubble to green. We have also eliminated the avatar display because we do not need it right now.

The next thing we are going to do is override some of the methods that come with the parent controller so that we can display messages, customize the feel and more:

1override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAt indexPath: IndexPath!) -> JSQMessageData! {
2        return messages[indexPath.item]
3    }
4
5    override func collectionView(_ collectionView: JSQMessagesCollectionView!, attributedTextForCellBottomLabelAt indexPath: IndexPath!) -> NSAttributedString! {
6        if !isAnOutgoingMessage(indexPath) {
7            return nil
8        }
9
10        let message = messages[indexPath.row]
11
12        switch (message.status) {
13        case .sending:
14            return NSAttributedString(string: "Sending...")
15        case .sent:
16            return NSAttributedString(string: "Sent")
17        case .delivered:
18            return NSAttributedString(string: "Delivered")
19        }
20    }
21
22    override func collectionView(_ collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForCellBottomLabelAt indexPath: IndexPath!) -> CGFloat {
23        return CGFloat(15.0)
24    }
25
26    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
27        return messages.count
28    }
29
30    override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAt indexPath: IndexPath!) -> JSQMessageBubbleImageDataSource! {
31        let message = messages[indexPath.item]
32        if message.senderId == senderId {
33            return outgoingBubble
34        } else {
35            return incomingBubble
36        }
37    }
38
39    override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {
40        return nil
41    }
42
43    override func didPressSend(_ button: UIButton, withMessageText text: String, senderId: String, senderDisplayName: String, date: Date) {
44        let message = addMessage(senderId: senderId, name: senderId, text: text, id: nil)
45
46        if (message != nil) {
47            postMessage(message: message as! AnonMessage)
48        }
49
50        finishSendingMessage(animated: true)
51    }
52
53    private func isAnOutgoingMessage(_ indexPath: IndexPath!) -> Bool {
54        return messages[indexPath.row].senderId == senderId
55    }

The next thing we are going to do is create some new methods on the controller that will help us post a new message. After that, we create a method to hit the remote endpoint which sends the message. Finally, we create a method to append the new message sent (or received) to the messages array:

1private func postMessage(message: AnonMessage) {
2        let params: Parameters = ["sender": message.senderId, "text": message.text]
3        hitEndpoint(url: ChatViewController.API_ENDPOINT + "/messages", parameters: params, message: message)
4    }
5
6    private func hitEndpoint(url: String, parameters: Parameters, message: AnonMessage? = nil) {
7        Alamofire.request(url, method: .post, parameters: parameters).validate().responseJSON { response in
8            switch response.result {
9            case .success(let JSON):
10                let response = JSON as! NSDictionary
11
12                if message != nil {
13                    message?.id = (response.object(forKey: "ID") as! Int) as Int
14                    message?.status = .sent
15                    self.collectionView.reloadData()
16                }
17
18            case .failure(let error):
19                print(error)
20            }
21        }
22    }
23
24    private func addMessage(senderId: String, name: String, text: String, id: Int?) -> Any? {
25        let status = AnonMessageStatus.sending
26
27        let id = id == nil ? nil : id;
28
29        let message = AnonMessage(senderId: senderId, status: status, displayName: name, text: text, id: id)
30
31        if (message != nil) {
32            messages.append(message as AnonMessage!)
33        }
34
35        return message
36    }

Great. Now every time we send a new message, the didPressSend method will be triggered and all the other ones will fall into place nicely!

For the last piece of the puzzle, we want to create the method that listens for Pusher events and fires a callback when an event trigger is received:

1private func listenForNewMessages() {
2        let options = PusherClientOptions(
3            host: .cluster("PUSHER_CLUSTER")
4        )
5
6        pusher = Pusher(key: "PUSHER_KEY", options: options)
7
8        let channel = pusher.subscribe("chatroom")
9
10        channel.bind(eventName: "new_message", callback: { (data: Any?) -> Void in
11            if let data = data as? [String: AnyObject] {
12                let messageId = data["ID"] as! Int
13                let author = data["sender"] as! String
14
15                if author != self.senderId {
16                    let text = data["text"] as! String
17
18                    let message = self.addMessage(senderId: author, name: author, text: text, id: messageId) as! AnonMessage?
19                    message?.status = .delivered
20
21                    let params: Parameters = ["ID":messageId]
22                    self.hitEndpoint(url: ChatViewController.API_ENDPOINT + "/delivered", parameters: params, message: nil)
23
24                    self.finishReceivingMessage(animated: true)
25                }
26            }
27        })
28
29        channel.bind(eventName: "message_delivered", callback: { (data: Any?) -> Void in
30            if let data = data as? [String: AnyObject] {
31                let messageId = (data["ID"] as! NSString).integerValue
32                let msg = self.messages.first(where: { $0.id == messageId })
33
34                msg?.status = AnonMessageStatus.delivered
35                self.finishReceivingMessage(animated: true)
36            }
37        })
38
39        pusher.connect()
40    }

In this method, we have created a Pusher instance, we have set the cluster and the key. We attach the instance to a chatroom channel and then bind to the new_message event on the channel. We also bind a message_delivered event, this will be the event that is triggered when a message is marked as delivered. It will update the message status to delivered so the sender knows the message has indeed been delivered.

? Remember to replace the key and cluster with the actual values you have gotten from your Pusher dashboard.

Now we should be done with the application and as it stands, it should work but no messages can be sent just yet as we need a backend application for it to work properly.

Building the backend Node application

Now that we are done with the iOS and Xcode parts, we can create the NodeJS backend for the application. We are going to use Express so that we can quickly whip something up.

Create a directory for the web application and then create two new files:

The index.js file…

1// ------------------------------------------------------
2    // Import all required packages and files
3    // ------------------------------------------------------
4
5    let Pusher     = require('pusher');
6    let express    = require('express');
7    let bodyParser = require('body-parser');
8    let Promise    = require('bluebird');
9    let db         = require('sqlite');
10    let app        = express();
11    let pusher     = new Pusher(require('./config.js')['config']);
12
13    // ------------------------------------------------------
14    // Set up Express
15    // ------------------------------------------------------
16
17    app.use(bodyParser.json());
18    app.use(bodyParser.urlencoded({ extended: false }));
19
20    // ------------------------------------------------------
21    // Define routes and logic
22    // ------------------------------------------------------
23
24    app.post('/delivered', (req, res, next) => {
25      let payload = {ID: ""+req.body.ID+""}
26      pusher.trigger('chatroom', 'message_delivered', payload)
27      res.json({success: 200})
28    })
29
30    app.post('/messages', (req, res, next) => {
31      try {
32        let payload = {
33          text: req.body.text,
34          sender: req.body.sender
35        };
36
37        db.run("INSERT INTO Messages (Sender, Message) VALUES (?,?)", payload.sender, payload.text)
38          .then(query => {
39            payload.ID = query.stmt.lastID
40            pusher.trigger('chatroom', 'new_message', payload);
41
42            payload.success = 200;
43
44            res.json(payload);
45          });
46
47      } catch (err) {
48        next(err)
49      }
50    });
51
52    app.get('/', (req, res) => {
53      res.json("It works!");
54    });
55
56
57    // ------------------------------------------------------
58    // Catch errors
59    // ------------------------------------------------------
60
61    app.use((req, res, next) => {
62        let err = new Error('Not Found');
63        err.status = 404;
64        next(err);
65    });
66
67
68    // ------------------------------------------------------
69    // Start application
70    // ------------------------------------------------------
71
72    Promise.resolve()
73      .then(() => db.open('./database.sqlite', { Promise }))
74      .then(() => db.migrate({ force: 'last' }))
75      .catch(err => console.error(err.stack))
76      .finally(() => app.listen(4000, function(){
77        console.log('App listening on port 4000!')
78      }));

Here we define the entire logic of our backend application. We are also using SQLite to store the chat messages; this is useful to help identify messages. Of course, you can always change the way the application works to suite your needs.

The index.js file also has two routes where it receives messages from the iOS application and triggers the Pusher event which is picked up by the application.

The next file is the packages.json where we define the NPM dependencies:

1{
2      "main": "index.js",
3      "dependencies": {
4        "bluebird": "^3.5.0",
5        "body-parser": "^1.16.0",
6        "express": "^4.14.1",
7        "pusher": "^1.5.1",
8        "sqlite": "^2.8.0"
9      }
10    }

You’ll also need a config.js file in the root directory:

1module.exports = {
2        appId: '',
3        key: '',
4        secret: '',
5        cluster: '',
6    };

Substitute with the actual values from your Pusher application. Now run npm install on the directory and then node index.js once the npm installation is complete. You should see an App listening on port 4000! message.

Screen showing the node application is currently running.

Testing the application

Once you have your local node web server running, you will need to make some changes so your application can talk to the local web server. In the info.plist file, make the following changes:

Changes on info.plist file to allow iOS connect to localhost

With this change, you can build and run your application and it will talk directly with your local web application.

Conclusion

In this article, we have explored how to create an iOS chat application with a message delivery status message after the message is sent to other users. For practice, you can expand the statuses to support more instances.

Have a question or feedback on the article? Please ask below in the comment section. The repository for the application and the Node backend is available here.