Skip to content

Conversation

@harshil1712
Copy link
Contributor

Summary

Updates the WebSocket and WebSocket Hibernation examples.

@cloudflare-workers-and-pages
Copy link

Deploying cloudflare-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: f2b809e
Status: ✅  Deploy successful!
Preview URL: https://41100605.cloudflare-docs-7ou.pages.dev
Branch Preview URL: https://do-websocket-examples.cloudflare-docs-7ou.pages.dev

View logs

@github-actions github-actions bot added product:durable-objects Durable Objects: https://developers.cloudflare.com/workers/learning/using-durable-objects/ size/l labels Dec 3, 2024
@Oxyjun Oxyjun mentioned this pull request Aug 4, 2025
@Oxyjun
Copy link
Contributor

Oxyjun commented Aug 4, 2025

Closes #24141

@Oxyjun Oxyjun requested a review from mikenomitch as a code owner August 4, 2025 15:31
@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2025

This pull request requires reviews from CODEOWNERS as it changes files that match the following patterns:

Pattern Owners
/src/content/docs/durable-objects/ @elithrar, @vy-ton, @joshthoward, @oxyjun, @harshil1712, @mikenomitch, @cloudflare/pcx-technical-writing, @cloudflare/workers-runtime-1

Comment on lines +85 to +86
let attachment = ws.deserializeAttachment();
if (ws.deserializeAttachment()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems wrong?

Comment on lines +89 to +90
const { ...session } = attachment;
this.sessions.set(ws, { ...session });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the spreading and then recollecting? Just pass attachment or if you only want its properties spread that directly.

Suggested change
const { ...session } = attachment;
this.sessions.set(ws, { ...session });
this.sessions.set(ws, { ...attachment });

Comment on lines +139 to +149
this.sessions.forEach((attachment, session) => {
session.send(`[Durable Object] message: ${message}, from: ${attachment.id}. Total connections: ${this.sessions.size}`);
});

// Send a message to all WebSocket connections except the connection (ws),
// loop over all the connected WebSockets and filter out the connection (ws).
this.sessions.forEach((attachment, session) => {
if (session !== ws) {
session.send(`[Durable Object] message: ${message}, from: ${attachment.id}. Total connections: ${this.sessions.size}`);
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combine these two loops?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loops are kept separate to make it easy for users to simply copy-paste based on their use-case.

Comment on lines +29 to +37
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
if (request.url.endsWith('/websocket')) {
// Expect to receive a WebSocket Upgrade request.
// If there is one, accept the request and return a WebSocket Response.
const upgradeHeader = request.headers.get('Upgrade');
if (!upgradeHeader || upgradeHeader !== 'websocket') {
return new Response('Worker expected Upgrade: websocket', {
status: 426,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting indentation is messed up here, and below.

Comment on lines +114 to +126
// Broadcast the message to all the connections,
// except the one that sent the message.
this.sessions.forEach((k, session) => {
if (session !== ws) {
session.send(`[Durable Object] message: ${message}, from: ${connection.id}`);
}
});

// Broadcast the message to all the connections,
// including the one that sent the message.
this.sessions.forEach((k, session) => {
session.send(`[Durable Object] message: ${message}, from: ${connection.id}`);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge the loops.


return stub.fetch(request);
}
return stub.fetch(request);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than removing the extra stub.fetch, what's the diff here?

description: Build a WebSocket server using WebSocket Hibernation on Durable
Objects and Workers.
---

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix this. It seems to worsen a subsequent diff by a lot.

Copy link
Contributor

@joshthoward joshthoward left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harshil1712 git rebase -i production is your friend. There are a lot of changes here which are unsubstantiated, e.g. large changes which only adjust indentation, and inconsistent, e.g. changing ```` to ``` in some cases. Please clean up this PR so that it can be reviewed properly.

@joshthoward
Copy link
Contributor

@harshil1712 git rebase -i production is your friend. There are a lot of changes here which are unsubstantiated, e.g. large changes which only adjust indentation, and inconsistent, e.g. changing ```` to ``` in some cases. Please clean up this PR so that it can be reviewed properly.

Oh sorry @harshil1712... @Oxyjun contributed the later commits. The advice still stands... Please clean up this PR so that it can be reviewed properly.

@Oxyjun
Copy link
Contributor

Oxyjun commented Aug 7, 2025

We're now tracking this here: #24234

@Oxyjun Oxyjun closed this Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

product:durable-objects Durable Objects: https://developers.cloudflare.com/workers/learning/using-durable-objects/ size/l

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants