On this page

Watch channels

Use onMessageReceived() to receive messages on a channel without joining as a member. The method subscribes to a message event listener and returns an AutoCloseable handle to stop listening.

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

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

1channel.onMessageReceived(
2 callback: @escaping (MessageImpl) -> Void
3) -> AutoCloseable

Input

* required
ParameterDescription
callback *
Type: (MessageImpl) -> Void
Default:
n/a
Closure called whenever a new message is published on the channel.

Output

ParameterDescription
AutoCloseable
An object you must retain. When released or closed, the listener stops and the channel is unsubscribed.

Sample code

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.

1

Last updated on