Search.../

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
channel
Channel

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

payload
*

Message payload.

Max: 10kb

options
Optional
PublishOptions

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:

Promise<void>

 | 

void

Was this helpful?

Publish to a channel

Copy Code
1import wixRealtimeBackend from 'wix-realtime-backend';
2
3// ...
4
5const channel = {"name": "someChannel"};
6
7wixRealtimeBackend.publish(channel, "My message")
8 .then(() => {
9 // published
10 });
Publish to a resource on a channel

Copy Code
1import wixRealtimeBackend from 'wix-realtime-backend';
2
3// ...
4
5const channel = {
6 "name": "someChannel",
7 "resourceId": "someId"
8};
9
10wixRealtimeBackend.publish(channel, "My message")
11 .then(() => {
12 // published
13 });
Publish to specific users on a channel and include publisher information

Copy Code
1import wixRealtimeBackend from 'wix-realtime-backend';
2
3// ...
4
5const channel = {"name": "someChannel"};
6
7const message = {
8 "firstKey": "firstValue",
9 "secondKey": "secondValue"
10};
11
12const user1 = // get ID of first user to publish to
13const user2 = // get ID of second user to publish to
14
15const options = {
16 "users" : [user1, user2],
17 "includePublisher": true
18};
19
20wixRealtimeBackend.publish(channel, message, options)
21 .then( () => {
22 // published
23 } );
Send and receive messages across multiple channels and channel resources

You can test out the code in our example template.

Copy Code
1import { subscribe } from 'wix-realtime-frontend';
2import { publishMessage } from 'backend/realtime';
3
4const channelA = { name: 'a' };
5const channelB = { name: 'b' };
6const channelB1 = { name: 'b', resourceId: '1' };
7const channelB2 = { name: 'b', resourceId: '2' };
8
9$w.onReady(function () {
10 subscribeToChannels();
11 setupPublishButtons();
12});
13
14function subscribeToChannels() {
15 subscribe(channelA, messageHandler);
16 subscribe(channelB, messageHandler);
17 subscribe(channelB1, messageHandler);
18 subscribe(channelB2, messageHandler);
19}
20
21function setupPublishButtons() {
22 $w('#publishButtonA').onClick(event => {
23 publish(event.target, channelA);
24 });
25
26 $w('#publishButtonB').onClick(event => {
27 publish(event.target, channelB);
28 });
29
30 $w('#publishButtonB1').onClick(event => {
31 publish(event.target, channelB1);
32 });
33
34 $w('#publishButtonB2').onClick(event => {
35 publish(event.target, channelB2);
36 });
37}
38
39async function publish(button, channel) {
40 if ($w('#message').value) {
41 button.disable();
42 $w('#message').disable();
43
44 await publishMessage(channel, $w('#message').value);
45
46 $w('#message').value = undefined;
47 $w('#message').enable();
48 button.enable();
49 }
50}
51
52function messageHandler(message, channel) {
53 let textBoxId;
54
55 if (channel.name === 'a') {
56 textBoxId = '#receivedMessagesA';
57 } else { // Channel B - check resource ID
58 if (channel.resourceId === '1') {
59 textBoxId = '#receivedMessagesB1';
60 } else if (channel.resourceId === '2') {
61 textBoxId = '#receivedMessagesB2';
62 } else {
63 textBoxId = '#receivedMessagesB';
64 }
65 }
66
67 $w(textBoxId).value += (message.payload + '\n');
68}
69