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 a function to disconnect.

Deprecated

connect() is deprecated as of chat-1-0-0. Use onMessageReceived() instead.

icon

Under the hood

Method signature

onMessageReceived() 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.onMessageReceived(
2 callback: (message: Message) => void
3): () => 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 "onMessageReceived()" method
4channel.onMessageReceived((message: Message) => {
5 console.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.onMessageReceived(/* handle message callback */)
3// after some time...
4disconnect()