<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jeffrey Sanchez</title>
    <description>The latest articles on DEV Community by Jeffrey Sanchez (@jeffreysanchez_adm).</description>
    <link>https://dev.to/jeffreysanchez_adm</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4264706%2F939d3770-8694-41ad-99b2-f31c48110641.png</url>
      <title>DEV Community: Jeffrey Sanchez</title>
      <link>https://dev.to/jeffreysanchez_adm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jeffreysanchez_adm"/>
    <language>en</language>
    <item>
      <title>How a Browser Extension Hands a Link to a Desktop App: Native Messaging Lessons</title>
      <dc:creator>Jeffrey Sanchez</dc:creator>
      <pubDate>Wed, 25 Nov 2026 12:21:27 +0000</pubDate>
      <link>https://dev.to/jeffreysanchez_adm/how-a-browser-extension-hands-a-link-to-a-desktop-app-native-messaging-lessons-60c</link>
      <guid>https://dev.to/jeffreysanchez_adm/how-a-browser-extension-hands-a-link-to-a-desktop-app-native-messaging-lessons-60c</guid>
      <description>&lt;p&gt;Browser extensions live in a sandbox. They can read the page you're on (if you let them), add items to the right-click menu and talk to web servers, but they can't start a program on your computer. So how does a "Download with…" menu item end up launching a desktop download manager?&lt;/p&gt;

&lt;p&gt;The answer is &lt;strong&gt;native messaging&lt;/strong&gt;, and we learned most of its sharp edges while building the browser add-on for &lt;a href="https://advanceddownloadmanager.net/" rel="noopener noreferrer"&gt;Advanced Download Manager&lt;/a&gt;, a free Windows download manager. This post walks through the moving parts and the mistakes worth avoiding. No product code here, just the concepts and a generic config example.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three pieces
&lt;/h2&gt;

&lt;p&gt;Native messaging has three participants:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The extension&lt;/strong&gt;, which calls &lt;code&gt;chrome.runtime.connectNative()&lt;/code&gt; or &lt;code&gt;chrome.runtime.sendNativeMessage()&lt;/code&gt; with a host name.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A host manifest&lt;/strong&gt;, a small JSON file that tells the browser which executable that host name maps to and which extensions may use it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The host executable&lt;/strong&gt;, a program the browser starts, which reads messages from standard input and writes replies to standard output.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The browser sits in the middle. It starts the host, pipes messages both ways, and kills the host when the port closes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The host manifest
&lt;/h2&gt;

&lt;p&gt;A host manifest looks like this (generic names):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"com.example.myhost"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Example native host"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"path"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"C:\\Program Files\\Example\\host.exe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"stdio"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"allowed_origins"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;"chrome-extension://abcdefghijklmnopabcdefghijklmnop/"&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On Windows, the browser finds this file through the registry: a key named after the host under &lt;code&gt;Software\Google\Chrome\NativeMessagingHosts&lt;/code&gt; (or the Edge equivalent), whose default value is the path to the JSON file.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;allowed_origins&lt;/code&gt; is the security boundary. Only the extension IDs listed there can talk to your host. If you publish the same add-on under more than one ID (say, an old store listing and a new one, or Chrome plus Edge), every ID has to be in the list, or users of the missing one see "host not found" even though everything is installed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson 1: check both registry hives
&lt;/h2&gt;

&lt;p&gt;The registry key can live under &lt;code&gt;HKEY_CURRENT_USER&lt;/code&gt; or &lt;code&gt;HKEY_LOCAL_MACHINE&lt;/code&gt;. A per-machine installer usually writes to HKLM, and on 64-bit Windows a 32-bit installer lands under &lt;code&gt;WOW6432Node&lt;/code&gt;. When you debug "it says not installed", look in all of them before assuming the installer failed. We lost an afternoon checking only HKCU.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson 2: the wire format is length-prefixed JSON
&lt;/h2&gt;

