Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fuzzy-rice-send.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router-hono-server": minor
---

✨ feat: expose WebSocket instance for node adapter
2 changes: 1 addition & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: ⎔ Setup node
uses: actions/setup-node@v4
with:
node-version: "22.x"
node-version: "24.x"
registry-url: "https://registry.npmjs.org"
cache: "pnpm"

Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -872,15 +872,13 @@ You can use React Router middleware with this package.

Then, you can use the `use` function from `react-router` to access the context in your loaders and actions.

```ts


### Using WebSockets
#### Node
This package has a built-in helper to use `@hono/node-ws`

> [!TIP]
> Check this [example](./examples/node/websocket/) to see how to use it.
> Check this [example](./examples/node/websocket/) to see how to use it

```ts
import type { WSContext } from "hono/ws";
Expand All @@ -892,7 +890,13 @@ const clients = new Set<WSContext>();
export default createHonoServer({
useWebSocket: true,
// 👆 Unlock this 👇 from @hono/node-ws
configure: (app, { upgradeWebSocket }) => {
configure: (app, { upgradeWebSocket, wss }) => {
// You now have access to the WebSocketServer instance
wss.on('connection', (ws) => {
console.log('A new client connected!');
// Implement pingpong or something.
});

app.get(
"/ws",
upgradeWebSocket((c) => ({
Expand Down
2 changes: 1 addition & 1 deletion examples/node/simple-future/react-router.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import type { Config } from "@react-router/dev/config";

export default {
future: {
unstable_viteEnvironmentApi: true,
v8_viteEnvironmentApi: true,
},
} satisfies Config;
16 changes: 15 additions & 1 deletion examples/node/websocket/app/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NodeWebSocket } from "@hono/node-ws";
import type { WSContext } from "hono/ws";
import { createHonoServer } from "react-router-hono-server/node";

Expand All @@ -6,10 +7,18 @@ console.log("loading server");
// Store connected clients
const clients = new Set<WSContext>();
Copy link

Choose a reason for hiding this comment

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

Instead of storing clients manually could use wss.clients in this example, since @hono/node-ws has clientTracking enabled.


let $wss:NodeWebSocket["wss"] | undefined;

export default await createHonoServer({
useWebSocket: true,
// 👆 Unlock this 👇 from @hono/node-ws
configure: (app, { upgradeWebSocket }) => {
configure: (app, { upgradeWebSocket, wss }) => {
$wss = wss;

wss.on('connection', () => {
console.log('A new client connected! From configure');
});

app.get(
"/ws",
upgradeWebSocket((c) => ({
Expand Down Expand Up @@ -37,3 +46,8 @@ export default await createHonoServer({
);
},
});

$wss?.on('connection', () => {
console.log('A new client connected! From outside configure');
// Implement pingpong or something.
});
2 changes: 1 addition & 1 deletion examples/node/websocket/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router": "^7.0.1",
"react-router-hono-server": "*",
"react-router-hono-server": "file:../../../out",
"react-use-websocket": "^4.11.1"
},
"devDependencies": {
Expand Down
Loading
Loading