# NativeMessaging addon with error in console: "Attempt to postMessage on disconnected port"

**URL:** <https://discourse.mozilla.org/t/nativemessaging-addon-with-error-in-console-attempt-to-postmessage-on-disconnected-port/74740>\
**Category:** Development\
**Created:** [January 31, 2021, 10:39am UTC](https://discourse.mozilla.org/t/nativemessaging-addon-with-error-in-console-attempt-to-postmessage-on-disconnected-port/74740 "2021-01-31T10:39:24Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![daniell](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/daniell/32/44425_2.png) [@daniell](https://discourse.mozilla.org/u/daniell)\
**Post date:** [February 2, 2021, 7:44am UTC](https://discourse.mozilla.org/t/nativemessaging-addon-with-error-in-console-attempt-to-postmessage-on-disconnected-port/74740/23 "2021-02-02T07:44:40Z")

</div>

BTW I can do it: Python script gets the ping, repasses to the Bash script, and pong: the bash script repasses to the Python script that repasses to the addon.

So, the browser/addon and the Bash script are still the transmitters/receivers, and the Python script is the medium.

---

<div class="post-metadata">

**Author:** ![caitlin](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/caitlin/32/6091_2.png) [@caitlin](https://discourse.mozilla.org/u/caitlin)\
**Post date:** [February 2, 2021, 5:30pm UTC](https://discourse.mozilla.org/t/nativemessaging-addon-with-error-in-console-attempt-to-postmessage-on-disconnected-port/74740/25 "2021-02-02T17:30:16Z")

</div>

Hi folks, this is a gentle reminder that we expect respectful interaction in this space as part of our [Community Participation Guidelines](https://www.mozilla.org/en-US/about/governance/policies/participation/). Personal attacks are not welcome.

---

<div class="post-metadata">

**Author:** ![rob](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/rob/32/30727_2.png) [@rob](https://discourse.mozilla.org/u/rob)\
**Post date:** [February 2, 2021, 9:10pm UTC](https://discourse.mozilla.org/t/nativemessaging-addon-with-error-in-console-attempt-to-postmessage-on-disconnected-port/74740/26 "2021-02-02T21:10:09Z")

</div>

> [@daniell](#):
>
> > Have you read the documentation or tried to understand the example before modifying it?
> 
> So, I need to be an professional programmer to build this?

Not necessarily. An understanding of the API is necessary though. From your post, it wasn’t clear whether you’ve understood the example that you’re modifying, because the logic of your “native messaging host” (the shell script) was far from a correct implementation of the documented “stdio” protocol of the nativeMessaging API.

> [@daniell](#):
>
> Instead:  
> See [https://stackoverflow.com/questions/24764657/how-do-i-use-a-shell-script-as-chrome-native-messaging-host-application](https://stackoverflow.com/questions/24764657/how-do-i-use-a-shell-script-as-chrome-native-messaging-host-application)
> 
> Remember what yourself have posted?
> 
> > Here is a minimal example that assumes that the input ends with a `}` , reads it (without processing) and replies with a result. Although this demo works,
> 
> It was very helpful for me, and I thank you for that!

Note that even in that post that I wrote 6+ years ago (and is still valid today), the first line of the answer is “I suggest to not use (bash) shell scripts as a native messaging host, because bash is too limited to be useful.”. The shown example has several caveats (read the inline code comments!) to show under which limited circumstances it works.

> [@daniell](#):
>
> > bash is not a good choice for developing a native messaging host, because null bytes are not consistently preserved. For example, when stdin is `\0x06\0x00\x00\x00"ping"` , your bit of code with `while read line` will contain `\0x06"ping"`
> 
> Not even that my bash script is doing, simply because the addon isn’t even sending the message, I think. So, if bash “is so horrible” but at least can read `\0x06"ping"` , why its not doing it?

You were using `while read line; do if ["${line}" = "ping"] ; then`. This has two issues: (1) `read` reads line by line. The native messaging protocol does not use line breaks, but `<integer of message length><message here>`. The `read` call never assigns the input to `line`. (2) even if the loop body was entered, then your if-condition would be false.

> [@daniell](#):
>
> @rob  
> BTW there is an unanswered question using your bash implementation:
> 
> [javascript - How to parse JSON from stdin at Chrome Native Messaging host? - Stack Overflow](https://stackoverflow.com/questions/48413999/how-to-parse-json-from-stdin-at-chrome-native-messaging-host)
> 
> This could be very relevant to the purpose of the script you said that works and how I want to use it.

Please read [my Stack Overflow answer](https://stackoverflow.com/questions/24764657/how-do-i-use-a-shell-script-as-chrome-native-messaging-host-application/24777120#24777120) carefully. The demo script “works”, but only under very limited circumstances (stdin discarded, message in a specific format, output not using multibyte characters). The majority of the answer is filled with warnings and explanations on why Bash is not a good choice as the language to write a native messaging host.

> [@daniell](#):
>
> BTW I can do it: Python script gets the ping, repasses to the Bash script, and pong: the bash script repasses to the Python script that repasses to the addon.
> 
> So, the browser/addon and the Bash script are still the transmitters/receivers, and the Python script is the medium.

The start of your answer is a better use of bash: as an intermediary. It’s not uncommon to have platform-specific shell scripts to wrap the actual program. But your insistence on using Bash for primary control flow is strange. It makes the programming task more difficult, and the resulting program performs worse than a pure-Python script.

If you really want to end up using a Bash script, then I recommend to still start with a Python script (or any other language/program) that behaves as desired. Once it works, commit the changes, and then create your desired new Bash script to reproduce the logic. If the Bash script doesn’t work as expected, then you can compare its behavior with the other program to see if it’s an issue with your Bash script, or with your extension.

[Previous page](https://discourse.mozilla.org/t/nativemessaging-addon-with-error-in-console-attempt-to-postmessage-on-disconnected-port/74740.md?page=1)
