Real Time Chat

Real-Time updates in your chat app using Pub-Sub

Set up real-time updates for your application utilizing Applozic SDK integrations
Google+ Pinterest LinkedIn Tumblr


This tutorial will cover how to set up real-time updates for your application using Applozic SDK integrations and leverage the power of the pub-sub messaging pattern.
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.

pub-sub model message flow
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

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

Handling the delegate callbacks

For handling real-time updates you need to:

  • Implement the delegate ApplozicUpdatesDelegate.
  • Implement the callbacks in Appdelegate and in your ViewController file
Note: Please replace <APP_ID> with your App Id

#import "AppDelegate.h"
#import <Applozic/Applozic.h>

@interface AppDelegate () <ApplozicUpdatesDelegate>

    ApplozicClient *applozicClient;


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  // Setup ApplozicClient with self Delegate
    applozicClient = [[ApplozicClient alloc] initWithApplicationKey:@"<APP_ID>" withDelegate:self];
    return YES;

//Real time updates delegate callbacks 

- (void)conversationReadByCurrentUser:(NSString *)userId withGroupId:(NSNumber *)groupId {
//will be called once the conversation is read by the same user login in different devices or platforms.

- (void)onConversationDelete:(NSString *)userId withGroupId:(NSNumber *)groupId {
    // Once conversation is deleted by same user from different devices you will get a callback


- (void)onAllMessagesRead:(NSString *)userId {
    // Once all message read you will get this callback

- (void)onMessageDeleted:(NSString *)messageKey {
   // Once the message is deleted from same user from different device you will get a callback here


- (void)onMessageDelivered:(ALMessage *)message {
  // Once message is Delivered this callback will be received 
  // Check out this doc for checking status : 

- (void)onMessageDeliveredAndRead:(ALMessage *)message withUserId:(NSString *)userId {
    // Will be called once the message is read and delivered by the receiver user.
 // Check out this doc for checking status : 

- (void)onMessageReceived:(ALMessage *)alMessage {
    // Once the message is received this callback will trigger and you can update on the UI

- (void)onMessageSent:(ALMessage *)alMessage {
    // Once the message is sent from same user login in different devices you will get callback for message sent


- (void)onUpdateLastSeenAtStatus:(ALUserDetail *)alUserDetail {
  // Once a user is online or offline this callback will be received with user details. In the connected flag, true means user is online, false means user is offline and using this you can get the lastSeenAtTime to check when user was last online then show the same in UI

- (void)onUpdateTypingStatus:(NSString *)userId status:(BOOL)status {
    // If someone has started typing this callback will be received with Bool value true for typing started and false for typing stopped


- (void)onUserBlockedOrUnBlocked:(NSString *)userId andBlockFlag:(BOOL)flag {
   // Once user is blocked or unblocked this callback will be called
    ALContactService *service = [[ALContactService alloc] init];
    ALContact * contact = [service loadContactByKey:@"userId" value:userId];

    // If someone blocks login user
    NSLog(@"User has blocked me %@ %@", contact.userId, contact.blockBy ? @"YES": @"NO");

    // If login user block other user
    NSLog(@"I have blocked the user %@ %@", contact.userId, contact.block ? @"YES": @"NO");

- (void)onUserDetailsUpdate:(ALUserDetail *)userDetail {
    //Once the user details are updated for users who had a chat with you will get their updated details like displayName,imageUrl


- (void)onMqttConnected {
   // Once the mqtt is connected this method onMqttConnected will called 

- (void)onMqttConnectionClosed {
    // Once the mqtt is disconnected this method onMqttConnectionClosed will be called and you can subscribe to mqtt again using applozicClient.subscribeToConversation() method.

- (void)onChannelUpdated:(ALChannel *)channel {
    //On channel update this callback will trigger 

- (void)onUserMuteStatus:(ALUserDetail *)userDetail {
 // userDetail.isNotificationMuted You can check if the user is mute or unmute 


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];

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

recieverUserIdStringPass 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 BoolPass true or YES in case user started typing
Pass false or NO in case user stopped typing

Subscribing to typing status

Use the below code to subscribe for the typing status for a user

[self.applozicClient subscribeToTypingStatusForOneToOne];

Use the below code to subscribe for the typing status for a channel/group

[self.applozicClient subscribeToTypingStatusForChannel:channelKey];

Unsubscribing to typing status

Use the below code to unsubscribe for the typing status for a user

[self.applozicClient unSubscribeToTypingStatusForOneToOne];

Use the below code to unsubscribe for the typing status for a channel/group

[self.applozicClient unSubscribeToTypingStatusForChannel:channelKey];


We began this article by discussing how to enable real-time updates in your application by leveraging Applozic SDKs.

A few key features that we wanted to implement were how to subscribe/unsubscribe from topics and handle callbacks and also how to set up typing status indicators in your 1-to-1 and group chats.

As we end this tutorial, you should be all set to send and receive real-time updates within your application.

Please leave your feedback and doubts in the comments below!


Anurag is a Technical Writer and technology enthusiast who enjoys science fiction and PC gaming.