publish( )
Publishes a message to a channel or channel resource.
Description
The publish()
function publishes a message to the specified channel or
channel resource.
When site visitors subscribe to a channel, they do not receive messages published to a resource on that same channel. Similarly, when site visitors subscribe to a channel resource, they do not receive messages published to that same channel without a specified resource.
To publish a message to specific users only, specify the users by ID in
the PublishOptions
object sent to the options
parameter.
To include the ID of the user who triggered the publish in the published message,
set the includePublisher
property of the PublishOptions
object to true
.
Syntax
function publish(channel: Channel, payload: *, [options: PublishOptions]): Promise<void> | void
publish Parameters
NAME
TYPE
DESCRIPTION
The channel, and optionally the resource, to publish to.
Message payload.
Max: 10kb
Options to use when publishing.
Returns
When the message is published to the channel or channel resource, void
is returned. Does not indicate that the message was received.
Return Type:
|
voidWas this helpful?
1import wixRealtimeBackend from 'wix-realtime-backend';23// ...45const channel = {"name": "someChannel"};67wixRealtimeBackend.publish(channel, "My message")8 .then(() => {9 // published10 });
1import wixRealtimeBackend from 'wix-realtime-backend';23// ...45const channel = {6 "name": "someChannel",7 "resourceId": "someId"8};910wixRealtimeBackend.publish(channel, "My message")11 .then(() => {12 // published13 });
1import wixRealtimeBackend from 'wix-realtime-backend';23// ...45const channel = {"name": "someChannel"};67const message = {8 "firstKey": "firstValue",9 "secondKey": "secondValue"10};1112const user1 = // get ID of first user to publish to13const user2 = // get ID of second user to publish to1415const options = {16 "users" : [user1, user2],17 "includePublisher": true18};1920wixRealtimeBackend.publish(channel, message, options)21 .then( () => {22 // published23 } );
You can test out the code in our example template.
1import { subscribe } from 'wix-realtime-frontend';2import { publishMessage } from 'backend/realtime';34const channelA = { name: 'a' };5const channelB = { name: 'b' };6const channelB1 = { name: 'b', resourceId: '1' };7const channelB2 = { name: 'b', resourceId: '2' };89$w.onReady(function () {10 subscribeToChannels();11 setupPublishButtons();12});1314function subscribeToChannels() {15 subscribe(channelA, messageHandler);16 subscribe(channelB, messageHandler);17 subscribe(channelB1, messageHandler);18 subscribe(channelB2, messageHandler);19}2021function setupPublishButtons() {22 $w('#publishButtonA').onClick(event => {23 publish(event.target, channelA);24 });2526 $w('#publishButtonB').onClick(event => {27 publish(event.target, channelB);28 });2930 $w('#publishButtonB1').onClick(event => {31 publish(event.target, channelB1);32 });3334 $w('#publishButtonB2').onClick(event => {35 publish(event.target, channelB2);36 });37}3839async function publish(button, channel) {40 if ($w('#message').value) {41 button.disable();42 $w('#message').disable();4344 await publishMessage(channel, $w('#message').value);4546 $w('#message').value = undefined;47 $w('#message').enable();48 button.enable();49 }50}5152function messageHandler(message, channel) {53 let textBoxId;5455 if (channel.name === 'a') {56 textBoxId = '#receivedMessagesA';57 } else { // Channel B - check resource ID58 if (channel.resourceId === '1') {59 textBoxId = '#receivedMessagesB1';60 } else if (channel.resourceId === '2') {61 textBoxId = '#receivedMessagesB2';62 } else {63 textBoxId = '#receivedMessagesB';64 }65 }6667 $w(textBoxId).value += (message.payload + '\n');68}69