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.
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.
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.
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.
If you face any difficulties while following this tutorial, you can contact us at email@example.com.
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];
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
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.
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
|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
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!