Skip to content
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

Support Cloudflare Durable Object WebSocket Hibernation #271

Open
garrett-hopper opened this issue Jan 20, 2025 · 0 comments
Open

Support Cloudflare Durable Object WebSocket Hibernation #271

garrett-hopper opened this issue Jan 20, 2025 · 0 comments

Comments

@garrett-hopper
Copy link

Currently, using createTriplitHonoServer (from @triplit/server/hono) along with upgradeWebSocket (from @triplit/server/cloudflare) causes the websocket connection to be terminated directly within the DO (using server.accept()).

Ideally, this would instead use ctx.acceptWebSocket(server) to allow the workers runtime to terminate the websocket, allowing the worker to sleep during periods of inactivity on the websocket.

Messages would then need to be handled through the webSocketMessage class method instead of addEventListener on the websocket.

Build a WebSocket server with WebSocket Hibernation

// Calling acceptWebSocket() informs the runtime that this WebSocket is to begin terminating
// request within the Durable Object. It has the effect of "accepting" the connection,
// and allowing the WebSocket to send and receive messages.
// Unlike ws.accept(), state.acceptWebSocket(ws) informs the Workers Runtime that the WebSocket
// is "hibernatable", so the runtime does not need to pin this Durable Object to memory while
// the connection is open. During periods of inactivity, the Durable Object can be evicted
// from memory, but the WebSocket connection will remain open. If at some later point the
// WebSocket receives a message, the runtime will recreate the Durable Object
// (run the constructor) and deliver the message to the appropriate handler.

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

No branches or pull requests

1 participant