YSTP: Cloudflare Workers and Durable Objects Tutorial (Pt. 1)
This Christmas I was thinking on revisiting a Rust port of croc I wrote some years ago to teach myself the language but ended up doing something different.
I had just been introduced to Cloudflare’s Durable Objects and thought of porting the port and check how hard it would be to build the same thing on the Cloudflare stack.
It proved to be incredibly educational and quite fun so I ended up using it in an introductory talk for the stack.
What are we building?
YSTP (short for Your Super Transfer Program) is a file transfer tool that allows sending a file privately to a party directly without relying on port-forwarding to be set up.
It’s quick and easy to use and it all lives on Cloudflare’s edge, built with Workers and Durable Objects. No storage anywhere, just a transfer.
You can use the one I built (ystp.dbk.wtf) or you deploy your own. Let’s go.
Why are we building it?
Let’s have a look at what happens when we share files nowadays.
-
Alice wants to send a file to Bob.
-
Alice uploads the file somewhere accessible by Bob
-
Alice waits for the upload to finish…
-
Alice shares the link to the file with Bob
-
Bob waits for the download to finish…
-
Profit!
-
Profit?
OK, what’s going on?
I’m not going to explain how the internet works here but basically it is not straightforward for any device to connect directly to another device not public on the Internet (think Alice’s iPad to Bob’s computer). That normally requires extra setup on both ends, which we don’t want to do if we’re sending a file to a friend.
So, in the previous example Alice does an entire transfer to a publicly accessible server (like Doogle Grive) and stores the file there. Only then, can she share access to the file so Bob can do a second transfer from the server to his device.
Well, that’s one solution but we don’t want to store anything in between; we just want to transfer to the end device. So how shall we do it?
Game plan
We’ll build a publicly accessible relay, where both clients can establish a WebSocket connection and communicate between each other directly, the relay acting as a bridge.
So our protocol will follow these steps:
- Sender establishes WebSocket connection
- Relay sends the session code
- Sender shares file metadata
- Receiver connects with code
- Relay sends file metadata
- Receiver accepts transfer
- Relay tells sender to start transfer
- Transfer happens
- Transfer completes
- Profit
Hold on… what about the Cloudflare part?
Well spotted, astute reader. As you might be expecting, we won’t require an actual server to build our relay. Instead we’ll build it on top of Durable Objects which, contrary to standard serverless technologies, allows us to have stateful code and even hibernate WebSocket connections, making it a great fit for our use case (hibernating is good because if we were to be charged for every second our relay had active connections, we wouldn’t get very far with my budget).
Of course, Cloudflare will provide us with an endpoint to our Worker and Durable Object that will be publicly accessible, so it already covers all our requirements!
Building the thing
I’ve already dumped enough text here, so check out YSTP: Cloudflare Workers and Durable Objects tutorial (Pt. 2) .