Workers - WebSockets now automatically reply to Close frames
Key Points
- Auto-reply sent on received Close frames
- readyState becomes CLOSED before close event
- Use allowHalfOpen:true to coordinate proxy closes
Summary
The Workers runtime now automatically sends a reciprocal WebSocket Close frame when it receives one from the peer. This makes Workers follow the WebSocket specification and align with browser behavior. The runtime transitions the socket's readyState to CLOSED before the close event fires.
Key Points
- Automatic close reply is enabled by default for Workers using compatibility dates on or after
2026-04-07(via theweb_socket_auto_reply_to_closeflag). - When auto-reply is active, calling
close()inside acloseevent handler is a no-op (silently ignored) because the socket is alreadyCLOSED. - For proxying scenarios that need independent close coordination, call
server.accept({ allowHalfOpen: true }). WithallowHalfOpen: true, thereadyStateremainsCLOSINGduring thecloseevent so you can manually close the socket (e.g.server.close(code, reason)). - This change is intended to be backwards-compatible; existing handlers continue to work, but behavior differs when you expect to perform manual close coordination.
Recommendations for engineers
- If you proxy WebSockets between client and backend, add
allowHalfOpen: truetoaccept()to retain control over the close sequence. - If you rely on the socket being
CLOSEDinside yourclosehandler, you can remove manualclose()calls to avoid redundant operations.
Resources
- See the WebSocket Close behavior docs for full details.