Decentralized Applications via the Web Push API
Tags: decentralization, web3, pushapi
Published: 2022-11-03 01:44:36

---
title: Decentralized Applications via the Web Push API
published: true
date: 2022-11-02 21:05:00 UTC
tags: Decentralization,Web3,PushAPI
canonical_url: https://kevincox.ca/2022/11/02/decentralized-via-webpush/
---

<main style="--width:min(36em,100%);display:grid;grid-template-columns:1fr var(--width) 1fr;overflow:auto;padding:0;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><h1 id="web-push-api" style="box-sizing:border-box;grid-column:2;margin:.4em 1rem .8em"><a href="https://kevincox.ca/2022/11/02/decentralized-via-webpush/#web-push-api" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Web Push API</a></h1>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">Push notifications are often used for evil, but I think we can all agree that they can be invaluable when used correctly.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">I’ve been a fan of the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Push_API" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">Web Push API</a> for a long time. It provides a way to send push notifications to users that is browser-independent (<a href="https://web-push-book.gauntface.com/non-standards-browsers/" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">despite Google’s efforts to require a registered <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap">gcm_sender_id</code></a>). Not only that but it is end-to-end encrypted!</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">The “backend API” is <a href="https://datatracker.ietf.org/doc/html/draft-ietf-webpush-protocol" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">standardized too</a> and is a generic protocol that can be used for non-web applications. I think a lot of Google Play Services FCM alternatives should look into this instead of their home-rolled solutions that are usually not half as well thought-out.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">In fact if you have a public IP (or a trusted proxy) you can even run an HTTP Web Push backend locally. So your device could be its own Push Service.</p>
<h1 id="filepush" style="box-sizing:border-box;grid-column:2;margin:.4em 1rem .8em"><a href="https://kevincox.ca/2022/11/02/decentralized-via-webpush/#filepush" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">FilePush</a></h1>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">Recently a thought came through my mind. While the Web Push API is designed to send push messages from server-to-client, why can’t we send them client-to-client? This would be a way to have interactive, communicating web apps without a backend service. Did someone say Web3? (Err, maybe it is best to avoid that term.)</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">With the technology in my head I just needed an app to build, a classic solution looking for a problem. Luckily I had just the problem!</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">Sending files between devices can be a real pain. You can send it to yourself via email, but that is slow, and you need to clean up afterwards to avoid wasting tons of your email space. <a href="https://support.apple.com/en-ca/HT204144" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">AirDrop</a> is quite nice for transfers between Apple hardware, but I want something hardware independent and cross-platform. I often end up using services such as <a href="https://snapdrop.net" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">Snapdrop</a>, <a href="https://www.sharedrop.io/" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">ShareDrop</a> and <a href="https://file.pizza/" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">FilePizza</a> to transfer files device-to-device. However, none of these were a perfect fit for my need. The first two do discovery by public IP, so you can only share if you are on the same network. They also require opening the website on both devices, which is a minor inconvenience. FilePizza lacks end-to-end encryption and has a slow “Processing” step before transferring the file. (It is turning the file into a Torrent which is great for sharing to many people, but that is a different problem than the one I am targeting here.)</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">I figured that device-to-device file transfer would be an easy proof of concept. The idea is simple.</p>
<ol style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">
<li style="box-sizing:border-box;margin:0">Out-of-band contact setup via URL sharing (link or QR code).</li>
<li style="box-sizing:border-box;margin:0">Select contact.</li>
<li style="box-sizing:border-box;margin:0">Select file.</li>
<li style="box-sizing:border-box;margin:0">Receiver gets a push notification.</li>
<li style="box-sizing:border-box;margin:0">Click notification, receive file.</li>
</ol>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">The contact setup makes it a bit more work than the other solutions for the first transfer, but my target use case is repeated transfers between my own devices. So one-time contact setup pays off quickly.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">I’ve thrown this together and it is live at <a href="https://filepush.kevincox.ca" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">filepush.kevincox.ca</a>. It seems to work fairly well. <a href="https://gitlab.com/kevincox/filepush" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">The code is available as well</a>.</p>
<h2 id="web-push" style="box-sizing:border-box;grid-column:2;margin:.4em 1rem .8em"><a href="https://kevincox.ca/2022/11/02/decentralized-via-webpush/#web-push" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Web Push</a></h2>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">The first step is the Web Push API. This is used for <strong style="box-sizing:border-box;margin:0">signalling</strong> a WebRTC transfer (where the actual transfer happens).</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">Web Push requires a bit of setup. The receiver generates a <a href="https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray"><code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap">PushSubscription</code></a> which is then sent to the sender. The sender then uses this information to send push messages. It looks something like this:</p>
<div style="background:#222;border-bottom:1px solid #444;border-top:1px solid #444;box-sizing:border-box;display:grid;grid-column:2;grid-column:1/4;grid-template-columns:1fr var(--width) 1fr;margin:0 0 .8em;overflow-x:auto;padding:0"><pre style="box-sizing:border-box;grid-column:2;margin:.4em 1rem"><code style="background:none!important;box-sizing:border-box;color:#fff;margin:0;white-space:pre-wrap;white-space:pre"><span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">endpoint</span><span style="box-sizing:border-box;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">https://push.example/secretsubscriptionid</span>
<span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">keys</span><span style="box-sizing:border-box;margin:0;padding:0">:</span>
  <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">p256dh</span><span style="box-sizing:border-box;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">secrete2ekey</span>
  <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">auth</span><span style="box-sizing:border-box;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">secretauthkey</span>
