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.

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 } );