> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/ably/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Getting started: Chat with Android

This guide will help you get started with Ably Chat in a new Android Kotlin application built with Jetpack Compose.

You'll learn how to create chat rooms, send messages, and implement realtime features like typing indicators and presence.

## Prerequisites

1. [Sign up](https://ably.com/signup) for an Ably account.

2. Create a [new app](https://ably.com/accounts/any/apps/new), and get your first API key.

3. Create a new Android project with Jetpack Compose. For detailed instructions, refer to the [Android Studio documentation](https://developer.android.com/jetpack/compose/setup).

4. Add the Ably dependencies to your app-level `build.gradle.kts` file:

<Code>
  ```android theme={null}
  implementation("com.ably.chat:chat:1.2.0")
  implementation("com.ably.chat:chat-extensions-compose:1.2.0")
  ```
</Code>

## Step 1: Set Up Ably

In production, you should use [token authentication](/docs/auth/token) to avoid exposing your API keys publicly.

Replace the contents of your `MainActivity.kt` file:

<Code>
  ```android theme={null}
  package com.example.chatexample

  import android.os.Bundle
  import androidx.activity.ComponentActivity
  import androidx.activity.compose.setContent
  import androidx.compose.material3.*
  import androidx.compose.runtime.*
  import com.ably.chat.*
  import io.ably.lib.realtime.AblyRealtime
  import io.ably.lib.types.ClientOptions

  class MainActivity : ComponentActivity() {
      private lateinit var chatClient: ChatClient

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)

          val realtimeClient = AblyRealtime(
              ClientOptions().apply {
                  key = "{{API_KEY}}"
                  clientId = "my-first-client"
              },
          )

          chatClient = ChatClient(realtimeClient) { logLevel = LogLevel.Info }

          setContent {
              App(chatClient)
          }
      }
  }
  ```
</Code>

## Step 2: Create a Room and Send Messages

Add composable functions to create a room and send messages:

<Code>
  ```android theme={null}
  @Composable
  fun App(chatClient: ChatClient) {
      val roomName = "my-first-room"
      var room by remember { mutableStateOf<Room?>(null) }
      
      LaunchedEffect(roomName) {
          val chatRoom = chatClient.rooms.get(roomName)
          chatRoom.attach()
          room = chatRoom
      }
      
      Scaffold { paddingValues ->
          Column(modifier = Modifier.padding(paddingValues)) {
              room?.let { ChatBox(it) }
          }
      }
  }

  @Composable
  fun ChatBox(room: Room) {
      var textInput by remember { mutableStateOf("") }
      val messages = remember { mutableStateListOf<Message>() }
      val scope = rememberCoroutineScope()

      LaunchedEffect(room) {
          room.messages.asFlow().collect { event ->
              when (event.type) {
                  MessageEventType.Created -> messages.add(0, event.message)
                  else -> Unit
              }
          }
      }

      Column {
          LazyColumn(reverseLayout = true) {
              items(messages.size) {
                  Text(messages[it].text)
              }
          }
          
          Row {
              OutlinedTextField(
                  value = textInput,
                  onValueChange = { textInput = it },
                  modifier = Modifier.weight(1f)
              )
              Button(onClick = {
                  scope.launch {
                      room.messages.send(text = textInput)
                      textInput = ""
                  }
              }) {
                  Text("Send")
              }
          }
      }
  }
  ```
</Code>

## Next Steps

* Understand [token authentication](/docs/auth/token) before going to production
* Read more about using [rooms](/docs/chat/rooms?lang=kotlin) and sending [messages](/docs/chat/rooms/messages?lang=kotlin)
* Find out more regarding [presence](/docs/chat/rooms/presence?lang=kotlin)

Explore the [Ably CLI](https://www.npmjs.com/package/@ably/cli) further, or check out the [Chat Kotlin API references](https://sdk.ably.com/builds/ably/ably-chat-kotlin/main/dokka/chat/com.ably.chat/) for additional functionality.