<span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">expirationTime</span><span style="box-sizing:border-box;margin:0;padding:0">:</span> <span style="box-sizing:border-box;margin:0;padding:0">null</span>
</code></pre></div>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em"><code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap">endpoint</code> is simply the place to send the notification, the push service should route messages sent to this endpoint to the user’s browser. This is generally a server operated by your browser vendor. <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap"><span style="box-sizing:border-box;margin:0;padding:0">keys</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">p256dh</span></code> is the receiver’s public key. This is what provides end-to-end confidentiality for push messages. <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap"><span style="box-sizing:border-box;margin:0;padding:0">keys</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">auth</span></code> is a shared authentication key, this ensures that the push service can’t inject messages. If the subscription has a set expiry it would be provided in <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap">expirationTime</code>. Firefox and Chromium both appear to have indefinite subscriptions, so I have been ignoring this value for now.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">This subscription is simply embedded in the “invite” URL. Once the other device gets this info it can send a push message with its own information to set up bidirectional communication.</p>
<h3 id="sender-authentication" style="box-sizing:border-box;grid-column:2;margin:.4em 1rem .8em"><a href="https://kevincox.ca/2022/11/02/decentralized-via-webpush/#sender-authentication" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Sender Authentication</a></h3>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">There is one last wrinkle. Push service operators want to be able to identify senders to resolve issues. This is done via <a href="https://datatracker.ietf.org/doc/html/rfc8292" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">Voluntary Application Server Identification (VAPID) for Web Push</a>. This is a keypair generated by “the server” and used to authenticate pushes. Presumably if your server was buggy or abusive they could attempt to contact you (via the email address in the VAPID signature) or block your key. Then in order to resume notifications you would need to generate a new key and recreate all of your subscriptions.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">This creates a wrinkle in our plan since we aren’t supposed to share this key publicly …but the whole app is public. At first, I thought I could generate a keypair per sender, but this doesn’t work because the Push API only allows a single subscription per device, so all senders need to use the same VAPID key for a particular receiver. This effectively means that the key needs to be shared between devices.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">For now, I have just hardcoded a keypair into the code. Maybe in the future each device could generate a keypair to be used for incoming notifications, then it could share that keypair with senders as part of the setup. However, I’m slightly worried that 100s of keypairs for a single web origin would set off flags for popular Push Services anyways, so maybe it is best to stick with one for now. I don’t think there is much potential for abuse anyways since the subscription is liked to the application’s origin, so all abusers could do is alert themselves.</p>
<h3 id="client-side-push" style="box-sizing:border-box;grid-column:2;margin:.4em 1rem .8em"><a href="https://kevincox.ca/2022/11/02/decentralized-via-webpush/#client-side-push" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Client-Side Push</a></h3>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">Next step was sending a push notification. This was relative simple.</p>
<div style="background:#222;border-bottom:1px solid #444;border-top:1px solid #444;box-sizing:border-box;display:grid;grid-column:2;grid-column:1/4;grid-template-columns:1fr var(--width) 1fr;margin:0 0 .8em;overflow-x:auto;padding:0"><pre style="box-sizing:border-box;grid-column:2;margin:.4em 1rem"><code style="background:none!important;box-sizing:border-box;color:#fff;margin:0;white-space:pre-wrap;white-space:pre"><span style="box-sizing:border-box;margin:0;padding:0">fetch</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">subscription</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">endpoint</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
	<span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">method</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">"</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">POST</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">"</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span>
	<span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">headers</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
		<span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">urgency</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">"</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">high</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">"</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span>
		<span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">authorization</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">`WebPush </span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">${</span><span style="box-sizing:border-box;margin:0;padding:0">vapidToken</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">}</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">`</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span>
		<span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">ttl</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">"</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">300</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">"</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span>
	<span style="box-sizing:border-box;color:#999;margin:0;padding:0">},</span>
	<span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">body</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;margin:0;padding:0">body</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">cyphertext</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span>
