Skip to content

Chat client life cycle 2.x

Marco Brescianini edited this page Aug 30, 2024 · 5 revisions

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. If you are integrating a 1.x version of the SDK you should take a look a this guide instead.

Table of contents

Life cycle

The life cycle of the BandyerSDK chat client is very simple and inspired by the call client life cycle. It can be in several states, you can inspect in which one the client is at any moment in time querying its state property. It begins its lifecycle in the stopped state, and while in this state chat services won't be available. Once started, the client will eventually transition to the running state. While in that state all chat services will be available to your user and she / he will be able to send and receive messages. When your app goes in background, the chat client is paused automatically, closing any connection to Bandyer platform, and it resumes automatically when your app returns in foreground. As you might expect, while in those states the chat client cannot perform any task, nor it can receive or send any message. You don't have to explicitly pause or resume the chat client when your app goes in background or returns in the active state, we'll take care of it for you.

State changes

In order to be notified about chat client state changes, you should subscribe as a chat client observer conforming to the ChatClientObserver protocol. Here's a snippet of code showing you how to do it:

class MyViewController: UIViewController, ChatClientObserver {

    func subscribeAsChatClientObserver() {
        BandyerSDK.instance().chatClient.add(observer: self, queue: .main)
    }
}
@implementation MyViewController

- (void)subscribeAsChatClientObserver
{
    [BandyerSDK.instance.chatClient addObserver:self queue:dispatch_get_main_queue()];
}

@end

The chat client will keep a weak reference to its observers, so if you keep a strong reference to the client you can safely subscribe as an observer and you won't get a retain cycle. The chat client has two method overloads for adding an object as an observer, the first method is addObserver: (add(observer:)) in swift) the second one is addObserver:queue: (add(observer:queue:)) in swift) (the former is just a convenience method that will call the latter providing a nil queue reference), as stated in our code documentation if you provide a nil queue as the second argument of the addObserver:queue:, your observer will be called synchronously on a background private queue, whereas if you provide a queue as the second argument in the method call, the observer will be called asynchronously on the queue you provided. You must take that into account if you manipulate your UI from one of the observer protocol method.

Start / Stop

Since the initial state of the client is stopped, you must start it by yourself. The chat client provides the start() method that you must call after the BandyerSDK has been initialized and after a user session has been opened. Once the chat client notifies your observer that has started running you can perform any task, like opening a channel view controller. Remember also to call the stop() method on the client when you log out your user or when your app is going to be terminated.

What's next

Clone this wiki locally