---
source_url: https://www.pubnub.com/docs/chat/swift-chat-sdk/build/features/channels/watch
title: Watch channels
updated_at: 2026-06-16T12:48:53.108Z
---

> 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/swift-chat-sdk/build/features/channels/join) as a member. The method subscribes to a [message event listener](https://www.pubnub.com/docs/sdks/swift/api-reference/configuration#event-listeners) and returns an `AutoCloseable` handle to stop listening.

You can also use `channel.stream.messages()` to receive messages as an `AsyncStream`.

:::note Deprecation
`connect()` is deprecated. Use `onMessageReceived()` (closure-based) or `channel.stream.messages()` (AsyncStream-based) instead.
:::

## Receive messages (closure)

`onMessageReceived()` calls a closure whenever a new message is published on the channel.

### Method signature

```swift
channel.onMessageReceived(
    callback: @escaping (MessageImpl) -> Void
) -> AutoCloseable
```

#### Input

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| callback | (MessageImpl) | Yes |  | Closure called whenever a new message is published on the channel. |

#### Output

| Parameter | Description |
| --- | --- |
| `AutoCloseable` | An object you must retain. When released or closed, the listener stops and the channel is unsubscribed. |

### Sample code

:::tip Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
:::

Start receiving messages on the `support` channel.

###### Closure

```swift
// Assumes a "ChannelImpl" reference named "channel"
  
// Important: Keep a strong reference to the returned "AutoCloseable" object as long as you want
// to receive updates. If the "AutoCloseable" is deallocated, the stream will be cancelled,
// and no further items will be produced. You can also stop receiving messages manually
// by calling the "close()" method on the "AutoCloseable" object.
autoCloseable = channel.connect { message in
  debugPrint("Received message: \(message.content)")
}
```

###### AsyncStream

```swift
// Assumes a "ChatImpl" reference named "chat"
Task {
  if let channel = try await chat.getChannel(channelId: "support") {
    for await message in channel.connect() {
      debugPrint("Received message: \(message)")
    }
  } else {
    debugPrint("Channel not found")
  }
}
```