Durable Objects · Exercise 04

Talk in real time

Connect clients to a room over WebSockets, broadcast every message to everyone in it, and let the room sleep between messages without dropping anyone.

Talk in real time

Your room stores messages, but nobody finds out about a new one unless they ask. Real-time apps keep a connection open so the server can push updates the moment they happen.

Because every client asking for the same room reaches the same Durable Object, the room can hold all of their connections and send each new message to everyone.

What you’ll build

WebSocket support for Room: clients connect to /room?name=lobby, and every message, sent over a WebSocket or posted over HTTP, is stored and pushed to everyone in the room. Between messages, the room can sleep without dropping anyone.

Prerequisites

  • You’ve completed Store data with SQL and still have that Worker on disk.
  • Node.js 22 or newer, for a quick WebSocket test script.