Invalid WebSocket close code: 1006.
Somehow my websocket closes after 1 minute of inactivity, I don't really understand why, since I am letting the durable object hibernate and this should NOT close the websocket. Any ideas what I am doing wrong?
More of my error:
message: "Invalid WebSocket close code: 1006.", exception: { stack: " at WebSocketHibernationServer.webSocketClose (index.js:65:8)", name: "TypeError", message: "Invalid WebSocket close code: 1006.", timestamp: 1754763792033, },But if I look at my code, webSocketClose only has this:
async webSocketClose(ws, code, reason, wasClean) {
// If the client closes the connection, the runtime will invoke the webSocketClose() handler.
ws.close(code, Durable Object is closing WebSocket because ${reason}, it ${wasClean ? '' : "didn't"} close cleanly
);
}
So this doesn't make any sense to me, any help is greatly appreciated.10 Replies
Is it consistently happening after a minute or just that one time? As Cloudflare does sometimes randomly close websocket connections for reasons such as a server restart and/or updates.
https://developers.cloudflare.com/network/websockets/#technical-note
When Cloudflare releases new code to its global network, we may restart servers, which terminates WebSockets connections.
@Peps it's consistently happening after a minute, even recreated this example:
https://developers.cloudflare.com/durable-objects/examples/websocket-hibernation-server/
But the same thing happens, it makes a websocket connection -> 60 seconds later it closes it
Cloudflare Docs
Build a WebSocket server with WebSocket Hibernation
Build a WebSocket server using WebSocket Hibernation on Durable Objects and Workers.
Or is this because when I originally created my worker there was no: setWebSocketAutoResponse
So I should ping/pong to keep it alive?
you could try sending ping frames
those won't cause the DO to wake up from hibernation, they'll get automatically replied to without having to use autoresponse
Cloudflare Docs
Durable Object Base Class
The DurableObject base class is an abstract class which all Durable Objects inherit from. This base class provides a set of optional methods, frequently referred to as handler methods, which can respond to events, for example a webSocketMessage when using the WebSocket Hibernation API. To provide a concrete example, here is a Durable Object MyDu...
Lol, I first tried ping on 60 sec interval, that didn't work
But at 59 sec it does
Seems quite fast to close a websocket connection?
I'm not aware myself about CF dropping websockets after exactly a minute, but there are proxies/firewalls that do drop connections when they've been inactive for a minute
a minute is too big of an interval for keeping a websocket alive anyway, most do it at around 30s
does the ping count as a worker request?
The ping frames? they don't wake up hibernated workers, so I assume not
yep you don't get billed for ping frames
A request is needed to create a WebSocket connection. There is no charge for outgoing WebSocket messages, nor for incoming WebSocket protocol pings ↗. For compute requests billing-only, a 20:1 ratio is applied to incoming WebSocket messages to factor in smaller messages for real-time communication.https://developers.cloudflare.com/durable-objects/platform/pricing/
cool, than I will lower it to 30s
thanks a lot for the help