Watch channels

You can let users watch a given channel and its messages using the connect() method, without the need to join the channel as members.

Under the hood, this method allows your app to receive messages on a given channel by subscribing it to a message event listener underneath to receive all message events of type text. This method also returns a function you can invoke to stop receiving message events and unsubscribe from the channel.

icon

Under the hood

Method signature

connect() 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.

This method takes the following parameters:

channel.connect(
callback: (
message: Message
) => void
): () => void

Input

ParameterTypeRequiredDefaultDescription
callbackn/aYesn/aCallback function passed as a parameter. It defines the custom behavior to be executed whenever someone sends a message on the given channel.
 → messageMessageYesn/aAny Message object received on this channel.

Output

TypeDescription
() => voidFunction you can call to disconnect (unsubscribe) from the channel and stop receiving message events.

Basic usage

Start receiving messages on the support channel.

// reference the "channel" object
const channel = await chat.getChannel("support")
// invoke the "connect()" method
channel.connect((message: Message) => {
console.log(
"This is my first message on this channel! Nice to meet you all!", message.content.text)
})

Other examples

Stop receiving messages on the support channel.

const channel = await chat.getChannel("support")
const disconnect = channel.connect(/* handle message callback */)
// after some time...
disconnect()
Last updated on