Skip to content

Conversation

@exectx
Copy link

@exectx exectx commented Nov 6, 2024

Summary

Hi, here's the tutorial for the Cloudflare Developer Spotlight program. It's covers using Next.js Workers with Durable Objects and Workers RPC to visualize connected users cursors via WebSockets. I've also shortened the original title a bit

Documentation checklist

Copy link
Contributor

@hyperlint-ai hyperlint-ai bot left a comment

Choose a reason for hiding this comment

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

1 files reviewed, 17 total issue(s) found.

@github-actions github-actions bot added size/l product:workers Related to Workers product labels Nov 6, 2024
@db-cloudflare db-cloudflare added DevRel Tasks that need support from developer relations. developer-spotlight labels Nov 6, 2024
Copy link
Contributor

@hyperlint-ai hyperlint-ai bot left a comment

Choose a reason for hiding this comment

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

1 files reviewed, 12 total issue(s) found.


Note: We resolved prior Hyperlint review comments because:

We updated our inline suggestion AI.

We do this to avoid keeping outdated or irrelevant comments around. We'll leave a new review with current comments below.

@harshil1712 harshil1712 self-assigned this Nov 14, 2024
Copy link
Contributor

@ToriLindsay ToriLindsay left a comment

Choose a reason for hiding this comment

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

Fixing typos and grammar

ToriLindsay and others added 2 commits November 25, 2024 11:33
…c-do/index.mdx

Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com>
Copy link
Contributor

@harshil1712 harshil1712 left a comment

Choose a reason for hiding this comment

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

Hey @exectx, this is a great tutorial! I have left a few comments. Will it be possible to simplify the Cursor component? Not a huge issue, but if it can be simplified, it will help the reader follow more easily. Rest looks good! I am excited to see this go live

tag = "v1"
new_classes = ["CursorSessions"]
```
2. Initialize the Durable Object class in your `worker/src/index.ts` file:
Copy link
Contributor

Choose a reason for hiding this comment

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

We should add a few lines here explaining the intention of the code below

Comment on lines 180 to 185
- The main `fetch` handler routes requests with a `/ws` URL to the `CursorSessions` Durable Object where a WebSocket connection is established.
- The `CursorSessions` class manages WebSocket connections, session states, and broadcasts messages to other connected clients.
- When a new WebSocket connection is established, the Durable Object broadcasts a `join` message to all connected clients; similarly, a `quit` message is broadcast when a client disconnects.
- It tracks each WebSocket client's last cursor position under the `move` message, which is broadcasted to all active clients.
- When a `get-cursors` message is received, it sends the number of currently active clients to the specific client that requested it.

Copy link
Contributor

Choose a reason for hiding this comment

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

We should modify the sentences here. It sounds like we have already implemented this in the code. Either move it after the code block, or reword it to let the user know this is what the below code block implements

Comment on lines 717 to 721
Build the app:
<PackageManagers type="run" pkg="build:worker" frame="none" />

Now, you can run Next.js development server:
<PackageManagers type="run" pkg="dev" frame="none" />
Copy link
Contributor

Choose a reason for hiding this comment

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

I get an error in the console. I build the worker app and started the Next.js server. Do I need to run the worker as well?

We should encourage the user to open the app in the browser and test it out

Copy link
Author

Choose a reason for hiding this comment

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

Did the build fail? I got eslint warnings, mainly caused by the code that highlights incoming/outgoing ws messages, which isn't directly relevant to the main content of the tutorial.

Yes, the durable object worker needs to be running. I'm adding a note

Copy link
Contributor

@hyperlint-ai hyperlint-ai bot left a comment

Choose a reason for hiding this comment

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

1 files reviewed, 13 total issue(s) found.


Note: We resolved prior Hyperlint review comments because:

We updated our inline suggestion AI.

We do this to avoid keeping outdated or irrelevant comments around. We'll leave a new review with current comments below.

Copy link
Contributor

@hyperlint-ai hyperlint-ai bot left a comment

Choose a reason for hiding this comment

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

1 files reviewed, 13 total issue(s) found.

@bllchmbrs
Copy link
Contributor

bllchmbrs commented Nov 27, 2024

@exectx , I would recommend adding the label hyperlint-ignore to this PR. The indentation isn't something we support well today and this is just too chatty to be useful (IMO). Up to you, but that might help remove some of the extraneous suggestions while we look at how best to support the indentation.

@exectx
Copy link
Author

exectx commented Nov 27, 2024

@bllchmbrs, thank you for the information but I don't have the necessary permissions for that. Perhaps @harshil1712 or @ToriLindsay could?

- Ensured WebSocket ID remains accessible by closing connections after broadcasting.
- Cleaned up `cursor.tsx` by moving the main functions to the top.
- Simplified component logic for easier understanding and maintenance.
Copy link
Contributor

@harshil1712 harshil1712 left a comment

Choose a reason for hiding this comment

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

Hey @exectx,
Thank you for making the changes. We are at the finish line! I have left two more comments. Once we have resolved them, we should be good to go 🚀

Copy link
Contributor

@harshil1712 harshil1712 left a comment

Choose a reason for hiding this comment

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

Thank you for making the changes!

@db-cloudflare
Copy link
Contributor

Amazing work. @exectx Once your author details are confirmed by email and have been updated on this PR then this should be ready to merge!

@db-cloudflare db-cloudflare enabled auto-merge (squash) December 3, 2024 13:01
@db-cloudflare db-cloudflare merged commit 0feed50 into cloudflare:production Dec 3, 2024
4 checks passed
@workers-devprod workers-devprod added the contribution [Holopin] Recognizes a docs contribution, big or small label Dec 3, 2024
@holopin-bot
Copy link

holopin-bot bot commented Dec 3, 2024

Congratulations @exectx, the maintainer of this repository has issued you a holobyte! Here it is: https://holopin.io/holobyte/cm48hb98o100910cl72yrc2m0a

This badge can only be claimed by you, so make sure that your GitHub account is linked to your Holopin account. You can manage those preferences here: https://holopin.io/account.
Or if you're new to Holopin, you can simply sign up with GitHub, which will do the trick!

harshil1712 pushed a commit that referenced this pull request Dec 3, 2024
…bjects (#18008)

* add new nextjs durable objects rpc tutorial

* fix: hyperlint-ai suggestions

* fix: address reviewer comments

* Update src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx

* Update src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx

* Update src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx

* Update src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx

* Update src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx

Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com>

* fix: address reviewer comments

* fix: use `getWebSockets` method

* fix: durable object bug and simplify client-side code

- Ensured WebSocket ID remains accessible by closing connections after broadcasting.
- Cleaned up `cursor.tsx` by moving the main functions to the top.
- Simplified component logic for easier understanding and maintenance.

* Explicitly mention to use host, add optional RPC step

* Update src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx

Add author details

---------

Co-authored-by: ToriLindsay <[email protected]>
Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com>
Co-authored-by: db-cloudflare <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contribution [Holopin] Recognizes a docs contribution, big or small developer-spotlight DevRel Tasks that need support from developer relations. hyperlint-ignore product:workers Related to Workers product size/l

Projects

None yet

Development

Successfully merging this pull request may close these issues.