Skip to content

bare websockets - #6932

Open
benedikt-bartscher wants to merge 99 commits into
reflex-dev:mainfrom
benedikt-bartscher:make-sio-optional
Open

benedikt-bartscher wants to merge 99 commits into
reflex-dev:mainfrom
benedikt-bartscher:make-sio-optional

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

saves ~11.6 KB gz / ~41 KB raw frontend size (a small js chunk replaces whole socketio)
dropped python deps: wsproto, simple-websocket, python-socketio, python-engineio, bidict
new benchmarks in test_event_transport.py show about 2x performance increase compared to socketio

also migrates AppHarness to granian for consistency. otherwise downstream tests using AppHarness still need uvicorn and wsproto/websockets

related reflex-dev/xy#513

Review in cubic

@codspeed

codspeed Bot commented Aug 22, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 8.1%

3 warnings

⚠️ 20 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 2 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 2 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

❌ 2 regressed benchmarks
✅ 144 untouched benchmarks
🆕 4 new benchmarks
⏩ 8 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_route_matching[1000] 90 ms 100 ms -9.93%
❌ test_route_matching[100] 9.4 ms 10 ms -6.23%
🆕 test_transport_inbound_socketio N/A 30.5 ms N/A
🆕 test_transport_inbound_websocket N/A 25.1 ms N/A
🆕 test_transport_outbound_socketio N/A 32.3 ms N/A
🆕 test_transport_outbound_websocket N/A 14.9 ms N/A

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing benedikt-bartscher:make-sio-optional (bd355b5) with main (810bbd2)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@greptile-apps

greptile-apps Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The changes since the previous review appear safe to merge.

Summary

Replaces the default Socket.IO connection with a plain WebSocket and adds channels for application messages and binary data.

  • Reflex state events now travel over a plain WebSocket by default.
  • Apps can send named channel messages over the event WebSocket.
  • AppHarness runs apps with Granian instead of Uvicorn.
  • Transport benchmarks compare plain WebSockets with Socket.IO.

Reviews (84) · Last reviewed commit: "Build the hydrate-too-large error in one..." · Reviewed by Greptile

Comment thread reflex/event_namespace.py Resolved
@benedikt-bartscher
benedikt-bartscher marked this pull request as ready for review August 22, 2026 22:09
@benedikt-bartscher
benedikt-bartscher requested a review from a team as a code owner August 22, 2026 22:09

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 19 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread reflex/event_namespace.py Resolved
Comment thread reflex/event_namespace.py Resolved
Comment thread reflex/utils/exec.py Resolved
Comment thread reflex/utils/exec.py Resolved
@benedikt-bartscher
benedikt-bartscher marked this pull request as draft August 22, 2026 22:27
…ace, awaited disconnect cleanup; revert server-wide ws_max_size
Comment thread reflex/event_namespace.py Resolved
@benedikt-bartscher
benedikt-bartscher marked this pull request as ready for review August 22, 2026 22:46

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 17 files

Requires human review: Auto-approval blocked because this review re-detected 1 unresolved issue already reported by Cubic.

Re-trigger cubic

Comment thread reflex/event_namespace.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 4 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread reflex/event_namespace.py Resolved
Comment thread reflex/event_namespace.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 4 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread reflex/event_namespace.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 6 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread reflex/testing.py Resolved
Comment thread reflex/utils/exec.py Resolved
Comment thread pyproject.toml Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 6 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread reflex/testing.py Resolved
Comment thread tests/units/test_testing.py Resolved
The benchmark socket client builds on reflex-bench's wire codec, and two
unit tests that also run on Windows imported it. Importing any part of
reflex-bench makes coverage count the whole Linux-only package, whose
own tests are ignored off Linux, which pulled Windows coverage under the
threshold. Run those two tests on Linux only and import the client
inside them.
Both transports now open sessions through BaseEventNamespace: one
connect-and-boot sequence and one handler failure policy. A failed boot
event ends the session on either transport (the plain WebSocket closes
with 1011), so the client retries rather than waiting for a hydrate that
never comes. Undeserializable input ends a session quietly, and other
handler failures are logged within the error budget; Socket.IO's
on_event, on_ping and on_client_error no longer raise into
python-socketio. Socket.IO packets are serialized like plain frames, so
an unpaired surrogate is escaped instead of breaking the send.

Also: one fan-out body for channel room and token sends, a socket policy
read once per connection for both transports, a supports_channels flag
instead of re-reading the config, the embedded test server capped like
the Granian launchers, CORS strings parsed as the environment variable
is, and an unreachable uvicorn warning removed. The Socket.IO namespace
tests move from test_app.py to test_socketio_namespace.py, and the
namespace tests share fixtures and a serve() helper.
websocket.js settles a closed connection in one place, flushes the
channel and transport queues through one helper, and checks and reports
oversized frames through one size check and one message. The
single-caller emitChannel, which bypassed the channel's guards, is gone,
and the generic JSON helpers move to json.js. state.js shares its
warm-socket, socket disposal and connect-error helpers and drops an
unreachable check.

The client-only tests that ran websocket.js through the Python harness
move to websocket_js.test.mjs, and the channels integration test now
queues its frame on the transport, as its name says.
The benchmark socket client detects a backend's protocol once instead of
per connection, so Socket.IO reconnect timings no longer include a
failed WebSocket upgrade per client, and codecs carry their bare connect
and ping frames instead of callers branching on the protocol.
reflex-bench shares its process spawn, stop and report handling, its
worker wrapper and small session helpers, and drift tests pin the
protocol constants it copies from Reflex. The bench tests share their
fixtures through factories.py.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 31 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

The echo server announced its generator's 16 MiB frame limit as its
message size, while a default Reflex app (and its own Socket.IO
greeting) announces 1 MB; the drift test only compared keys. It now
announces Reflex's default on both protocols, and the test compares the
whole handshake against a default-configured app.

detect_protocol always waited up to 30 seconds to open its websocket, so
the benchmark socket client's first connection to a hung backend ignored
the caller's timeout. Detection now takes the timeout, and the client
passes its own through.
Port upstream's repeated-header combining (reflex-dev#7588) from the removed
reflex/app.py helper to _decode_asgi_headers in reflex/event_namespace.py,
which both transports share, keeping the branch's Latin-1 fallback. Its
tests move from tests/units/test_app.py to tests/units/test_event_namespace.py.
Comment thread reflex/socketio_namespace.py Resolved
Granian sets TCP_NODELAY on listeners it binds itself, but the harness
binds its own socket, so with the Redis state manager each update
stalled ~40 ms on delayed ACKs and test_client_side_state raced it.
Also poll the client storage reads that follow an event or navigation.
Port upstream's reconnect back-off (reflex-dev#7605) to the transport abstraction:
retryConnect keeps the branch's addConnectError helper and caps the attempt
count at MAX_CONNECT_ERRORS. Socket.IO reports a transport closed before the
namespace connected as a disconnect, which now retries with back-off; the
plain WebSocket reports it as connect_error, so it gets the same back-off.

