addZpaxMessageListener

Adds a listener for a specific message type coming from the myzPAX parent container.

This function allows the embedded (iframe) application to respond to messages sent from myzPAX using postMessage. It verifies the message type and origin before invoking the provided handler.

Note: Ensure setTargets() is called before using this to define which origins are trusted.

Type Parameters

  • T extends keyof ResponseMessage
    The type of message to listen for. Must be a key in the ResponseMessage type.

Parameters

NameTypeDescription
messageTypeTThe name/type of the message to listen for.
handlerResponseMessageHandler<T>The callback function to invoke when the message is received.

Returns

  • () => void
    A cleanup function that removes the event listener when called.

Type Definition Reference

ResponseMessageHandler

Example Usage

// Listen for a 're_authenticated' message from myzPAX
const unsubscribe = addZpaxMessageListener('re_authenticated', () => {
  console.log('User re-authenticated');
});

// Later, remove the listener
unsubscribe();

Related

  • setTargets() – Must be called first to configure valid message origins.
  • sendZpaxMessage() – Used to send messages to the parent container.
  • ResponseMessage – Defines the available incoming message types and their expected payloads.