From 80fe3e8a56484a58f6f3a6f8c8bd9083ad441e8e Mon Sep 17 00:00:00 2001 From: Nicholas Kissel Date: Sun, 18 Jan 2026 17:41:38 -0800 Subject: [PATCH] chore: websocket fixes for cursor example --- examples/cursors-raw-websocket/frontend/App.tsx | 12 +++++++----- .../rivetkit/src/actor/router-websocket-endpoints.ts | 3 ++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/examples/cursors-raw-websocket/frontend/App.tsx b/examples/cursors-raw-websocket/frontend/App.tsx index ad6b5bc3f7..288dafc74c 100644 --- a/examples/cursors-raw-websocket/frontend/App.tsx +++ b/examples/cursors-raw-websocket/frontend/App.tsx @@ -6,7 +6,10 @@ import type { registry, } from "../src/actors.ts"; -const rivetUrl = "http://localhost:6420"; +// HTTP requests go through Vite proxy +const rivetUrl = `${location.origin}/api/rivet`; +// WebSocket connections go directly to RivetKit server (vite-plugin-srvx doesn't proxy WS) +const rivetWsUrl = "ws://localhost:6420"; const client = createClient(rivetUrl); @@ -89,13 +92,12 @@ export function App() { const actorId = await client.cursorRoom.getOrCreate(roomId).resolve(); console.log("found actor", actorId); - // FIXME: derive ws url from rivet url, should use metadata and `clientEndpoint` - const wsOrigin = rivetUrl.replace(/^http/, "ws"); - const wsUrl = `${wsOrigin}/gateway/${actorId}/websocket?sessionId=${encodeURIComponent(sessionId)}`; + // Connect directly to RivetKit server for WebSocket (vite-plugin-srvx doesn't proxy WS) + const wsUrl = `${rivetWsUrl}/gateway/${actorId}/websocket?sessionId=${encodeURIComponent(sessionId)}`; console.log("ws url:", wsUrl); - // Create WebSocket connection + // Create raw WebSocket connection (no subprotocols needed) ws = new WebSocket(wsUrl); wsRef.current = ws; diff --git a/rivetkit-typescript/packages/rivetkit/src/actor/router-websocket-endpoints.ts b/rivetkit-typescript/packages/rivetkit/src/actor/router-websocket-endpoints.ts index 618b313b00..fec29c5dd4 100644 --- a/rivetkit-typescript/packages/rivetkit/src/actor/router-websocket-endpoints.ts +++ b/rivetkit-typescript/packages/rivetkit/src/actor/router-websocket-endpoints.ts @@ -384,7 +384,8 @@ export function parseWebSocketProtocols( } } - const encoding = EncodingSchema.parse(encodingRaw); + // Default to "json" encoding for raw WebSocket connections without subprotocols + const encoding = EncodingSchema.parse(encodingRaw ?? "json"); const connParams = connParamsRaw ? JSON.parse(connParamsRaw) : undefined; return { encoding, connParams };