0x0dd154
0x0dd154
CDCloudflare Developers
Created by ceifa on 1/8/2024 in #workers-help
How to use the hibernatable websocket API on workers + durable objects?
There is a full example in the workers-chat-demo repo https://github.com/cloudflare/workers-chat-demo/blob/hibernation/src/chat.mjs
3 replies
CDCloudflare Developers
Created by ceifa on 1/8/2024 in #workers-help
How to use the hibernatable websocket API on workers + durable objects?
You accept new connections with the state.acceptWebSocket function and your durable object interface should implement the handlers - here is an incomplete example:
export function myDurableObject(state, env) {

const fetch = (request) => {
// stuff that handles websocket upgrade
// calling state.acceptWebSocket()
const [client, server] = new WebSocketPair();
state.acceptWebSocket(server);
return new Response(null, { status: 101, webSocket: client })
}

const webSocketMessage = (ws, message) => {
// handle message
}

const webSocketClose = (ws, code, reason, wasClean) => {
// handle close
}

const webSocketError = (ws, error) => {
// handle error
}

return { fetch, webSocketMessage, webSocketClose, webSocketError };
}
export function myDurableObject(state, env) {

const fetch = (request) => {
// stuff that handles websocket upgrade
// calling state.acceptWebSocket()
const [client, server] = new WebSocketPair();
state.acceptWebSocket(server);
return new Response(null, { status: 101, webSocket: client })
}

const webSocketMessage = (ws, message) => {
// handle message
}

const webSocketClose = (ws, code, reason, wasClean) => {
// handle close
}

const webSocketError = (ws, error) => {
// handle error
}

return { fetch, webSocketMessage, webSocketClose, webSocketError };
}
3 replies
CDCloudflare Developers
Created by Gagan Suie on 12/24/2023 in #workers-help
Disable swaggerUI in production env
I think you only have access to this in your fetch handler. So depending on how that looks it will need handling there with either middleware on the router or in the handlers for your openapi endpoints.
2 replies