Client Setup

Last Edit: May 05 2020

Basic setup

Setup Stream API key for the Client in your AppDelegate


import UIKit
import StreamChatCore

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        Client.configureShared(.init(apiKey: "<#STREAM_API_KEY#>"))
        return true
    }
}
                    

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

Connecting & Disconnecting

Client.shared.set(user:token:) acts like login, it'll connect immediately after this call:


Client.shared.set(user: User(id: "john"), token: <#T##Token#>)
                    

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


Client.shared.disconnect()
                    

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

Additional Parameters

1. Enable logs

By default logs are disabled.

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


Client.configureShared(.init(apiKey: <#API KEY#>, logOptions: .info))
                    

⚠️ 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 BaseURL


Client.configureShared(.init(apiKey: <#API KEY#>, baseURL: .dublin))
// or
Client.configureShared(.init(apiKey: <#API KEY#>, baseURL: "www.your-custom-url.com")!)
                    

3. Setup a custom dispatch queue for responses:


Client.configureShared(.init(apiKey: <#API KEY#>, callbackQueue: .global()))
                    

4. App in the background behavior

By default stayConnectedInBackground is true. The Client will try to keep connection when the app went to background to avoid a multiple reconnections. For example, when a user needs temporary switch to another app.


Client.configureShared(.init(apiKey: <#API KEY#>, stayConnectedInBackground: false))