> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt # Method name: publish(channel: Channel, payload: any, options: PublishOptions) # Method package: wixRealtimeBackend # Method menu location: wixRealtimeBackend --> publish # Method Link: https://dev.wix.com/docs/velo/velo-only-apis/wix-realtime-backend/publish.md # Method Description: Publishes a message to a channel or channel resource. 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`. # Method Code Examples: *** Note: do not assume any prop names or enum values other than the ones in the example. ## Publish to a channel ```javascript import wixRealtimeBackend from 'wix-realtime-backend'; // ... const channel = {"name": "someChannel"}; wixRealtimeBackend.publish(channel, "My message") .then(() => { // published }); ``` ## Publish to a resource on a channel ```javascript import wixRealtimeBackend from 'wix-realtime-backend'; // ... const channel = { "name": "someChannel", "resourceId": "someId" }; wixRealtimeBackend.publish(channel, "My message") .then(() => { // published }); ``` ## Publish to specific users on a channel and include publisher information ```javascript import wixRealtimeBackend from 'wix-realtime-backend'; // ... const channel = {"name": "someChannel"}; const message = { "firstKey": "firstValue", "secondKey": "secondValue" }; const user1 = // get ID of first user to publish to const user2 = // get ID of second user to publish to const options = { "users" : [user1, user2], "includePublisher": true }; wixRealtimeBackend.publish(channel, message, options) .then( () => { // published } ); ``` ## Send and receive messages across multiple channels and channel resources ```javascript import { subscribe } from 'wix-realtime-frontend'; import { publishMessage } from 'backend/realtime'; const channelA = { name: 'a' }; const channelB = { name: 'b' }; const channelB1 = { name: 'b', resourceId: '1' }; const channelB2 = { name: 'b', resourceId: '2' }; $w.onReady(function () { subscribeToChannels(); setupPublishButtons(); }); function subscribeToChannels() { subscribe(channelA, messageHandler); subscribe(channelB, messageHandler); subscribe(channelB1, messageHandler); subscribe(channelB2, messageHandler); } function setupPublishButtons() { $w('#publishButtonA').onClick(event => { publish(event.target, channelA); }); $w('#publishButtonB').onClick(event => { publish(event.target, channelB); }); $w('#publishButtonB1').onClick(event => { publish(event.target, channelB1); }); $w('#publishButtonB2').onClick(event => { publish(event.target, channelB2); }); } async function publish(button, channel) { if ($w('#message').value) { button.disable(); $w('#message').disable(); await publishMessage(channel, $w('#message').value); $w('#message').value = undefined; $w('#message').enable(); button.enable(); } } function messageHandler(message, channel) { let textBoxId; if (channel.name === 'a') { textBoxId = '#receivedMessagesA'; } else { // Channel B - check resource ID if (channel.resourceId === '1') { textBoxId = '#receivedMessagesB1'; } else if (channel.resourceId === '2') { textBoxId = '#receivedMessagesB2'; } else { textBoxId = '#receivedMessagesB'; } } $w(textBoxId).value += (message.payload + '\n'); } ``` ---