-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[Workers] Add tutorial: Live cursors & RPC with Next.js and Durable Objects #18008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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.
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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.
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
ToriLindsay
left a comment
There was a problem hiding this 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
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
…c-do/index.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com>
harshil1712
left a comment
There was a problem hiding this 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
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
| tag = "v1" | ||
| new_classes = ["CursorSessions"] | ||
| ``` | ||
| 2. Initialize the Durable Object class in your `worker/src/index.ts` file: |
There was a problem hiding this comment.
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
| - 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. | ||
|
|
There was a problem hiding this comment.
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
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Outdated
Show resolved
Hide resolved
| 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" /> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this 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.
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
There was a problem hiding this 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.
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
|
@exectx , I would recommend adding the label |
|
@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.
harshil1712
left a comment
There was a problem hiding this 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 🚀
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
harshil1712
left a comment
There was a problem hiding this 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!
|
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! |
src/content/docs/workers/tutorials/live-cursors-with-nextjs-rpc-do/index.mdx
Show resolved
Hide resolved
…c-do/index.mdx Add author details
|
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. |
…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]>
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