Intermodule Communication

A method that allows modules in a custom view to communicate with each other.

Communication between modules happens by opening "channels" with the App instance:

// Module 1

import { App } from "@formant/data-sdk";

App.addChannelDataListener("test_channel", (event) => {
  if (event.source === App.getCurrentModuleContext()) {
    // listening for our own channel data
    App.showMessage("channel data i sent: " + JSON.stringify(event));
  } else {
    // listen for other's channel data
    App.showMessage("channel data: " + JSON.stringify(event));
  }
});


// Module 2

import { App } from "@formant/data-sdk";

(document.querySelector("#sendchannel") as HTMLElement).addEventListener(
  "click",
  () => {
    App.sendChannelData("test_channel", { abc: 123 });
  }
);

In the above example, "test_channel" is used to send arbitrary JSON information from Module 2 to Module 1. The source field of the event can tell you which module the data was sent from.

For an in-depth option of many ways to communicate between modules and the Formant app itself, see the following recipe: