Client Setup

LAST EDIT Feb 16 2021

Basic setupCopied!

Setup Stream API key for the ChatClient in your AppDelegate.

Once set up, you cannot change the API key of the ChatClient.

The ChatClient 's TokenProvider can be set again later, but remember to call chatClient.currentUserController().reloadUserIfNeeded().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//> import UIKit 
//> import StreamChat 
 
@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 
        /// 1: Create a static token provider. Use it for testing purposes. 
        let token = Token("{{ chat_user_token }}") 
        let tokenProvider = TokenProvider.static(token) 
 
        /// 2: Create a `ChatClientConfig` with the API key. 
        let config = ChatClientConfig(apiKeyString: "{{ api_key }}") 
 
        /// 3: Set the `ChatClient` instance with the config and the token provider. 
        chatClient = ChatClient(config: config, tokenProvider: tokenProvider) 
        return true 
    } 
}

ℹ️ You can find how to setup a user here.

Connecting & DisconnectingCopied!

Connection is handled automatically. However if you disconnect, you'll need to connect again. There's no need to set the token provider again.

1
2
3
4
5
6
7
8
9
import UIKit 
import StreamChat 
 
chatClient.connectionController().connect { error in 
    if let error = error { 
        // handle possible errors 
        print(error) 
    } 
}

Client.shared.disconnect() acts like the logout, it'll clear Client and logout the user. After this call, you should set user again to login & connect:

1
2
3
4
//> import UIKit 
//> import StreamChat 
 
chatClient.connectionController().disconnect()

You do not need to disconnect when app is backgrounded, SDK takes care of background state.

Additional ParametersCopied!

1. Enable logsCopied!

By default logs are disabled.

You can enable it for requests and/or web socket events with different log levels (.error.debug, .info .warning).

1
2
3
import StreamChat 
 
LogConfig.level = .debug

⚠️ Using .info and .debug levels for production builds are not recommended.

ℹ️ You can find more info about logging you can find here.

2. You can specify a custom Stream BaseURLCopied!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//> import StreamChat 
 
/// 1: Create a `ChatClientConfig` with the API key. 
var config = ChatClientConfig(apiKeyString: "{{ api_key }}") 
 
/// 2: Set the config's baseURL 
config.baseURL = .singapore // .dublin, .usEast, .sydney 
 
// or 
 
config.baseURL = .init(url: URL(string: "www.your-custom-url.com")!) 
 
/// 3: Create a `ChatClient` instance with the config and the token provider. 
chatClient = ChatClient(config: config, tokenProvider: tokenProvider)

3. Setup a custom dispatch queue for responses:Copied!

Dispatch queue for callbacks can be set for each controller individually.

1
2
3
import StreamChat 
 
controller.callbackQueue = .global()

4. App in the background behaviorCopied!

The chat client stays connected in the background. It's not possible to change this behavior at the moment.

1
// coming soon