messageport

The messageport JavaScript object enables passing a JavaScript object as an associative array from JavaScript to BrightScript. This interface works whether the script is executing inside an HTML widget created by BrightScript, an HTML widget created from JavaScript, or a Node.js® instance. 

This interface is the preferred way for JavaScript content to communicate with its parent application.

messageport IDL

interface BsMessage { attribute String type; }; callback MessagePortCallback = void (BsMessage event); interface MessagePort { void PostBSMessage(Object message); void addEventListener(String type, MessagePortCallback callback); void removeEventListener(String type, MessagePortCallback callback); };

ON THIS PAGE





Object Creation

To create a messageport object first load the @brightsign/messageport module using the require() method. 

var MESSAGE_PORT = require("@brightsign/messageport"); var bsMessage = new MESSAGE_PORT();

MessagePort

PostBsMessage ()
PostBsMessage(Object message)

Objects passed to PostBSMessage() will be received as events in the parent application:

  • for HTML widgets created from BrightScript, the parent roHtmlWidget will generate a roHtmlWidgetEvent with reason set to "message"

  • for HTML widgets created from JavaScript, the parent @brightsign/htmlwidget will generate a "message" event

  • for Node applications, the parent roNodeJs object will generate a roNodeJsEvent with reason set to "message"

Examples

The following script will send a collection of properties to BrightScript:

The message will appear in BrightScript as an roNodeJsEvent. In this case, the GetData().reason equals "message" and GetData().message contains the roAssociativeArray.

The following script will display the contents of the event:

In BrightScript, the roNodeJs.PostJSMessage() method can be used to post a message to JavaScript: