On this page

Watch channels

Use connect() to receive messages on a channel without joining as a member. The method subscribes to a message event listener and returns a function to disconnect.

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:

1channel.connect(
2 callback: (
3 message: Message
4 ) => void
5): () => void

Input

* required
ParameterDescription
callback *
Type: n/a
Default:
n/a
Callback function passed as a parameter. It defines the custom behavior to be executed whenever someone sends a message on the given channel.
 → message *
Type: Message
Default:
n/a
Any Message object received on this channel.

Output

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

Sample code

Start receiving messages on the support channel.

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

Other examples

Stop receiving messages on the support channel.

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