Goal

In this POC, I set out to see if it was possible to create a “pay as you watch” system with the Lightning Network. This is a system whereby we pay in small increments (e.g. every 5 seconds, … ) instead of a monthly subscription or paying for the entire movie.

Setup

To accomplish this, I’ve set up two Lightning nodes with a channel between them. Both nodes run bitcoind (testnet) and lightningd.

lightning-node-1 (payee) also runs lightning-charge

lightning-node-2 (payer) also runs LightningPay

LightningPay is a very simple Express app that I created that communicates with Lightningd via JSON-RPC. This was required to do automatic payments.

DASH

Dynamic Adaptive Streaming over HTTP (DASH), also known as MPEG-DASH, is an adaptive bitrate streaming technique that enables high quality streaming of media content over the Internet delivered from conventional HTTP web servers. An mpd file is used to describe, amongst other things, the URL structure of the required media files.

<SegmentTemplate duration=”120" timescale=”30" media=”$RepresentationID$/$RepresentationID$_$Number$.m4v” startNumber=”1" initialization=”$RepresentationID$/$RepresentationID$_0.m4v”/>

The reference client (dash.js) is responsible for downloading all of the media files that will eventually be played backed by the browser. In our case, we’re proxying all requests made to https://dash.akamaized.net via the MicroPay Proxy. I modified dash.js so that all requests include HTTP header ‘CustomerId’ with a fixed value of 1.

MicroPayProxy

This proxy is mainly responsible for guarding unpaid video files, and serving payment requests.

If a request comes in for an m4v (video) file that has not yet been paid for, the proxy will return the HTTP status code 402 (Payment Required), and the Payment-Request header will be set.

I’ve modified the Dash.js library to watch out for HTTP 402 responses and automatically act on incoming payment requests. This is ofcourse a naive and unsecure way of paying a Lightning invoice. A real app using this sort of system would be using a secure Lightning web wallet.

Once an invoice has been paid, the request for the media file is simply retried by Dash.js and proxied through to the proxy target.

Closing Thoughts

This was an interesting exercise, and I’m happy to say that I learned a lot about the Lightning Network! ⚡️ Who knows, “Pay As You Watch” may one day become an interesting alternative to more traditional subscription models.

Sources