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.
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
| Parameter | Description |
|---|---|
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: MessageDefault: n/a | Any Message object received on this channel. |
Output
| Type | Description |
|---|---|
() => 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()