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
-
message
(Object | String):
The incoming message payload. The structure of this depends on the implementation.Available message types
Type Payload Description widgetStatus The status of the hosting widget hostStatus The status of the hosting page currentView The page's current view (Team/Staff) -
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
).
- Success or failure status (
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
ack
FunctionSignature:
ack(status?: boolean, data?: string | object);
Parameters:
-
status
(Optional,boolean
):true
: Indicates success.false
: Indicates failure.
-
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.
Updated 7 days ago