Developing a chat application in IOS is difficult. To do, You need to have your own server, which is also difficult to setup. It also requires your own IOS app to handle your HTTP requests and responses which requires asynchronous task. But to be happy we have some third party libraries to simply process. what if we don’t have those libraries and get a powerful chat application. That is the PubNub chat application. There is no difference between one to one chat and group chat. Please find below screens for one to one and group chat in my app. Main thing here is to create a channel for chat.

team chat chat app

What is PubNub:

PubNub is a secure global Data Stream Network(DSN) and easy to use API that enables our customers to build, scale and manage realtime functionality for their applications and IOT devices. Realtime means: When some sends you a message in chat app, and you receive that message on your phone, or you get a push notification, that notification or message is received in ‘realtime.’

Let’s Start:

First we need to integrate PubNub into our IOS App. Here i used CocoaPods. Before that we need to create Xcode project. The pod file is  pod ‘PubNub’, ‘~> 4’.

Subscribing and Receiving Messages:

First we have to initialise the SDK with PubNub’s publish key and subscribe key. Then the next step is to subscribe to a particular channel. Channel is one of the most important part of PubNub. Once subscribed to a particular channel, you can sent and receive messages through a particular channel. One or more devices can connect to the same channel simultaneously at the same time.Once subscribed to a channel, you can have the following callbacks, which you can override depending upon your needs.

i) Import PubNub module.

import UIKit
import PubNub

ii) Add the PNObjectEventListener protocol to the AppDelegate and keys.

class AppDelegateUIResponderUIApplicationDelegatePNObjectEventListener {
   // Stores reference on PubNub client to make sure what it won't be released.
   var client: PubNub!
   func application(_ applicationUIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       // Initialize and configure PubNub client instance      
        let configuration=PNConfiguration(publishKey:"demo", subscribeKey:"demo")       
        self.client = PubNub.clientWithConfiguration(configuration)       
       // Subscribe to demo channel with presence observation  
        self.client.subscribeToChannels(["my_channel"], withPresence: true)  

iii) Handle new message from one of channels on which client has been subscribed.

func client(_clientPubNub, didReceiveMessagemessage: PNMessageResult) { 
     // Handle new message stored in
     // Message has been received on channel group stored in
          // Message has been received on channel stored in      
      print("Receivedmessage:\( on channel:\(" + "at \(")

Sending Messages:

To send messages through a particular channel, PubNub provides you the publish() method. See below.

appDelegate?.client!.publish( Message_Object, toChannel: Channel_Name , withCompletion: {(status: PNPublishStatus) -> Void in

Receiving Channel History:

To get messages history from a particular channel, PubNub provides you historyForChannel() method. See below.

appDelegate?.client.historyForChannel(playerNames as String, withCompletion: { (result, status) in

            if status == nil {

                for message: Any in (result?.data.messages)! {

                    if (message is [String : String]){

                        // This is message






Final Thought:

It is simple to setup a chat application using PubNub. PubNub is a super fast and reliable service for building softwares which require realtime messaging. They have SDK’s for more than 70 programming languages.


About the Author



IOS Application Developer at Colors Software Pvt Ltd.

Gopal Reddy

Author: Gopal Reddy