Skip to content

Send no Sec-WebSocket-Protocol header for the JSON transport - #69

Merged
FZambia merged 1 commit into
centrifugal:masterfrom
damirych:omit-empty-subprotocol-header
Oct 13, 2026
Merged

FZambia merged 1 commit into
centrifugal:masterfrom
damirych:omit-empty-subprotocol-header

Conversation

@damirych

Copy link
Copy Markdown
Contributor

What changed

For the JSON transport, _create_connection now passes subprotocols=None to websockets.connect() instead of []. The protobuf transport still offers centrifuge-protobuf.

Why

With [], websockets sends Sec-WebSocket-Protocol: with an empty value. RFC 6455 defines the header as a non-empty list of tokens (1#token, section 4.1), and strict servers refuse the handshake. The websockets server is one of them, which is why tests/fake_server.py says it speaks protobuf only because "the JSON client transport sends an empty Sec-WebSocket-Protocol header that the websockets server rejects". Centrifugo accepts the empty header, so this only shows up behind stricter servers or proxies.

centrifuge-js leaves the subprotocol out for JSON too (transport_websocket.ts):

if (subProtocol !== '') {
  this._transport = new this.options.websocket(this.endpoint, subProtocol);
} else {
  this._transport = new this.options.websocket(this.endpoint);
}

The paragraph in the tests/fake_server.py docstring that gave this as the reason for protobuf is reduced to "it speaks protobuf only".

Verification

Added tests/test_json_handshake.py, which needs no Docker. A plain websockets.serve() server answers the JSON connect command and records the Sec-WebSocket-Protocol headers of each handshake. The test asserts that a JSON client ends up CONNECTED and sent no such header. On master it fails: the handshake is refused and the client stays CONNECTING.

Also ran:

  • tests/test_client.py against this repo's docker-compose Centrifugo, which runs its tests over both JSON and protobuf: 62 tests OK.
  • The Docker-free suites (test_filter, test_fossil, test_proxy, test_ssl, test_state_invalidation, test_sub_refresh, test_background_tasks, test_ready, test_refresh, test_unsubscribe, test_utils) together with the new one: 62 tests OK.
  • ruff check . and ruff format --check: clean.

🤖 Generated with Claude Code

For JSON the client passed subprotocols=[] to websockets.connect(),
which then sends the header with an empty value. RFC 6455 allows the
header only with at least one subprotocol, and servers such as the
websockets one reject the handshake. Passing None leaves the header
out. The fake test server no longer needs protobuf for this reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@FZambia

FZambia commented Oct 13, 2026

Copy link
Copy Markdown
Member

Thanks @damirych !

@FZambia
FZambia merged commit 4ef4ddb into centrifugal:master Oct 13, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants