Skip to content

docs: add websocket guide#4197

Open
RihanArfan wants to merge 2 commits intomainfrom
docs/websocket
Open

docs: add websocket guide#4197
RihanArfan wants to merge 2 commits intomainfrom
docs/websocket

Conversation

@RihanArfan
Copy link
Copy Markdown
Member

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Adds back websocket documentation to v3 docs. Also documents namespaces in crossws.

Since it includes SSR I'm unsure whether we should call it websocket or realtime?

@RihanArfan RihanArfan requested a review from pi0 as a code owner April 9, 2026 16:54
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 9, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
nitro.build Ready Ready Preview, Comment Apr 9, 2026 4:54pm

Request Review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 9, 2026

📝 Walkthrough

Walkthrough

This pull request adds comprehensive WebSocket documentation to Nitro. It introduces a new documentation page detailing WebSocket features, handlers, lifecycle hooks, pub/sub functionality, and SSE support. The redirect rule for the legacy WebSocket guide path is updated, and a navigation link is added to the configuration documentation.

Changes

Cohort / File(s) Summary
WebSocket Documentation
docs/1.docs/50.websocket.md
New documentation page covering WebSocket enablement, handler lifecycle (upgrade, open, message, close, error), peer object properties and methods, message parsing helpers, pub/sub behavior, namespace isolation, and SSE integration with examples.
Documentation Navigation & Routing
docs/.config/docs.yaml, docs/3.config/0.index.md
Updated redirect for /guide/websocket to point to /docs/websocket and added a "read more" link in the configuration documentation to the new WebSocket guide.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description check ✅ Passed The description is directly related to the changeset, clearly explaining the documentation updates for websocket features and mentioning namespace documentation in crossws.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The PR title 'docs: add websocket guide' follows conventional commits format with a 'docs' type prefix and clearly describes the main change of adding websocket documentation.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch docs/websocket

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
docs/1.docs/50.websocket.md (2)

115-136: Optional: Vary sentence openings for better readability.

Three consecutive hook descriptions begin with "Called when". While technically correct, varying the sentence structure could improve readability. For example, you could use "Invoked when" or "Triggered when" for variety.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@docs/1.docs/50.websocket.md` around lines 115 - 136, The three hook
descriptions for open, message, and close all start with the same phrase; update
the text to vary sentence openings for readability by changing at least one to
alternatives such as "Invoked when", "Triggered when", or "Executed when" while
keeping the meaning and examples intact; modify the descriptions for the open,
message, and close hooks (symbols: open(peer), message(peer, message), close) so
each uses a different opener and ensure the subsequent explanatory text and code
blocks remain unchanged.

244-266: Consider using explicit peer properties for clarity.

The examples use ${peer} (lines 250, 263) and peer.toString() (line 256) to display peer information. While this may work if the Peer object has a toString() method, it's not documented in the Peer section above. Consider using explicit properties like peer.id or documenting the toString() behavior to avoid confusion.

📝 Suggested clarification
  open(peer) {
    peer.subscribe("chat");
-   peer.publish("chat", { system: `${peer} joined the chat` });
+   peer.publish("chat", { system: `User ${peer.id} joined the chat` });
    peer.send({ system: "Welcome to the chat!" });
  },
  message(peer, message) {
    // Broadcast to all other subscribers
    peer.publish("chat", {
-     user: peer.toString(),
+     user: peer.id,
      text: message.text(),
    });
    // Echo back to sender
    peer.send({ user: "You", text: message.text() });
  },
  close(peer) {
-   peer.publish("chat", { system: `${peer} left the chat` });
+   peer.publish("chat", { system: `User ${peer.id} left the chat` });
  },
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@docs/1.docs/50.websocket.md` around lines 244 - 266, The examples in
defineWebSocketHandler (open, message, close) use stringification of the Peer
object (`${peer}` and peer.toString()), which is ambiguous; update the handler
to use an explicit peer identifier property (e.g., peer.id) for all user/display
strings passed to peer.publish and peer.send and in broadcasts, or alternatively
add documentation that Peer implements toString(); specifically replace usages
of `${peer}`, peer.toString() and similar in the open, message and close
callbacks with a concrete property like peer.id so the displayed peer info is
deterministic and clear.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@docs/1.docs/50.websocket.md`:
- Around line 115-136: The three hook descriptions for open, message, and close
all start with the same phrase; update the text to vary sentence openings for
readability by changing at least one to alternatives such as "Invoked when",
"Triggered when", or "Executed when" while keeping the meaning and examples
intact; modify the descriptions for the open, message, and close hooks (symbols:
open(peer), message(peer, message), close) so each uses a different opener and
ensure the subsequent explanatory text and code blocks remain unchanged.
- Around line 244-266: The examples in defineWebSocketHandler (open, message,
close) use stringification of the Peer object (`${peer}` and peer.toString()),
which is ambiguous; update the handler to use an explicit peer identifier
property (e.g., peer.id) for all user/display strings passed to peer.publish and
peer.send and in broadcasts, or alternatively add documentation that Peer
implements toString(); specifically replace usages of `${peer}`, peer.toString()
and similar in the open, message and close callbacks with a concrete property
like peer.id so the displayed peer info is deterministic and clear.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 313d9694-d726-4cc2-a016-8c39902a79a9

📥 Commits

Reviewing files that changed from the base of the PR and between ab30376 and 2a99e53.

📒 Files selected for processing (3)
  • docs/.config/docs.yaml
  • docs/1.docs/50.websocket.md
  • docs/3.config/0.index.md

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 9, 2026

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4197

commit: 2a99e53

@pi0 pi0 changed the title docs: websocket docs: add websocket guide Apr 10, 2026

Enable WebSocket support in your Nitro configuration:

::code-group
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why code group?

```

## Hooks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

would be nice to link hooks/peer/message/etc to https://crossws.h3.dev/ docs as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants