---
source_url: https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/channels/watch
title: Watch channels
updated_at: 2026-06-15T12:11:31.858Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Watch channels

Use `onMessageReceived()` to receive messages on a channel without [joining](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/channels/join) as a member. The method subscribes to a [message event listener](https://www.pubnub.com/docs/sdks/kotlin/api-reference/configuration#event-listeners) and returns an `AutoCloseable` to stop receiving messages.

:::warning Deprecated method
`connect()` is deprecated. Use `onMessageReceived()` instead. Both methods have the same signature and behavior.
:::

### Method signature

`onMessageReceived()` accepts a callback function as an argument. The Chat SDK invokes this callback whenever someone sends a message on the given channel. This function takes a single argument of a [Message object](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/learn/chat-entities/message).

This method takes the following parameters:

```kotlin
channel.onMessageReceived(callback: (Message) -> Unit): AutoCloseable
```

#### Input

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| callback | (Message) | Yes |  | Callback function passed as a parameter. It defines the custom behavior to be executed whenever a message is received on a channel. |

#### Output

| Type | Description |
| --- | --- |
| `AutoCloseable` | Interface you can call to stop listening for new messages and clean up resources when they are no longer needed by invoking the `close()` method. |

### Sample code

Start receiving messages on the `support` channel.

```kotlin
// assume `chat` is an instance of your chat service or client
// get the "support" channel asynchronously
chat.getChannel("support").async { result ->
    result.onSuccess { supportChannel ->
        // handle success
        // start receiving messages on the "support" channel
        val subscription = supportChannel.onMessageReceived { message ->
            // handle the new message received on the channel
            println("New message received on 'support' channel: ${message.text}")
        }

        // The subscription can later be closed if needed
        // subscription.close()
    }.onFailure {
        // handle failure
    }
}
```

### Other examples

Stop receiving messages on the `support` channel.

```kotlin
// assume `chat` is an instance of your chat service or client
// get the "support" channel asynchronously
chat.getChannel("support").async { result ->
    result.onSuccess { supportChannel ->
        // handle success
        // start receiving messages on the "support" channel
        val subscription = supportChannel.onMessageReceived { message ->
            // handle the new message received on the channel
            println("New message received on 'support' channel: ${message.text}")
        }

        // later, when you want to stop receiving messages:
        subscription.close()
        println("Stopped receiving messages on 'support' channel.")
        
    }.onFailure {
        // handle failure
    }
}
```