Android Introduction

LAST EDIT Oct 08 2021

The Android SDK enables you to build any type of chat or messaging experience for Android. It consists of three major components:

  • Client: The client handles all API calls and receives events.

  • Offline: The offline library stores the data, implements optimistic UI updates, handles network failures, and exposes LiveData or StateFlow objects that make it easy to build your own UI on.

  • UI : The UI package includes ViewModels and custom views for common things like a channel list, message list, message input etc.

We typically recommend building a prototype with the full UI package. This is the fastest way to ensure that you have the integration set up correctly in combination with backend and other teams. After that, about half of our customers launch in production with our UI package.

There are some limitation to Android UI reusability, so if you have more complicated design needs, you'll want to drop down a level to the offline package. For most applications, you won't have to use the client directly (note that it's a lot of work to keep chat state updated manually, which you'd have to do when using the client).

Before going through these docs, we recommend trying out the Android Chat tutorial and taking a look at the Android Chat Sample App. You can also check out our API docs for more information, as well our source code on GitHub.

The online API tour is also a nice way to learn about how the API works. It's in-browser, and therefore Javascript based, but the ideas are pretty much the same in Kotlin.

Getting Started

Copied!

This guide quickly brings you up to speed on Stream’s Chat API. The API is flexible and allows you to build any type of chat or messaging. Note that if you build on our UI components, you won't need to call into this API too often after the initilization of the library.

You're currently not logged in. Create an account to automatically add your API key and secret to these code examples.

Add one of the three packages below to your dependencies for your module/app level build.gradle file:

For the latest version, check our GitHub releases page.

Chat Client

Copied!

Let's get started by initializing the client and connecting the current user:

The user token is typically provided by your backend when you login or register in the app. If authentication is disabled for your app, you can also use a ChatClient#devToken to generate an insecure token for development. Of course, you should never launch into production with authentication disabled.

For more complex token generation and expiration examples, have a look at Token Expiration.

Channels

Copied!

Let’s continue by creating your first channel. A channel contains messages, a list of members who are permanently associated with the channel, and a list of watchers currently watching the channel. The example below shows how to set up a channel:

The first two arguments are the channel type and the channel ID (messaging and travel in this case). In this case, the channel ID is explicit, but channels can also be created by specifying a list of members, in that case, an ID will be generated automatically.

The channel type controls the settings we’re using for this channel. There are four default types of channels:

  • livestream
  • messaging
  • team
  • commerce

These options provide you with the most sensible defaults for the given use cases. You can also define custom channel types if the defaults don’t work for your use case. See Channel Types for more info.

The extraData argument is an object containing custom channel data. You can add as many custom fields as you would like, as long as the total size of the object is less than 5KB. This applies not only to channels, but also messages, users, attachments, and reactions.

Messages

Copied!

Now that we have the channel set up, let's send our first chat message:

When you send a message to a channel, Stream Chat automatically broadcasts to all the people that are watching this channel and updates in real-time.

Querying Channels

Copied!

The client.queryChannels method enables you to retrieve a list of channels. You can specify a filter and sort order. The offline support library keeps the channels list updated as new messages, reactions and new channels arrive.

To learn more about which fields you can query and sort on have a look at the query channels documentation.

Conclusion

Copied!

Now that you understand the building blocks of a fully functional chat integration, let’s move on to the next sections of the documentation, where we dive deeper into details on each API endpoint.