Developing Real-time Games with Cloudflare Durable Objects and Websockets

(image from π“΄π“˜π“‘π“š 𝕝𝔸𝕀)

What would a scalable serverless-style game server look like?

I would expect it to:

Cloudflare Workers and Durable Objects provide these capabilities.

Next up is a quick introduction to Durable Objects, as well as an introduction to the open-source Planning Poker app build on these technologies.

What are Cloudflare Durable Objects?

In a traditional cloud-based app, you'd separate compute and storage. You'd run a stateless serverless function, or a long-lived container, and then read/write state data in a central database on each request.

These Durable Objects are a way to bring together distributed data (regardless of where on the network edge this is happening) into one place where it can be viewed or updated in real-time, as well as persisted. And it's easy to have a Durable Object represent a game instance.

You also never need to manage the lifecycle of the Durable Object directly. You just try to communicate with a stub (passing in a unique ID) and it will instantiate itself, or if already running will accept traffic, or if no longer receiving any traffic it will spin down.

Building a Planning Poker Web App on Cloudflare

My goals were to build a real-time multiplayer application using websockets, as well as learn about the Cloudflare platform which seemed well suited for this type of application.

I chose to create an open-source planning poker app. Each game consists of a feature/story that needed estimating as well as a team of players participating in the real-time voting. When all votes are in, the game ends and the results are revealed.

The site lives at https://planningpoker.games and the source code is at github.com/briangershon/planning-poker.

Screenshot of a Planning Poker game in progress where one player had voted their estimated t-shirt size

Some more details:

Some Durable Object learnings along the way:

In conclusion

This was my first time using the Cloudflare platform, and I really enjoyed it. It has the features many apps need, and Durable Objects are an innovative and useful abstraction.