<span style="box-sizing:border-box;color:#999;margin:0;padding:0">});</span>
</code></pre></div>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">The <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap">vapidToken</code> is simply a <a href="https://www.rfc-editor.org/rfc/rfc7519" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">JWT</a> signed with our “server key” as discussed earlier.</p>
<h3 id="cors" style="box-sizing:border-box;grid-column:2;margin:.4em 1rem .8em"><a href="https://kevincox.ca/2022/11/02/decentralized-via-webpush/#cors" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">CORS</a></h3>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">But oh no, our request is rejected! This is due to a legacy kludge called <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">Cross-Origin Resource Sharing (CORS)</a>. This is an opt-out protection system that blocks certain requests (basically anything that wasn’t already possible when the system was introduced). Since our request has headers that aren’t on the CORS safelist we need the server to explicitly approve it. Unfortunately neither Firefox nor Chromium’s Push Services support this opt-in.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">But all is not lost. CORS was primarily to avoid issues with automatically-sent information such as <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap">Cookie</code>s. Since our request doesn’t require cookies CORS is both useless and easy to bypass. We just need a CORS proxy. This is simply a service that authorizes cross-origin requests by doing the CORS dance but otherwise just proxies requests (hopefully stripping cookies). By ensuring cookies are not sent CORS is no longer needed.</p>
<aside style="background:#333;box-shadow:4px 4px 8px #000;box-sizing:border-box;color:#fff;grid-column:2;margin:.2em 1em .6em"><p style="box-sizing:border-box;margin:.4em">CORS does still have some purpose. As well as changing the origin a CORS proxy also ensures a “public” network perspective. This avoids websites from banging on your Wi-Fi router’s likely insecure web interface. …of course many routers can be hijacked with only CORS exempt requests, so this isn’t bulletproof protection anyways.</p></aside><p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">However, in addition to CORS the Firefox push service also blocks requests with an <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap">Origin</code> header. Most publicly available CORS proxies leave this header in place, so I wrote my own simple proxy that strips this header as well. That was enough to get push working on Firefox and Chromium. I don’t see any reason why other browsers wouldn’t work as well.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">Unfortunately this proxy does provide some server-side infrastructure and lowers our decentralization. However, it is incredibly simple and easy to replicate. It would be easy enough to provide an option for users to use a proxy of their choice to bring back full decentralization. Of course, it would be ideal if the browser’s Push Services were simply CORS enabled, maybe I’ll file some bugs with the browsers.</p>
<h3 id="encryption" style="box-sizing:border-box;grid-column:2;margin:.4em 1rem .8em"><a href="https://kevincox.ca/2022/11/02/decentralized-via-webpush/#encryption" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Encryption</a></h3>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">You may have noticed that the last request had no body. This is the simple form of push notification that is just a ping. It isn’t clear that there is much value here since all you can tell the user is “Something happened!”. I guess the assumption is that your app will sync with the server to get the details before showing a notification to the user.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">But we don’t have a server to sync with, so let’s add a body. Google has some <a href="https://github.com/web-push-libs/web-push" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">recommended libraries</a> for this however everything I found only supported <a href="https://nodejs.org" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">Node.js</a>. So I had to roll my own crypto!</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">After a lot of careful code and spec reading I have come up with what is, as far as I can tell, the first browser-based implementation of Web Push encryption. As scary as it is to roll your own crypto I am fairly confident it works correctly as the authenticated encryption would fail if I messed up the core protocol and I tried to avoid problems with edge cases. All of the hard decisions were made by the experts that designed the protocol, so I feel safe using it to transfer sensitive files. Of course review and feedback would be much appreciated.</p>
<h2 id="webrtc" style="box-sizing:border-box;grid-column:2;margin:.4em 1rem .8em"><a href="https://kevincox.ca/2022/11/02/decentralized-via-webpush/#webrtc" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">WebRTC</a></h2>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">The Push API only supports message payloads of up to 4078 bytes. While we can send “unlimited” messages I suspect that the Push Service operations wouldn’t be happy if we tried to transfer gigabytes of files. I also doubt that I would be writing home about the performance.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">Luckily we have <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">WebRTC</a>. WebRTC is an easy-to-use API for direct peer-to-peer encrypted connections (or optionally relayed connections) with end-to-end encryption. It supports low latency and most importantly for us high bandwidth.</p>
<h3 id="signalling" style="box-sizing:border-box;grid-column:2;margin:.4em 1rem .8em"><a href="https://kevincox.ca/2022/11/02/decentralized-via-webpush/#signalling" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Signalling</a></h3>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">WebRTC is a high-level API that manages most of the work for us. The biggest missing piece is connection setup. This is often called “signalling” in WebRTC, although we only need to do initial signalling. Once connected the browser will take care of the rest. Even this is mostly done for us, we just need to shuffle and “offer” from the initiator to the receiver and then shuffle an “answer” back. If all goes well that is enough for the two to establish a connection.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">Since we already have Web Push set up it is trivial to coordinate the session setup. It is a bit tricky to juggle the possibilities of the receiver having 0, 1 or multiple tabs open but with a bit of logic we can handle those cases and set up connections.</p>
<h3 id="transfer" style="box-sizing:border-box;grid-column:2;margin:.4em 1rem .8em"><a href="https://kevincox.ca/2022/11/02/decentralized-via-webpush/#transfer" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Transfer</a></h3>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">Next up is file transfer. This is fairly easy, just shove the bits into a pipe. WebRTC’s <a href="https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">RTCDataChannel</a> even differentiates between “strings” and “bytes” so we can abuse that to send our control data as JSON strings and file data as bytes to avoid needing to have extra framing or tagging there.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">The one complication is buffering. <a href="https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/send" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">RTCDataChannel.send()</a> is very unhelpful. The docs contain great quotes like:</p>
<blockquote style="box-sizing:border-box;font-size:1.2em;grid-column:2;margin:1.6em;position:relative">
<p style="box-sizing:border-box;font-style:italic;margin:0 1rem .8em;padding-top:0;text-align:center">NetworkError DOMException</p>
<p style="box-sizing:border-box;font-style:italic;margin:0 1rem;padding-bottom:0;text-align:center">Thrown when the specified data would need to be buffered, and there isn’t room for it in the buffer. In this scenario, the underlying transport is immediately closed.</p>
</blockquote>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">How big is the buffer? I can’t see any way to find out. But if you fill it you immediately have your connection closed. That sucks.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">To “solve” this issue I used the incredibly scientific approach of playing Numberwang with parameters until it worked. I ended up sending ¼ MiB chunks and waiting until the currently buffered amount was ¼ MiB or less. It seems like the buffer size is about 1 MiB. Not satisfying, but works in Firefox and Chromium. I didn’t bother writing code to re-establish the connection with a smaller chunk size and resume after hitting the limit yet. Maybe the API will improve at some point.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">The performance is decent, but not fantastic. However, I’m not sure that I can do much better with this many layers of abstraction between the network and the filesystem. Unless you are transferring very high quality movies you are unlikely to notice the slowness.</p>
<h3 id="download" style="box-sizing:border-box;grid-column:2;margin:.4em 1rem .8em"><a href="https://kevincox.ca/2022/11/02/decentralized-via-webpush/#download" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Download</a></h3>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">Another complication is downloading the file. It surprised me that the web doesn’t have any proper API for starting a download from JavaScript. The standard solution is:</p>
<div style="background:#222;border-bottom:1px solid #444;border-top:1px solid #444;box-sizing:border-box;display:grid;grid-column:2;grid-column:1/4;grid-template-columns:1fr var(--width) 1fr;margin:0 0 .8em;overflow-x:auto;padding:0"><pre style="box-sizing:border-box;grid-column:2;margin:.4em 1rem"><code style="background:none!important;box-sizing:border-box;color:#fff;margin:0;white-space:pre-wrap;white-space:pre"><span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">const</span> <span style="box-sizing:border-box;margin:0;padding:0">LINK</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">=</span> <span style="box-sizing:border-box;margin:0;padding:0">document</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">createElement</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">"</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">a</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">"</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">);</span>
<span style="box-sizing:border-box;margin:0;padding:0">LINK</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">style</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">display</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">=</span> <span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">"</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">none</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">"</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">;</span>
<span style="box-sizing:border-box;margin:0;padding:0">document</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">body</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">append</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">LINK</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">);</span>

