Real-time updates

Build your UI from scratch - Real-time updates

The real-time update is the most important part in building an instant chat messaging application Applozic provides very simple methods to subscribe and unsubscribe for the events to update them on the UI.

The publish-subscribe pattern is used in which the senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers, but instead categorize published messages into classes without knowledge of which subscribers if any, there may be. Similarly, subscribers express interest in one or more classes and only receive messages that are of interest, without knowledge of which publishers, if any, there are.

❗️

Setup ApplozicClient

Note: You need to setup ApplozicClient before using any methods refer to this link

Connecting to publish

Please do not forget to replace <APP_ID> with your App Id.

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

[self.applozicClient subscribeToConversation];
let applozicClient = ApplozicClient(applicationKey: "applozic-sample-app", with: self) as ApplozicClient

applozicClient.subscribeToConversation()

Disconnecting from publishing

Use the below code to disconnect from publish.

[self.applozicClient unsubscribeToConversation];
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 the real-time updates you need to implement the delegate ApplozicUpdatesDelegate
you have to implement the callbacks in Appdeleagte and in your ViewController file

Please do not forget to replace <APP_ID> with your App Id.

Example for implementing real-time update callbacks as shown below

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


@interface AppDelegate () <ApplozicUpdatesDelegate>

{
    ApplozicClient *applozicClient;
}

@end

@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 read by same user login in different devices or platforms.
}

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

}

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

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

}

- (void)onMessageDelivered:(ALMessage *)message {
  // Once message is Delivered this call back will come
  // Check out this doc for checking status : 
​// https://docs.applozic.com/docs/conversation-1#checking-status-of-the-message
}

- (void)onMessageDeliveredAndRead:(ALMessage *)message withUserId:(NSString *)userId {
    // Will be called once the message is read and delivered by receiver user.
 // Check out this doc for checking status : 
​// https://docs.applozic.com/docs/conversation-1#checking-status-of-the-message
}

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

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

}

- (void)onUpdateLastSeenAtStatus:(ALUserDetail *)alUserDetail {
  // Once user is  online or offline this callback will come with user deetails it will have connected flage true means user is online, false means user is offline then you can get the lastSeenAtTime to check when he was last online then show in UI
}

- (void)onUpdateTypingStatus:(NSString *)userId status:(BOOL)status {
    // If some is started typing this callback will come 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 some one 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 deatils updated for users who had a chat with you will get there updated deatils like displayName,imageUrl

}

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

- (void)onMqttConnectionClosed {
    // Once the mqtt is disconnected this  onMqttConnectionClosed will called 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 
}

@end
import UIKit
import Applozic


@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate,ApplozicUpdatesDelegate {

  
  
  
  func onMessageReceived(_ alMessage: ALMessage!) {
     // Once the message is received this call back will trigger you can update on your UI 
    }

    func onMessageSent(_ alMessage: ALMessage!) {
     // Once the message is sent from same user login in different devices  you will get back for message sent 
    }

    func onUserDetailsUpdate(_ userDetail: ALUserDetail!) {
      // Once the user deatils updated for users who had a chat with you will get there updated deatils like displayName,imageUrl
    }

    func onMessageDelivered(_ message: ALMessage!) {
      // Once message is Delivered this call back will come
    }

    func onMessageDeleted(_ messageKey: String!) {
     // Once the message is deleted from same user from different device you will get a call back here 
     // Status of message https://docs.applozic.com/docs/conversation-1#checking-status-of-the-message
    }

    func onMessageDeliveredAndRead(_ message: ALMessage!, withUserId userId: String!) {
        // Once the  message is read and delivered by receiver user.
       // Status of message https://docs.applozic.com/docs/conversation-1#checking-status-of-the-message
    }
    func onConversationDelete(_ userId: String!, withGroupId groupId: NSNumber!)
    {
     // Once conversation is deleted by same user from different device you will get a callback will come  
    }

    func onAllMessagesRead(_ userId: String!) {
       // Once all message read this callback will come   
    }
    
       func conversationRead(byCurrentUser userId: String!, withGroupId groupId: NSNumber!) {
        // Will be called once the conversation read by same user login in different devices or platforms.
    }

    func onUpdateTypingStatus(_ userId: String!, status: Bool) {
     // If some is started typing this callback will come with  Bool value true for typing started and false for typing stopped 
    }

    func onUpdateLastSeen(atStatus alUserDetail: ALUserDetail!) {
     // Once user is  online or offline this callback will come with user deetails it will have connected flage true means user is online, false means user is offline then you can get the lastSeenAtTime to check when he was last online then show in UI  
    }

    func onUserBlockedOrUnBlocked(_ userId: String!, andBlockFlag flag: Bool) {
       // when ever the user is blocked or un blocked this will be called with Bool true or false 
         let service = ALContactService()
        let contact = service.loadContact(byKey: "userId", value: userId)

        // If some one blocks login user
        if let userId = contact?.userId {
            print("User has blocked me \(userId) \(contact?.blockBy != nil ? "true" : "false")")
        }

        // If login user block other user
        if let userId = contact?.userId {
            print("I have blocked the user \(userId) \(contact?.block != nil ? "true" : "false")")
        }
    }
  
     func onMqttConnected() {
       // Once the mqtt is connected this  onMqttConnected will called 
    }
    
    func onMqttConnectionClosed() {
        // Once the mqtt is disconnected this  onMqttConnectionClosed will called you can subscribe to mqtt again using applozicClient.subscribeToConversation() method. 
    }
    
     func onMessageReceived(_ alChannel: ALChannel!) {
     // Once the message is received this call back will trigger you can update on your UI 
    }
    
    func onUserMuteStatus(_ userDetail: ALUserDetail!) {
     //  you can check by userDetail.isNotificationMuted() if the user is mute or unmute

    }
   
    
   }

Sending Typing Status

The user can publish his/her typing status to anyone who has subscribed for the same. You can publish your typing status using the below code

[self.applozicClient sendTypingStatusForUserId:recieverUserId orForGroupId:channelKey withTyping:isTypingStarted];
applozicClient.sendTypingStatus(forUserId:recieverUserId, orForGroupId: channelKey, withTyping: isTypingStarted)

Call the above method once a user starts typing or stopped in textViewDidChange start typing and in textViewDidEndEditing stop the typing

Argument

Type

Description

recieverUserId

String

Pass recieverUserId in case of one to one typing status and pass channelKey as nil

channelKey

NSNumber

Pass channelKey in case of group/channel typing status and pass nil in recieverUserId

isTypingStarted

Bool

Pass true or YES in case in case of typing started.
Pass false or NO in case of typing stopped

Subscribing to typing status

You need to subscribe to a users typing status to get notified for the same

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

[self.applozicClient subscribeToTypingStatusForOneToOne];
applozicClient.subscribeToTypingStatusForOneToOne()

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

[self.applozicClient subscribeToTypingStatusForChannel:channelKey];
applozicClient.subscribeToTypingStatus(forChannel: channelKey)

Argument

Type

Description

channelKey

NSNumber

Pass channelKey you will get channelKey from channel object channel.key

Unsubscribing to typing status

You need to unsubscribe to a users typing status so that you don't receive typing status

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

[self.applozicClient unSubscribeToTypingStatusForOneToOne];
applozicClient.unSubscribeToTypingStatusForOneToOne()

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

[self.applozicClient unSubscribeToTypingStatusForChannel:channelKey];
applozicClient.unSubscribeToTypingStatus(forChannel: channelKey)

Argument

Type

Description

channelKey

NSNumber

Pass channelKey you will get channelKey from channel object channel.key


What’s Next
Did this page help you?