Skip to content

Deliver each topic through its own Socket.IO room - #1010

Open
bracyw wants to merge 6 commits into
1001-heartbeat-message-countfrom
993-socket-io-rooms
Open

bracyw wants to merge 6 commits into
1001-heartbeat-message-countfrom
993-socket-io-rooms

Conversation

@bracyw

@bracyw bracyw commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Changes

scylla-server sends each data message only to sockets in its topic's Socket.IO room. A client subscribes with a set_subscriptions event (the full list, numbered so the latest always wins even when events are handled out of order) or in the connect handshake's auth topics; once it has, it never gets every topic again. Clients that never subscribe stay on an every-topic room, so older clients keep working. Topics nobody reads are skipped before serializing. ADR 0009 records the decision. Benchmarks are in scylla-server/benchmarks/.

Diagram

flowchart LR
  subgraph Connect["On connect"]
    direction TB
    H{"handshake has<br/>auth.topics?"}
    H -- no --> ALL["every-topic room<br/>(clients that send no list)"]
    H -- "yes, even empty" --> T["exactly those topic rooms<br/>(list seq 0)"]
  end
  subgraph Change["On set_subscriptions"]
    direction TB
    Q{"seq above the<br/>last list applied?"}
    Q -- no --> DROP["drop it, ack the latest"]
    Q -- yes --> J["leave removed rooms,<br/>join added ones;<br/>never back to every-topic"]
  end
  subgraph Emit["Each MQTT data message"]
    direction TB
    N{"any socket in its topic room<br/>or the every-topic room?"}
    N -- no --> SKIP["skip, never serialized"]
    N -- yes --> EM["serialize once, emit to<br/>topic room + every-topic room"]
  end
  Connect ~~~ Change ~~~ Emit
Loading

Testing

  • 13 new unit tests, including one that fails if the fence keeping a socket out of both the every-topic room and a topic room is removed
  • Live against Calypso over MQTT with Node clients: an old client got every topic, a handshake client only its topic, an empty handshake nothing, and set_subscriptions narrowed then stopped delivery

Benchmarks

Live, 15 clients each on a small page (~25 topics), release builds of this branch and of develop (cc50fa0), M4 Pro laptop:

develop rooms
data msgs/s, all clients 60,782 3,046
data MB/s, all clients 7.6 0.37
Server CPU (one core) 20 % 8 %
Client CPU each (Node) ~5 % 0.3–2.2 %

Per data emit with 15 sockets: develop's broadcast 3.8 µs, rooms with small pages 0.5 µs, a topic nobody reads 0.08 µs. Old clients cost the same as on develop. A page switch acks in 0.3–1.4 ms. Method, every table and caveats are in the benchmarks README.

Notes

Checklist

  • Commits are tagged with the ticket number
  • Lint, format and tests pass
  • No merge conflicts
  • All checks passing
  • Screenshots of UI changes (see Screenshots)
  • No package-lock.json changes unless dependencies changed
  • Request reviewers & ping on Slack

Closes #993

Clients name their topics in the handshake (auth.topics) or with
set_subscriptions (acked with {"count": n}) and get only those; clients
that never do stay on the every-topic room. Unread topics are skipped
before serialization. ADR 0009 records the decision.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013vbA3j9hMDhLiYyTkHLpH8
Co-Authored-By: bracyw <138251570+bracyw@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Central YAML; Path: .coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4f17cbce-d98a-46c5-bc9d-c3477fbdf26e

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@bracyw bracyw added backend Development Focused in the Backend by: ai Written by AI dev work Implementation work: feature, bug, or task labels Nov 20, 2026 — with Claude
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: bracyw <138251570+bracyw@users.noreply.github.com>
Claude-Session: https://claude.ai/code/session_013vbA3j9hMDhLiYyTkHLpH8
socketioxide handles each event in its own task, so lists sent close
together could apply out of order and leave released topics live. Each
list now carries a seq; scylla drops one numbered no higher than the last
it applied, and acks {count, seq}.
@bracyw
bracyw changed the base branch from develop to 1001-heartbeat-message-count November 20, 2026 21:16
@bracyw
bracyw added this pull request to stack #1016 November 20, 2026 21:16
@bracyw
bracyw marked this pull request as ready for review November 20, 2026 21:16

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

Looks good, a few problems and nits caught

SocketClientId(client_id): SocketClientId,
HandshakeTopics(topics): HandshakeTopics| async move {
// `data`: every topic, or only the handshake's or `set_subscriptions`' topics
ns_subscriptions.register(&socket, topics).await;

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.

Register set_subscriptions before the connect ack, as #986 does; a list sent right after connecting is dropped

/// only in its topic rooms: never both (socketioxide does not dedupe a
/// socket in two target rooms, so it would get the message twice) and
/// never neither (it would miss a topic it kept).
every_topic_exit: tokio::sync::RwLock<()>,

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.

Do sockets need to leave the every-topic room? No client does, and every emit takes this lock.

@@ -0,0 +1,25 @@
# Deliver each topic through its own Socket.IO room

scylla-server sends the live `data` event for a topic only to clients that asked for it, over the Socket.IO connection they already have. Each topic is a room. A client names the full list of topics it reads, in the connect handshake (auth payload `{"topics": [...]}`) or later with the `set_subscriptions` event (`{"seq": n, "topics": [...]}`, acked with `{"count", "seq"}` of the list it ended on), and scylla moves its socket into exactly those rooms. A client that never names any, such as an old Angular build or the Flutter app, sits in one every-topic room (`#`) and gets every topic, as before. Once a client has named a list, even an empty one, it is never in the every-topic room again. `metadata`, `faults`, `timers` and rule notifications still go to every socket.

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.

Say rule notifications go only to subscribed clients

@@ -0,0 +1,125 @@
# Selective delivery benchmarks

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.

Are the benchmarks needed in the repo? CI never runs them, and they add a Node package to the crate.

/// Connect extractor for the topics a socket named in its handshake, the
/// Socket.IO auth payload `{"topics": [...]}`. `None` when there is no
/// `topics` field, or it is not a list of strings.
pub struct HandshakeTopics(pub Option<Vec<String>>);

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.

Nit: Take TryData<HandshakeAuth> in the handler and drop this wrapper

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

backend Development Focused in the Backend by: ai Written by AI dev work Implementation work: feature, bug, or task

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deliver each topic through its own Socket.IO room

3 participants