2

roNodeJS: BrightScript to NodeJS event handler

I can't seem to get messaging from BrightScript into an roNodeJS application working.  I have a basic PoC setup, and I'm able to send messages from my Node application to BrightScript, but not vice versa.

autorun.brs

sub Main()
    msgPort = CreateObject("roMessagePort")
    node = CreateObject("roNodeJS","index.js",{message_port:msgPort})

    log = CreateObject("roSystemLog")
    while true
        msg = wait(0,msgPort)
        if type(msg) = "roNodeJsEvent" then
            payload = msg.GetData()
            if payload.reason = "message" then
                log.SendLine("===== Brightscript Rx from Node -> hello." + payload.message.hello)
                txresult = node.PostJSMessage({hello:"world"})
            end if
        end if
    end while
end sub

 

index.js:

var mqtt = require('mqtt');

var MSG_PORT = require('@brightsign/messageport');
var bsMsg = new MSG_PORT();

var mclient = mqtt.connect('mqtt://192.168.3.54');

// Connect to MQTT client
mclient.on('connect', function () {

    // ... and subscribe to all topics related to brightsign
    mclient.subscribe('brightsign/#', function (err) {

        if (!err) {      
            // Publish a message to MQTT
            mclient.publish('brightsign/status', 'Hello mqtt from BrightSign')

            // send a message from NodeJS into BrightScript
            bsMsg.PostBSMessage({hello:"Hello from NodeJS"});
        }
    })
})

// Capture and print to console any MQTT messages related to brightsign topics
mclient.on('message', function (topic, message) {
    // message is Buffer
    console.log(topic.toString() + ": " + message.toString())
    mclient.end()
})

// event handler for processing messages sent from BrightScript into NodeJS

bsMsg.onbsmessage = function(msg) {
    console.log('@@@@@ Node received a message from Brightscript');
}

setInterval(function(){console.log("ping")}, 10000)

In the code above, it seems as though bsMsg.onbsmessage never fires.  from the BrightScript Debug console, I can see that txresult returns true.

3 comments

  • 0
    Avatar
    Bright Scripters

    Try this

    bsMessage.addEventListener('bsmessage', function(msg) { console.log(JSON.stringify(msg)); })

  • 0
    Avatar
    Nick Miller

    I noticed the updated documentation on the @brightscript/messageport API today. (Page Comparison - messageport (v.19 vs v.20) - Documentation - BrightSign Documentation)

     

    the modified example code has resolved the issue in my proof of concept.

  • 0
    Avatar
    Bright Scripters

    Glad to hear.

    Thank you Romeo for responding to my support ticket in record time :)

     

Please sign in to leave a comment.