subscribe( )

Subscribes to a channel or channel resource.

The subscribe() function subscribes to receive messages published on the specified channel or channel resource and sets a function that handles incoming messages when they are published to the channel.

When you subscribe to a channel you do not receive messages published to a resource on that same channel. To receive those messages, you must explicitly subscribe to the specific resource.

The specified message handler is run each time a message is published on the given channel or channel resource. Use it to define what happens when messages are received.

You can subscribe to a channel or channel resource more than once to add multiple message handlers. Each subscription returns a unique subscription ID which can be used later to unsubscribe from the specific subscription with that ID, without affecting the other subscriptions on the same channel or channel resource.

Method Declaration
Method Parameters

The channel, and optionally the resource, to subscribe to.


handler(message: Message, channel: Channel): void The name of the function or the function expression to run when something is published to the subscription's channel or channel resource.

Return Type:Promise<string>
Was this helpful?