-
Notifications
You must be signed in to change notification settings - Fork 0
Chat client life cycle 1.x
The BandyerSDK chat client is the component responsible for managing the connection with our chat service. It's the unique entry point for enabling/disabling the chat service and it has a life cycle easy to understand and manage.
The life cycle of the BandyerSDK chat client is very simple and inspired by the call client life cycle. It has six state that you can inspect with the help of state property of the chat client.
The initial state of the client is stopped, so it's impossible to take advantage of the chat service. Once started, the client changes its internal state to running, your app can receive incoming messages and send messages using our services. Between those two states there is a transition state, starting: that means that you asked the client to start. You don't have to manage by yourself the background scenario, because in this condition the client will go in the paused state, so it's impossible to receive and sending messages. Once the app will go in foreground, the state will change to resuming and again in running when the client is connected.
Errors could happen to everyone, so there is also a failed state meaning you should try to restart the client or manage the error in other way.
In order to be notified about chat client state changes, you should subscribe as a call client observer conforming to the BCHChatClientObserver protocol. To subscribe as an observer, you chan use this code snippet:
@implementation MyViewController
- (void)subscribeAsChatClientObserver
{
[BandyerSDK.instance.chatClient addObserver:self queue:dispatch_get_main_queue()];
}
@end
class MyViewController: UIViewController {
func subscribeAsChatClientObserver() {
BandyerSDK.instance().chatClient.add(observer: self, queue: .main)
}
}
The chat client will keep a weak reference to its observers, so you don't have to worry about possible retain cycles. Just add the observer to the client and all is already done for you.
You can also subscribe as observer passing nil
to the queue parameter, or equivalently:
@implementation MyViewController
- (void)subscribeAsChatClientObserver
{
[BandyerSDK.instance.chatClient addObserver:self];
}
@end
class MyViewController: UIViewController {
func subscribeAsChatClientObserver() {
BandyerSDK.instance().chatClient.add(observer: self)
}
}
In this case your observer will be called synchronously on a background private queue, whereas if you provide a queue, the observer will be called asynchronously on the queue you provided. Please pay attention to the queue you provide, since if you will not pass any queue, your UI could be blocked.
Since the initial state of the client is stopped, you must start it by yourself.
The chat client provides the start:
method (start(_)
in swift) that you must call after the BandyerSDK has been initialized, otherwise an exception will be thrown. This method has one parameter which represent the "user_alias" of the currently logged user in your app. Once the chat client notifies your observer that has started running you can perform any task, like opening a channel view controller.
Whenever you don't need anymore the chat services, you must stop the client, using the stop
method, for example when you log out your user or your app is going to be terminated.
Looking for other platforms? Take a look at Android, Flutter, ReactNative, Ionic / Cordova. Anything unclear or inaccurate? Please let us know by submitting an Issue or write us here.