Introduction

Wix Chat events are fired on your site's backend when certain events related to the Wix Chat application occur. You can write event handlers that react to these events. Event handler functions receive data that corresponds to the event that fired. Use event handlers to create custom responses to chat message events.

To add a chat event handler, add an events.js file to the Backend section of your site if one does not already exist. All event handler functions for your site are defined in this file.

Event handler functions are defined using the following pattern:

Copy

For example, an event handler that handles backend chat messages looks like this:

Copy

Note that backend events don't work when previewing your site.

Did this help?

onMessage( )


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

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.

Method Declaration
Copy
function onMessage(event: SendMessageEvent): void;
Method Parameters
eventSendMessageEventRequired

The message data.

JavaScript
export function wixChat_onMessage(event) { const message = event.payload.text; const participant = event.participantId; } /* Example SendMessageEvent object: * * { * "channelId": "23b345b6-c78d-9012-e3f4-567g89h0i01k", * "type": "TEXT", * "summary": "Hey, I've got a question about your products", * "participantId": "12a345b6-e78f-8011-f3f5-567g89h0i12j", * "direction": "VisitorToBusiness", * "createdAt": "2019-10-27T06:02:12.008Z", * "payload": { * "text": "Hey, I've got a question about your products" * }, * "metadata": {} * } */
Did this help?