Player APIs (BrightScript & Ja...
JavaScript APIs

messageport

6min
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); }; 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 docid 7nvhxjmwlbcii8dx4a4p3 with reason set to "message" examples the following script will send a collection of properties to brightscript function myfunction() { var bsmessage = new require("@brightsign/messageport"); bsmessage postbsmessage({complete true, result "pass"}); } the message will appear in brightscript as an ronodejsevent in this case, the getdata() reason equals "message" and getdata() message contains the roassociativearray while not finished ev = mp waitmessage(30000) if type(ev) <> "rohtmlwidgetevent" then print type(ev) stop end if payload = ev getdata() print payload print "reason "; payload reason if payload reason = "message" then print "message "; payload message if payload message complete = invalid then stop else if payload message complete = "true" then finished = true if payload message result = "pass" then print "test passed" else print "test failed "; payload message err stop end if end if end if end while the following script will display the contents of the event var message port = require("@brightsign/messageport"); var bsmessage = new message port(); bsmessage addeventlistener('bsmessage', function(msg) { console log(json stringify(msg)); }) in brightscript, the ronodejs postjsmessage() method can be used to post a message to javascript nodejs postjsmessage({ param1 "data1", param2 "data2", param3 "data3" }) the messageport interface is the preferred way for javascript content to communicate with its parent application the messageport is a shared bus, so if you enable it in your application, you can also get messages from the brightsign os in that application to send/receive messages in brightauthor\ connected node js applications, see this example https //github com/brightsign/dev cookbook/blob/c34eea462428dde39cc3d6fcb80bbfb6ff73876c/examples/send plugin message/pluginmessageapp html#l14 in our github repository