Search.../

onMessage( )

An event that fires when a chat message is sent to or from the business.

Description

The onMessage() event handler runs when a backend chat message is sent to or from the business. The received SendMessageEvent object contains information about the message that was sent.

Note: Backend events don't work when previewing your site.

Syntax

function onMessage(event: SendMessageEvent): void

onMessage Parameters

NAME
TYPE
DESCRIPTION
event
SendMessageEvent

The message data.

Returns

This function does not return anything.

Return Type:

void

Was this helpful?

An event when a chat message is sent

Copy Code
1export function wixChat_onMessage(event) {
2 const message = event.payload.text;
3 const participant = event.participantId;
4}
5
6 /* Example SendMessageEvent object:
7 *
8 * {
9 * "channelId": "23b345b6-c78d-9012-e3f4-567g89h0i01k",
10 * "type": "TEXT",
11 * "summary": "Hey, I've got a question about your products",
12 * "participantId": "12a345b6-e78f-8011-f3f5-567g89h0i12j",
13 * "direction": "VisitorToBusiness",
14 * "createdAt": "2019-10-27T06:02:12.008Z",
15 * "payload": {
16 * "text": "Hey, I've got a question about your products"
17 * },
18 * "metadata": {}
19 * }
20 */
Get message text from an input element and send it as a chat message

In this example, we use an input element to get the text of a chat message from a site visitor. We get the business channel ID and send the chat message to the business.

Copy Code
1/*************
2 * page code *
3 *************/
4import { sendChatMessage } from 'backend/chat';
5import wixWindow from 'wix-window';
6
7// ...
8
9export async function sendMessageButton_click(event) {
10 const channel = await $w("#myChatbox").getChannel({ type: "Business" });
11 const channelId = channel.id;
12
13 const messageText = $w('#textInput').value;
14 const sendAsVisitor = "true";
15
16 sendChatMessage(messageText, channelId, sendAsVisitor);
17}
18
19/****************************
20 * Backend code - chat.jsw *
21 ****************************/
22import wixChatBackend from 'wix-chat-backend';
23
24export function sendChatMessage(messageText, channelId, sendAsVisitor) {
25 wixChatBackend.sendMessage({
26 "messageText": messageText,
27 "channelId": channelId,
28 "sendAsVisitor": sendAsVisitor
29 })
30}
31
32/**********************************
33 * Backend event code - events.js *
34 **********************************/
35export function wixChat_onMessage(event) {
36 const chatMessage = event.payload.text;
37 const chatParticipant = event.participantId;
38}
39
40/* Example SendMessageEvent object:
41 *
42 * {
43 * "channelId": "23b345b6-c78d-9012-e3f4-567g89h0i01k",
44 * "type": "TEXT",
45 * "summary": "Hey, I've got a question about your products",
46 * "participantId": "12a345b6-e78f-8011-f3f5-567g89h0i12j",
47 * "direction": "VisitorToBusiness",
48 * "createdAt": "2019-10-27T06:02:12.008Z",
49 * "payload": {
50 * "text": "Hey, I've got a question about your products"
51 * },
52 * "metadata": {}
53 * }
54 */
55