&lt;p&gt;Each message is a 32-bit length in native byte order followed by that many bytes of UTF-8 JSON. Two consequences:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Never print anything else to stdout from the host. A stray debug &lt;code&gt;print&lt;/code&gt; corrupts the stream and the browser drops the connection.&lt;/li&gt;
&lt;li&gt;Messages from the host to the browser are capped at 1 MB; from the browser to the host, the cap is 64 MiB. For a download link that's plenty, but don't try to push file contents through the pipe.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Lesson 3: detecting whether the desktop app is installed
&lt;/h2&gt;

&lt;p&gt;The extension can't see the file system, so it has to ask the browser. If you call &lt;code&gt;connectNative()&lt;/code&gt; and the host isn't registered, the port disconnects almost immediately and &lt;code&gt;chrome.runtime.lastError&lt;/code&gt; says the host was not found. If nothing happens within a short window, the host exists.&lt;/p&gt;

&lt;p&gt;You can use that to show "Install the desktop app first" in the popup without sending a real message. It's worth keeping the check side-effect free: if your host launches the main app on any message, a status check that sends one will pop the app open every time the popup is opened.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson 4: hosts are short-lived; the real app usually isn't
&lt;/h2&gt;

&lt;p&gt;The browser starts a fresh host process per connection and kills it when the port closes. A download manager, though, is a long-running app with its own window. The common pattern is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the host is a tiny executable that receives the link,&lt;/li&gt;
&lt;li&gt;if the main app is already running, the host forwards the link to it over a local channel (a loopback socket or named pipe);&lt;/li&gt;
&lt;li&gt;if not, the host starts the app and hands the link over.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Watch for a race here. If a user selects twenty links and sends them at once, twenty hosts start at nearly the same moment, each sees "app not running" and each tries to start it. Queue the hand-offs in the extension, or make the app's single-instance check robust, or both.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson 5: be careful with what you pass along
&lt;/h2&gt;

&lt;p&gt;Some downloads only work with the user's session, for example a file behind a hosting control panel login. The extension can read cookies for that site (with the &lt;code&gt;cookies&lt;/code&gt; permission and host access the user explicitly grants) and pass them to the app.&lt;/p&gt;

&lt;p&gt;Two rules we follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Never put cookies or tokens on a command line.&lt;/strong&gt; Other processes can read command lines. Pass them over the local channel, or in a temporary file only the user can read, and delete it after use.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ask for the permission at the moment it's needed&lt;/strong&gt;, as an optional permission, rather than at install time. Users are rightly wary of an add-on that wants every site's cookies on day one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Lesson 6: store reviews read your manifest closely
&lt;/h2&gt;

&lt;p&gt;Chrome Web Store and Edge Add-ons both ask you to justify every permission. &lt;code&gt;nativeMessaging&lt;/code&gt; is uncontroversial when the listing clearly says a desktop app is required; &lt;code&gt;cookies&lt;/code&gt; and &lt;code&gt;&amp;lt;all_urls&amp;gt;&lt;/code&gt; need a concrete reason. Writing the justification before you write the feature is a good test of whether you really need the permission.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;Native messaging is a small API with a few unforgiving details: a JSON manifest in the right registry hive, a strict length-prefixed stdout, an allow-list of extension IDs, and hosts that come and go. Get those right and an extension can hand work to a desktop app cleanly and locally, with no server in between.&lt;/p&gt;

&lt;p&gt;If you want to see the end result, the &lt;a href="https://advanceddownloadmanager.net/" rel="noopener noreferrer"&gt;ADM add-on for Chrome and Edge&lt;/a&gt; adds "Download with ADM" to the right-click menu and passes links to the Windows app this way. And Chrome's own &lt;a href="https://developer.chrome.com/docs/extensions/develop/concepts/native-messaging" rel="noopener noreferrer"&gt;native messaging documentation&lt;/a&gt; is the reference for every detail above.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>chrome</category>
      <category>windows</category>
    </item>
  </channel>
</rss>
