DEV Community

loading...
Cover image for Real Time updates in your iOS app using Pub/Sub
Applozic Inc

Real Time updates in your iOS app using Pub/Sub

Anurag Jayaraman
Technical Writer | Former Software Dev | Certified AWS-SAA
Originally published at applozic.com ・4 min read

Introduction

This tutorial will cover how to set up real-time updates for your application using Applozic 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, 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 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

Pub-Sub message flow

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 or iOS Voice and Video SDK 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];
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Setting up Typing Status Indicator

Typing indicators allow you to indicate if users are typing messages in a 1-to-1 or group chat. 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 code given below.

[self.applozicClient sendTypingStatusForUserId:recieverUserId orForGroupId:channelKey withTyping:isTypingStarted];
Enter fullscreen mode Exit fullscreen mode

Call the above method once a user starts typing in textViewDidChange or stops typing in textViewDidEndEditing.

Argument Type Description
recieverUserId String Pass recieverUserId in case of 1-to-1 typing status and pass channelKey as nil
channelKey NSNumber Pass channelKey in case of group/channel typing status and pass recieverUserId as nil
isTypingStarted Bool Pass true or YES in case user started typing
Pass false or NO in case user stopped typing

Continue this Tutorial on Applozic Blog!

We have the detailed tutorial with code snippets available on our blog for you to continue with your integration!

Click here to learn how to:

  • Handle the delegate callbacks
  • Subscribing and Unsubsribing to typing status

Please leave your feedback and doubts in the comments below!

Discussion (0)