The state.js back-off tests run over both transports, with the fake socket
reporting a close before connecting the way each transport does, and read
connect errors through upstream's lazily applied updaters.
@masenf masenf added the roadmap https://github.com/reflex-dev/reflex/issues/2727 label Oct 23, 2026
Port upstream's EventNamespace changes to the moved namespace modules:
the trusted proxy hops for the client IP (reflex-dev#7614) apply in
build_static_router_data, and the cancellation of a disconnected socket's
page-load chains (reflex-dev#7415) runs in BaseEventNamespace.handle_disconnect, so
both transports get them. The branch already decoded non-UTF-8 header
values (reflex-dev#7602). Their tests move from tests/units/test_app.py to
tests/units/test_event_namespace.py, and the transport benchmark's app
double gains the event processor attribute the disconnect reads.

Port the oversized hydrate check of reflex-dev#7551 to the Socket.IO transport only:
state.js wraps the socket.io encoder where it creates the Socket.IO
socket, since the plain WebSocket has no engine.io encoder. Its state.js
tests run over Socket.IO, whose fake socket now encodes CONNECT packets,
and the reconnect back-off of reflex-dev#7605 stays in place for both transports.

uv.lock is upstream's, relocked for the branch's dependencies.
The plain WebSocket sends its hydrate with the connect frame, before any
handshake could announce the server's message limit. A frame over the
limit closed the socket with 1009, which the client reported as a generic
connection failure and retried with back-off forever.

The server now names the limit in that close's reason, and the transport
reports such a close as the hydration_payload_too_large error reflex-dev#7551 added
for Socket.IO, with the frame's size and the limit when the reason gives
it. A reconnect over a limit an earlier handshake announced is refused
before it is sent. retryConnect treats the error as final for both
transports: it records it once, schedules no retry and keeps wait_connect
set, so reconnect() does not rebuild the hydrate, and the toast and banner
keyed on the error code show it.
Comment thread reflex/channels.py Resolved
When a token moved to another socket, the transport dropped the stale
recipient but send_to_token still returned True; the sender now reports
whether it accepted anyone and the fan-out returns that.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 4 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="reflex/event_namespace.py">

<violation number="1" location="reflex/event_namespace.py:1136">
P2: `bool(recipients)` reports success for linked SIDs even when `_deliver` skips the connection or `_Connection.send` rejects it after close. Return the number of recipients actually accepted by `_deliver` (or otherwise check the live connection state) so `send_to_token` does not report a delivery that never reached a socket.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread reflex/event_namespace.py Outdated
# teardown forgets its channel sessions.
connection.close(1008)
self._deliver(recipients, payload, event)
return bool(recipients)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: bool(recipients) reports success for linked SIDs even when _deliver skips the connection or _Connection.send rejects it after close. Return the number of recipients actually accepted by _deliver (or otherwise check the live connection state) so send_to_token does not report a delivery that never reached a socket.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At reflex/event_namespace.py, line 1136:

<comment>`bool(recipients)` reports success for linked SIDs even when `_deliver` skips the connection or `_Connection.send` rejects it after close. Return the number of recipients actually accepted by `_deliver` (or otherwise check the live connection state) so `send_to_token` does not report a delivery that never reached a socket.</comment>

<file context>
@@ -1130,6 +1133,7 @@ async def _send_channel_message(
                 # teardown forgets its channel sessions.
                 connection.close(1008)
         self._deliver(recipients, payload, event)
+        return bool(recipients)
 
     async def _send_channel_error(
</file context>

A linked session whose connection was closing, overflowed its send
backlog, or was already gone still counted as a recipient; the
connection now reports whether it queued the frame.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread reflex/event_namespace.py Resolved
A frame that overflowed the send backlog was queued before the
connection closed, so the writer could still deliver a send reported
as a miss.
…g it

An app that replaced EventNamespace and overrode on_event saw every
event but the hydrate on connect. The boot event goes through on_event
again, re-raised while booting so a failure still refuses the connect,
and overriding on_event now warns in favor of handle_event, which runs
for every event on both transports.
@FarhanAliRaza FarhanAliRaza added the enhancement Anything you want improved label Nov 12, 2026 — with ChatGPT Codex Connector
@masenf masenf added the breaking Public API facing change considered for minor release bump pre-v1.0 label Nov 16, 2026
Comment thread tests/units/test_testing.py Resolved
The added tests checked fragments of errors, warnings and log output;
compare each in full, with session ids normalized to a placeholder and
the expected traceback output derived from the raised exception.
Capture the token-loss test's log on the reflex logger, so its check
still sees debug records when that logger has a level of its own.
The websocket library and frame size warnings were checked by fragment;
compare the captured records in full.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 8 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="tests/units/test_app.py">

<violation number="1" location="tests/units/test_app.py:5438">
P2: These assertions inspect every captured log record, so an unrelated warning can fail the test despite correct channel behavior. Filter `caplog.records` by `record.name == "reflex.app"` and `record.levelno == logging.WARNING` before comparing the exact message in both tests.

(Based on your team's feedback about scoping app log assertions.) [6b5f533c-bd1d-446a-abb1-3cd757d9f8b0]</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread tests/units/test_app.py
app.register_channel(_ProbeChannel())

assert app._channels == {}
assert caplog.record_tuples == [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: These assertions inspect every captured log record, so an unrelated warning can fail the test despite correct channel behavior. Filter caplog.records by record.name == "reflex.app" and record.levelno == logging.WARNING before comparing the exact message in both tests.

(Based on your team's feedback about scoping app log assertions.) [6b5f533c-bd1d-446a-abb1-3cd757d9f8b0]

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At tests/units/test_app.py, line 5438:

<comment>These assertions inspect every captured log record, so an unrelated warning can fail the test despite correct channel behavior. Filter `caplog.records` by `record.name == "reflex.app"` and `record.levelno == logging.WARNING` before comparing the exact message in both tests.

(Based on your team's feedback about scoping app log assertions.) [6b5f533c-bd1d-446a-abb1-3cd757d9f8b0]</comment>

<file context>
@@ -5434,7 +5435,17 @@ def test_register_channel_skips_without_the_event_websocket(
 
     assert app._channels == {}
-    assert "needs the event websocket" in caplog.text
+    assert caplog.record_tuples == [
+        (
+            "reflex.app",
</file context>

The app double's enqueue returned None, so in dev mode handle_event's
add_done_callback raised AttributeError on every inbound event and both
inbound benchmarks measured the failure policy instead of the transport.
It now returns a resolved future, like the EventFuture enqueue returns, and
every transport benchmark fails if anything was logged at error level.

Walltime, 100 inbound events: websocket 146 us -> 140 us, Socket.IO
318-324 us -> 313-317 us.
The error code and message for a hydrate over the backend's limit were
defined in both websocket.js and state.js. websocket.js now exports
HYDRATION_PAYLOAD_TOO_LARGE and hydrationTooLargeError(size, limit, unit),
which the socket.io path in state.js uses too; the messages are unchanged.
The state.js harness builds the error with the real helper and pins the
socket.io message exactly.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Public API facing change considered for minor release bump pre-v1.0 documentation Improvements or additions to documentation enhancement Anything you want improved perf Performance-improving changes roadmap https://github.com/reflex-dev/reflex/issues/2727

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants