Skip to main content

Getting Started#

Let's see how you can get started with the Android Chat SDK after adding the required dependencies. This page shows you how to initialize the SDK in your app.

note

If you're looking for a complete, step-by-step guide that includes setting up an Android project from scratch, try the Android In-App Messaging Tutorial instead.

Creating a ChatClient#

Your first step is initializing the ChatClient, which is the main entry point for all operations in the library. ChatClient is a singleton: you'll create it once and re-use it across your application.

A best practice is to initialize ChatClient in the Application class:

class App : Application() {   override fun onCreate() {       super.onCreate()       val chatClient = ChatClient.Builder("apiKey", context).build()   }}

The Builder for ChatClient exposes configuration options for features such as Logging.

note

To generate an API key, you can sign up for a free 30-day trial. You can then access your api key in the Dashboard.

If you create the ChatClient instance following the pattern in the previous example, you will be able to access that instance from any part of your application using the instance() method:

class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        val chatClient = ChatClient.instance() // Returns the singleton instance    }}

Creating a ChatDomain#

If you want to have offline support or use the UI Components package, you'll need to initialize the ChatDomain class in addition to ChatClient. You can skip this initialization step if you're only using the low-level client.

The initialization is done similarly to ChatClient, using a builder, which exposes several methods for configuring the SDK's behavior:

val chatClient = ChatClient.Builder(apiKey, appContext).build()
val chatDomain = ChatDomain.Builder(appContext, chatClient)    .offlineEnabled()    .userPresenceEnabled()    .build()

For more about working with ChatDomain, see Working with Offline Support.

Connecting a User#

The next step is connecting the user. This requires a valid Stream Chat token. As you must use your API_SECRET to create this token, it is unsafe to generate this token outside of a secure server.

val user = User(    id = "bender",    extraData = mutableMapOf(        "name" to "Bender",        "image" to "https://bit.ly/321RmWb",    ),)
ChatClient.instance().connectUser(user = user, token = "userToken") // Replace with a real token    .enqueue { result ->        if (result.isSuccess) {            // Handle success        } else {            // Handle error        }    }
note

To learn about how to create a token and different user types, see Tokens & Authentication.

If the connectUser call was successful, you are now ready to use the SDK! 🎉

The methods of the ChatClient class allow you to create channels, send messages, add reactions, and perform many more low-level operations. You can also use the SDK's pre-built UI Components that will perform data fetching and sending for you, as described below.

Adding UI Components#

There are two UI Component implementations available: one built on regular, XML based Android Views, and another built from the ground up in Jetpack Compose.

Take a look at the Overview pages of the implementations to get started with them:

Did you find this page helpful?