subscribe( )
Subscribes to a channel or channel resource.
Description
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.
Syntax
function subscribe(channel: Channel, handler: MessageHandler): Promise<string>handler: function MessageHandler(message: Message, channel: Channel): void
subscribe Parameters
NAME
TYPE
DESCRIPTION
The channel, and optionally the resource, to subscribe to.
The name of the function or the function expression to run when something is published to the subscription's channel or channel resource.
Returns
Fulfilled - An ID representing the subscription, used for unsubscribing. Rejected - Error that caused the rejection, such as insufficient permissions.
Return Type:
MessageHandler Parameters
NAME
TYPE
DESCRIPTION
The message that was published.
The channel the message was published on.
Returns
This function does not return anything.
Return Type:
Was this helpful?
1import wixRealtimeFrontend from 'wix-realtime-frontend';23// ...45const channel = {"name": "someChannel"};6let subscriptionId;78wixRealtimeFrontend.subscribe(channel, (message, channel) => {9 let payload = message.payload;10 let channelName = channel.name;11 if(message.publisher) {12 let publisherId = message.publisher.id;13 }14})15.then((id) => {16 subscriptionId = id;17});
1import wixRealtimeFrontend from 'wix-realtime-frontend';23// ...45const channel = {6 "name": "someChannel",7 "resourceId": "someId"8};9let subscriptionId;1011wixRealtimeFrontend.subscribe(channel, (message, channel) => {12 let payload = message.payload;13 let channelName = channel.name;14 let resourceId = channel.resourceId;15 if(message.publisher) {16 let publisherId = message.publisher.id;17 }18})19.then((id) => {20 subscriptionId = id;21});
This example demonstrates how to send and receive messages across a single channel. You can test out the code in our example template.
1import { subscribe } from 'wix-realtime-frontend';2import { publishMessage } from 'backend/realtime';34const channel = { name: 'hello-world' };56$w.onReady(function () {7 subscribe(channel, (message) => {8 $w('#receivedMessages').value += (message.payload + '\n');9 });1011 $w('#publishMessageButton').onClick(async () => {12 if($w('#message').value) {13 $w('#publishMessageButton').disable();14 $w('#message').disable();1516 await publishMessage(channel, $w('#message').value);1718 $w('#message').value = undefined;19 $w('#message').enable();20 $w('#publishMessageButton').enable();21 }22 });23});24
This example demonstrates how to display a popup notification when a message is received on a channel that a visitor is subscribed to. The notification displays the contents of the message that was received.
The example assumes the page contains a box that contains a text element and a button. The box is set to be hidden when the page loads.
When a message is received, the text element's text value is set to the contents of the message and the box is shown to display the message to the site visitor. When the button is clicked, the box is hidden again.
1import wixRealtimeFrontend from 'wix-realtime-frontend';23// ...45$w.onReady(function() {6 const channel = {"name": "updates"};78 wixRealtimeFrontend.subscribe( channel, (message, channel) => {9 $w("#updateMessage").text = message.payload;10 $w("#updateBox").show();11 });1213 $w("#hideUpdateButton").onClick( () => {14 $w("#updateBox").hide();15 });16});1718