Course lesson 41 · Advanced Durable Objects

Setting up Websockets

One study unit · Read, recall, then practice · Original video 00:08:50

This lesson replaces the tracker’s temporary JSON response with a WebSocket handshake. It introduces the object’s message, close, and error handlers, then explains how one connection can receive a response or how all connected clients can receive a broadcast. The surrounding Hono route forwards the incoming request to the selected account object.

Primary source: course video 41 · Supplied English subtitles · 00:08:50.

Understand the idea

What the course does

  1. Review message, close, and error handlers on the Durable Object class.
  2. Explore echoing a received message and broadcasting to all registered sockets.
  3. Create a WebSocket pair inside fetch and accept the server endpoint through the object context.
  4. Return the client endpoint with the protocol upgrade response.
  5. Adapt the Hono socket route to identify the account from a request header and proxy to its object.

Watch for

Recall before revealing

Which end of a WebSocketPair remains associated with the Durable Object?

Reveal the explanation

The server end is accepted by the object; the client end is returned to establish the browser’s connection.

Try it

Trace an incoming message through an echo implementation and then through a broadcast implementation.

Check your result

You distinguish sending to the originating socket from iterating over every accepted socket.

Practice prompts and answer checks are added teaching material. Answer from memory first, then compare and explain any difference.

Rewatch only what you need

Open this lesson in your original course library and seek to the times below. Videos are not hosted on this site.

Companion primary documentation: Durable Objects WebSockets. For recorded API names, commands, limits, and prices, check the version you use.

← Previous   Next →