addEventListener (callback)

Registers a listener to handle incoming messages. The listener receives two arguments: the message and an acknowledgment (ack) function.

Syntax

messageHandler.addListener((message, ack) => { ... });

Parameters

  1. message (Object | String):
    The incoming message payload. The structure of this depends on the implementation.

    Available message types

    TypePayloadDescription
    widgetStatusThe status of the hosting widget
    hostStatusThe status of the hosting page
    currentViewThe page's current view (Team/Staff)
  2. ack (Function):
    A function to acknowledge receipt of the message. Can optionally include:

    • Success or failure status (boolean).
    • Additional data, such as status messages or payloads (Object | String).

Usage

Here’s how to add a listener and use the ack function:

Example: Basic Success Acknowledgment

messageHandler.addListener((message, ack) => {
  console.log('Message received', message);
  ack(); // Acknowledge success with no additional data
});

Example: Success with Additional Information

messageHandler.addListener((message, ack) => {
  console.log('Processing message:', message);
  ack(true, '3 records added'); // Acknowledge success with a status message
});

Example: Success with Payload

messageHandler.addListener((message, ack) => {
  ack(true, { Uid: 123 }); // Acknowledge success with additional payload
});

Example: Failure with Error Message

messageHandler.addListener((message, ack) => {
  ack(false, 'Unauthorized'); // Acknowledge failure with a reason
});

Example: Failure with Detailed Error Object

messageHandler.addListener((message, ack) => {
  ack(false, { errorCode: 404 }); // Acknowledge failure with an error object
});

ack Function

Signature:

ack(status?: boolean, data?: string | object);

Parameters:

  1. status (Optional, boolean):

    • true: Indicates success.
    • false: Indicates failure.
  2. data (Optional, string | object):
    Additional data to provide context for the acknowledgment. Could be a string message or an object containing more detailed information.


Notes

  • If ack is not called, the message will not be acknowledged, which may cause retries or other behavior depending on the implementation.
  • Use meaningful messages or payloads in the acknowledgment to help downstream systems handle the response effectively.