<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">function</span> <span style="box-sizing:border-box;margin:0;padding:0">saveFile</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">name</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">string</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span> <span style="box-sizing:border-box;margin:0;padding:0">blob</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;margin:0;padding:0">Blob</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">)</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
	<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">var</span> <span style="box-sizing:border-box;margin:0;padding:0">url</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">=</span> <span style="box-sizing:border-box;margin:0;padding:0">URL</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">createObjectURL</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">blob</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">);</span>
	<span style="box-sizing:border-box;margin:0;padding:0">LINK</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">setAttribute</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">"</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">href</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">"</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span> <span style="box-sizing:border-box;margin:0;padding:0">url</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">);</span>
	<span style="box-sizing:border-box;margin:0;padding:0">LINK</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">setAttribute</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">"</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">download</span><span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">"</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span> <span style="box-sizing:border-box;margin:0;padding:0">name</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">);</span>
	<span style="box-sizing:border-box;margin:0;padding:0">LINK</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">click</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">();</span>
	<span style="box-sizing:border-box;margin:0;padding:0">URL</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">revokeObjectURL</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">url</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">);</span>
<span style="box-sizing:border-box;color:#999;margin:0;padding:0">}</span>
</code></pre></div>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">Yup, create a link and fake a click. It feels wrong but it works.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">However, there is a major issue with this. It doesn’t appear possible to start a streaming download. Ideally we could start streaming to disk as soon as the transfer starts (like downloading a regular file over HTTP) but there isn’t an API for that. I’ve found that you can use a <a href="https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray"><code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap">ReadableStream</code></a> then use that to create a <a href="https://developer.mozilla.org/en-US/docs/Web/API/Response" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray"><code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap">Response</code></a> which you can then convert to a <a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray"><code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap">Blob</code></a> using <a href="https://developer.mozilla.org/en-US/docs/Web/API/Response/blob" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray"><code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap"><span style="box-sizing:border-box;margin:0;padding:0">Response</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">blob</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">()</span></code></a>. However, <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap"><span style="box-sizing:border-box;margin:0;padding:0">Response</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">blob</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">()</span></code> doesn’t resolve until the entire response is downloaded. This seems to be required since <a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob/size" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray"><code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap"><span style="box-sizing:border-box;margin:0;padding:0">Blob</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">size</span></code></a> needs to be known to create the <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap">Blob</code> and <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap">ReadableStream</code> doesn’t know the size until the producer closes the controller. <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:.1em;white-space:pre-wrap"><span style="box-sizing:border-box;margin:0;padding:0">Response</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">.</span><span style="box-sizing:border-box;margin:0;padding:0">blob</span></code> seems to buffer the stream to disk then a new copy will be started when the file is “downloaded” outside of the browser. This means that the user effectively needs to wait for two copies of the file. It isn’t ideal but at least arbitrarily large files can be copied, and it is much cleaner (and probably much more efficient) than my previous approach that cached chunks to disk using <a href="https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">IndexedDB</a>.</p>
<h2 id="conclusion" style="box-sizing:border-box;grid-column:2;margin:.4em 1rem .8em"><a href="https://kevincox.ca/2022/11/02/decentralized-via-webpush/#conclusion" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Conclusion</a></h2>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">That’s basically it. I think it is pretty cool that we can have cross-platform, decentralized device-to-device communication without a server (excluding the proxy).</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">The app itself could use a little polish, but the transfer works well. I’ve proved out the concept and made myself a useful tool.</p></main>