---
title: messageport
slug: develop/messageport
docTags: 
createdAt: 2025-03-26T14:45:17.615Z
---

The *messageport* JavaScript object enables passing a JavaScript object as an associative array from JavaScript to BrightScript. This communication channel 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 is the preferred way for JavaScript content to communicate with its parent application.

:::hint{type="warning"}
Note that the `brightsign_js_objects_enabled` flag must be set to `true` in the [roHtmlWidget](docId\:xljq5_YhDmA2SSmpZimtK)  config.
:::

**messageport IDL**

```javascript
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. 

```javascript
var bsMessage = new MESSAGE_PORT();
```

## MessagePort

**PostBsMessage ()**

```javascript
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:

```javascript
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.

```javascript
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:

```javascript
var MESSAGE_PORT_CLASS = require("@brightsign/messageport");
var bsMessage = new MESSAGE_PORT_CLASS();
 
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:

```javascript
nodejs.PostJSMessage({ Param1: "Data1", Param2: "Data2", Param3: "Data3" })
```

:::hint{type="info"}
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.  
:::

