We have quite a history with real-time technologies here at CodePen. We’ve had all sorts of things in production from hand-rolled and self-hosted ideas, to services that don’t even exist anymore, to well-known products like Google Firebase’s Realtime Database. Sometimes our real-time needs are fairly complex, like our Collab Mode PRO feature that allows people to code within the same code editors at the same time, which is capable of conflict resolution and that kind of exotic science. Sometimes our real-time needs are more basic. Our real-time Live View is closer to the basic side. It has just a few jobs:
That this is mostly single-directional real-time-ness. The only thing Live View needs to do is authenticate to ensure the user is PRO (and looking at a Pen they own), but there are otherwise no events it needs to send back.
We have been keeping our eye on Cloudflare’s Durable Objects technology to use for future real-time needs of CodePen. We’re already heavy users of Cloudflare Workers, K/V storage, and HTMLRewriter — and Durable Objects fit into that same ballpark. The fairly basic nature of Live View made it a perfect testing ground to try out Durable Objects. Nothing like getting a new technology to production to really kick the tires! It’s not that Durable Objects isn’t capable of complex situations (in fact, it’s quite good at it), we just wanted to keep complexity low for our first rodeo.
The one-nickel version of how Live View Works:
If a Pen is owned by a PRO user, it’ll kick off a connection to a Cloudflare Worker which establishes a connection to a Durable Object. Then if Live View is opened, that web page does the same thing, ultimately connecting to the same Durable Object. Now messages can be passed from the Pen Editor to Live View (“Hey! A new preview is ready!”).
Live View was using Firebase Realtime Database before and now we’ve converted it to using Durable Objects. Here’s why we started down this road:
What did we find? Drumroll, I suppose…
So far so good! Over the course of a few days, we slowly rolled it out to 100% of PRO users and experienced no issues.
The post Real-Time Live View Now Powered By Cloudflare Durable Objects appeared first on CodePen Blog.