#Invalid WebSocket close code: 1006.

19 messages · Page 1 of 1 (latest)

spare glen
#

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.

crisp pebble
#

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.

spare glen
#

Or is this because when I originally created my worker there was no: setWebSocketAutoResponse

#

So I should ping/pong to keep it alive?

crisp pebble
#

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

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...

spare glen
#

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?

crisp pebble
#

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

spare glen
#

does the ping count as a worker request?

crisp pebble
#

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.

spare glen
#

cool, than I will lower it to 30s