Real Time updates in your iOS app using Pub/Sub
Tags: tutorial, ios, applozic, pubsub
Published: 2021-05-26 07:31:49

##Introduction

This tutorial will cover how to set up real-time updates for your application using [Applozic](https://www.applozic.com/) SDK integrations.

You will learn how to:
* Subscribe/unsubscribe to topics and handle callbacks.
* Setup typing status indicators in your 1-to-1 and group chats.

In [Applozic SDKs](https://github.com/AppLozic), real-time updates are handled by the pub-sub messaging pattern. Let's take a look at some of the basic concepts of what pub-sub means in a messaging paradigm.

##The Publish-Subscribe messaging pattern

In a publish-subscribe (often called pub-sub for brevity) architecture, a central source of information called a **broker** receives and distributes all data in the network. Pub-sub clients can publish data to the broker or subscribe to get data from it—or both.

The important concept to remember in pub-sub is that a communication channel [topic](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) is set up between a server and several clients. This channel is represented by a persistent network communication (WebSocket, MQTT, etc.). The intermediary which handles data transfer based on subscriptions is called the broker.

The broker does not store data; it simply moves it from publishers to subscribers. When data comes in from a publisher, the broker promptly sends it off to any client subscribed to that data.

![image](https://d1.awsstatic.com/product-marketing/Messaging/sns_img_topic.e024462ec88e79ed63d690a2eed6e050e33fb36f.png)
<figcaption>Pub-Sub message flow</figcaption>

This is different from the standard request/response (pull) models in which publishers check if new data has become available. This makes the pub/sub method the most suitable framework for streaming data in real-time. It also means that dynamic networks can be built at scale using pub-sub architecture.

##Setting up real-time updates in your application

As a starting point for this tutorial, we are going to consider that you already have a working application and have implemented one of the Applozic Chat SDKs.

You can follow these tutorial articles in case you want to set up your application with the [iOS Chat SDK](https://www.applozic.com/blog/build-chat-app-using-applozic-swift-sdk/) or [iOS Voice and Video SDK](https://www.applozic.com/blog/how-to-build-an-ios-app-for-real-time-communication-with-voice-and-video/) offered by Applozic.

If you face any difficulties while following this tutorial, you can contact us at support@applozic.com.

###Connecting to publish

In order for a subscriber to receive messages on a topic, it needs to be subscribed and also needs a custom security policy to allow the topic to deliver messages to the queue. The following code handles both of these for you, without you ever having to deal with the details around building that custom policy.

```
Note: Please replace <APP_ID> with your App Id

ApplozicClient *applozicClient = [[ApplozicClient alloc]initWithApplicationKey:@"<APP_ID>" withDelegate:self];

[self.applozicClient subscribeToConversation];
```

###Disconnecting from publish

If you want a subscriber to stop receiving messages on a topic, you can disconnect using the following code.
```
[self.applozicClient unsubscribeToConversation];
Note: subscribeToConversation and unsubscribeToConversation need to be called in Appdelegate 
and in your VIewController file also where you want callbacks in 
applicationWillEnterForeground or applicationDidEnterBackground
```

###Setting up Typing Status Indicator 

Typing indicators allow you to indicate if users are typing messages in a 1-to-1 or group chat.&nbsp;You can trigger a typing event when a user starts typing a message and this will let the user publish their typing status to anyone who has subscribed for the same.

###Sending typing status

You can publish your typing status using the&nbsp;code given below.

```
[self.applozicClient sendTypingStatusForUserId:recieverUserId orForGroupId:channelKey withTyping:isTypingStarted];
```

Call the above method once a user starts typing in `textViewDidChange` or stops typing in `textViewDidEndEditing`.
 
<!-- wp:table {"className":"is-style-regular"} -->
<figure class="wp-block-table is-style-regular"><table><tbody><tr><td class="has-text-align-center" data-align="center">Argument</td><td class="has-text-align-center" data-align="center">Type</td><td class="has-text-align-center" data-align="center">Description</td></tr><tr><td class="has-text-align-center" data-align="center">recieverUserId</td><td class="has-text-align-center" data-align="center">String</td><td class="has-text-align-center" data-align="center">Pass recieverUserId in case of 1-to-1 typing status and pass channelKey as nil</td></tr><tr><td class="has-text-align-center" data-align="center">channelKey</td><td class="has-text-align-center" data-align="center">  NSNumber    </td><td class="has-text-align-center" data-align="center">Pass channelKey in case of group/channel typing status and pass recieverUserId as nil</td></tr><tr><td class="has-text-align-center" data-align="center">  isTypingStarted    </td><td class="has-text-align-center" data-align="center">Bool</td><td class="has-text-align-center" data-align="center">Pass true or YES in case user started typing<br>Pass false or NO in case user stopped typing</td></tr></tbody></table></figure>
<!-- /wp:table -->

##Continue this Tutorial on Applozic Blog!
We have the detailed tutorial with code snippets available on [our blog](https://www.applozic.com/blog/) for you to continue with your integration!

[Click here to learn](https://www.applozic.com/blog/real-time-updates-in-your-chat-app-using-pub-sub/) how to:
* Handle the delegate callbacks
* Subscribing and Unsubsribing to typing status


Please leave your feedback and doubts in the comments below!