OpenAICloudflare Developer PlatformApr 7, 2026, 12:00 AM

Workers - WebSockets now automatically reply to Close frames

A condensed section focused on the key takeaways first.

Original Post

Quick Digest

Summary

A condensed section focused on the key takeaways first.

openaienmodel: gpt-5-mini-2025-08-07

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 the web_socket_auto_reply_to_close flag).
  • When auto-reply is active, calling close() inside a close event handler is a no-op (silently ignored) because the socket is already CLOSED.
  • For proxying scenarios that need independent close coordination, call server.accept({ allowHalfOpen: true }). With allowHalfOpen: true, the readyState remains CLOSING during the close event 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: true to accept() to retain control over the close sequence.
  • If you rely on the socket being CLOSED inside your close handler, you can remove manual close() calls to avoid redundant operations.

Resources

  • See the WebSocket Close behavior docs for full details.

Full Translation

Translations

A translation section that keeps the flow of the original article.

openaijamodel: gpt-5-mini-2025-08-07

Workers - WebSockets が Close フレームに自動で応答するようになりました

Workers - WebSockets が Close フレームに自動で応答するようになりました

公開日:2026-04-07

Workers ランタイムは、ピアから Close フレームを受信したときに相互の Close フレームを自動的に送信するようになりました。readyState は close イベントが発火する前に CLOSED に遷移します。この動作は WebSocket specification と標準ブラウザの挙動に一致します。

この変更は、互換性日付が 2026-04-07 以降の Workers(web_socket_auto_reply_to_close 互換フラグ経由)でデフォルトで有効になります。

  • 既存のコードで close イベントハンドラ内から明示的に close() を呼び出している場合でも動作は継続します。WebSocket が既に閉じているときの呼び出しは静かに無視されます。

動作の例

サーバ側が相手の Close を受け取ったとき、readyState はすでに CLOSED になっています。サンプル:

const [client, server] = Object.values(new WebSocketPair());
server.accept();
server.addEventListener("close", (event) => {
    // readyState はすでに CLOSED — server.close() を呼ぶ必要はありません。
    console.log(server.readyState); // WebSocket.CLOSED
    console.log(event.code); // 1000
    console.log(event.wasClean); // true
});

WebSocket プロキシ(Half-open)モード

自動 Close の動作は、Worker がクライアントとバックエンドの間に入り、両側の Close を独立して調整する必要がある WebSocket プロキシングと干渉することがあります。このユースケースをサポートするために、accept() に { allowHalfOpen: true } を渡してください。

サンプル:

const [client, server] = Object.values(new WebSocketPair());
server.accept({ allowHalfOpen: true });
server.addEventListener("close", (event) => {
    // ここでは readyState はまだ CLOSING のままなので、
    // 他側の Close を調整する時間があります。
    console.log(server.readyState); // WebSocket.CLOSING

    // 準備ができたら手動で閉じる。
    server.close(event.code, "done");
});

補足

  • この変更は WebSocket の仕様に準拠したものであり、ブラウザの標準的な挙動と一致します。
  • 追加情報は「WebSockets Close behavior」を参照してください。

リソース: API、Cloudflare ドキュメント、ヘルプセンター。

Workers - WebSockets now automatically reply to Close frames | Cloudflare Developer Platform | DocsDigest