# Native messaging host communication

**URL:** <https://discourse.mozilla.org/t/native-messaging-host-communication/86534>\
**Category:** Development\
**Created:** [September 29, 2021, 1:58pm UTC](https://discourse.mozilla.org/t/native-messaging-host-communication/86534 "2021-09-29T13:58:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BumerangArt](https://avatars.discourse-cdn.com/v4/letter/b/aca169/32.png) [@BumerangArt](https://discourse.mozilla.org/u/BumerangArt)\
**Post date:** [September 29, 2021, 1:58pm UTC](https://discourse.mozilla.org/t/native-messaging-host-communication/86534/1 "2021-09-29T13:58:37Z")

</div>

Hello!  
We have a Firefox extension which communicates to a native messaging host declared in its manifest. We’ve noticed one weird moment about how the browser sends messages to the native host. For some reason, it spawns a new process for each new message. Is that expected behavior?  
The flow looks something like this on the extension side:

- background script initiates port by calling “browser.runtime.connectNative”
- native messaging host process starts up
- and then the extension uses “browser.runtime.sendNativeMessage” to send messages. Each of these calls result in a new native host process (with a new uid, this is occurring on macOS).

Is this supposed to happen or it it possible to send messages to the existing instance on native messaging host?  
This is mainly a concern for optimization. not to start all the routines on the native messaging host side.  
Thanks!

---

<div class="post-metadata">

**Author:** ![freaktechnik](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/freaktechnik/32/37766_2.png) [@freaktechnik](https://discourse.mozilla.org/u/freaktechnik)\
**Post date:** [September 29, 2021, 3:53pm UTC](https://discourse.mozilla.org/t/native-messaging-host-communication/86534/2 "2021-09-29T15:53:51Z")

</div>

If you use `runtime.connectNative` you would use the [`Port`](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/Port) it returns to then send messages to the instance that started. `runtime.sendNativeMessage` is essentially a shorthand for doing `connectNative` and then sending a single message.

---

<div class="post-metadata">

**Author:** ![BumerangArt](https://avatars.discourse-cdn.com/v4/letter/b/aca169/32.png) [@BumerangArt](https://discourse.mozilla.org/u/BumerangArt)\
**Post date:** [September 30, 2021, 8:26pm UTC](https://discourse.mozilla.org/t/native-messaging-host-communication/86534/3 "2021-09-30T20:26:09Z")

</div>

Hey, @freaktechnik. thanks for a prompt response! Indeed, this works as intended with this correction. It keeps the process alive and reuses the first instance spawned after establishing the connection.
