Skip to content

refactor: Websocket clients can now subscribe to specific subjects - #364

Merged
0xB10C merged 6 commits into
peer-observer:masterfrom
m4ycon:refactor/subject-sub-websocket
Mar 9, 2026
Merged

0xB10C merged 6 commits into
peer-observer:masterfrom
m4ycon:refactor/subject-sub-websocket

Conversation

@m4ycon

@m4ycon m4ycon commented Feb 24, 2026 •

Copy link
Copy Markdown
Collaborator

Websocket tool now accepts incoming messages to update subjects that it will deliver to client. E.g.:

{
  "ebpf": {
    "messages": false,
    "mempool": false,
    "validation": false,
    "connections": false,
    "addrman": false
  },
  "p2p": false,
  "log": false,
  "rpc": false
}

Client can omit subjects that they don't want, keeping just the "true" ones. Also, there's a "help" command to get a default template of all possibilities.

Updated tests, docs and www folder pages to this new strategy.

closes #276

@m4ycon

m4ycon commented Feb 24, 2026

Copy link
Copy Markdown
Collaborator Author

Hi @0xB10C,

This is a base PR to avoid webhook flooding all connections, feel free to take a initial look. It still misses docs and tests, I'll see about that later.

@deadmanoz

Copy link
Copy Markdown
Collaborator

Seems like GitHub's infrastructure is having issues... (see ubuntu-build)!

@0xB10C

0xB10C commented Feb 24, 2026

Copy link
Copy Markdown
Collaborator

Thank you @m4ycon!

Only had a very superficial look, but I think a different approach could possibly be:

  • keep the global subscription where we subscribe to everything
  • instead of using the URL query parameter, have the client send us a websocket message similar to { subscribe: ["ebpf-extractor", "log-extractor"] } to get ebpf and log extractor data

This might have a few benefits over the proposed approach such as:

  • not running into resource constraints when a lot of clients connect to the websocket server
  • support for subscribing to multiple websockets at a time

If you are still around, let's discuss in person! I also have a new demo NATS server we can use for testing, if you want.

@0xB10C

0xB10C commented Feb 24, 2026 •

Copy link
Copy Markdown
Collaborator

I've started coding up my approach, it's still wip and doesn't have tests. Feel free to have a look and use it (or parts): https://github.com/peer-observer/peer-observer/pull/new/2026-02-websocket-filtering

The first commit 78e6d5b is fairly complete. It filters a message from being send to a client, if the client didn't subscribe to it.

let is_subscribed = match &event {
PeerObserverEvent::EbpfExtractor(ebpf) => match &ebpf.ebpf_event {
Some(ebpf::EbpfEvent::Message(_)) => client.subscriptions.ebpf.messages,
Some(ebpf::EbpfEvent::Connection(_)) => client.subscriptions.ebpf.connections,
Some(ebpf::EbpfEvent::Addrman(_)) => client.subscriptions.ebpf.addrman,
Some(ebpf::EbpfEvent::Mempool(_)) => client.subscriptions.ebpf.mempool,
Some(ebpf::EbpfEvent::Validation(_)) => client.subscriptions.ebpf.validation,
None => false,
},
PeerObserverEvent::RpcExtractor(_) => client.subscriptions.rpc,
PeerObserverEvent::P2pExtractor(_) => client.subscriptions.p2p,
PeerObserverEvent::LogExtractor(_) => client.subscriptions.log,
};
if !is_subscribed {
continue;
}

The second commit 2f2b7a9 is mostly still a TODO: The goal is to handle the actual subscription of clients. So a client tells us about a topic he want to subscribe to in a ClientSubscriptions JSON message, we parse it, and update the client subscriptions.

TungsteniteMessage::Text(text) => {
log::debug!("Received message from client '{}': {}", addr, text);
// TODO:
// We assume the client is sending us a JSON message constisting of a ClientSubscriptions struct
// Parse it here, and update the subscriptions of the client. This requires locking and updating
// the client.
// If we receive something we can't parse, we should print an error and close the connection to the client.
// This should allow us to catch this in e.g. tests or manual testing of websocket HTML tools.
}
_ => (),

A third commit could update the JS client code to support this.

The tests need to be updated too.

Comment thread tools/websocket/Cargo.toml Resolved
@m4ycon

m4ycon commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator Author

Restarted from your WIP branch @0xB10C, tested with your demo nodes and seems to work well. In addition to the default json payload is, a "help" command that gives us an example from it.

I'll see docs now and then tests, let me know if you see anything missing there.

@m4ycon
m4ycon force-pushed the refactor/subject-sub-websocket branch 3 times, most recently from 84fe330 to ee4a8eb Compare March 1, 2026 19:48
@m4ycon

m4ycon commented Mar 3, 2026

Copy link
Copy Markdown
Collaborator Author

Updated PR's description and I think now it's ready for review.

@m4ycon
m4ycon marked this pull request as ready for review March 3, 2026 01:48
Copilot AI review requested due to automatic review settings March 3, 2026 01:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Refactors the websocket tool to support per-client, opt-in event delivery via a JSON “subscriptions” message (plus a "help" command for a template), reducing unnecessary websocket traffic as requested in #276.

Changes:

  • Add server-side per-client subscription state and filter broadcasts accordingly.
  • Update www/ visualizations to send subscription messages on connect.
  • Update integration tests and documentation to reflect the new subscription protocol.

Reviewed changes

Copilot reviewed 11 out of 12 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
tools/websocket/src/lib.rs Adds subscription structs, tracks per-client subscriptions, filters outgoing events, implements "help" + subscription updates.
tools/websocket/tests/integration.rs Refactors integration harness to send subscription messages and adds a specific-subjects subscription test.
tools/websocket/www/js/websocket-picker.js Sends subscriptions on websocket open and adds updateSubscriptions(...) helper.
tools/websocket/www/addrrelay.html Sets default page subscriptions before initializing websocket picker.
tools/websocket/www/txrelay.html Sets default page subscriptions before initializing websocket picker.
tools/websocket/www/pingpong.html Sets default page subscriptions before initializing websocket picker.
tools/websocket/www/peers.html Sets default page subscriptions before initializing websocket picker.
tools/websocket/www/p2p-circle.html Sets default page subscriptions before initializing websocket picker.
tools/websocket/www/netinfo.html Subscribes only to rpc events for this page.
tools/websocket/README.md Documents subscription behavior and "help" command.
tools/websocket/Cargo.toml Adds serde dependency for subscription (de)serialization.
Cargo.lock Records dependency updates.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tools/websocket/tests/integration.rs Resolved
Comment thread tools/websocket/tests/integration.rs Resolved
Comment thread tools/websocket/src/lib.rs Resolved
Comment on lines 250 to +274
let mut clients = clients.lock().await;
for (addr, client) in clients.iter_mut() {
let is_subscribed = match &event {
PeerObserverEvent::EbpfExtractor(ebpf) => match &ebpf.ebpf_event {
Some(ebpf::EbpfEvent::Message(_)) => client.subscriptions.ebpf.messages,
Some(ebpf::EbpfEvent::Connection(_)) => client.subscriptions.ebpf.connections,
Some(ebpf::EbpfEvent::Addrman(_)) => client.subscriptions.ebpf.addrman,
Some(ebpf::EbpfEvent::Mempool(_)) => client.subscriptions.ebpf.mempool,
Some(ebpf::EbpfEvent::Validation(_)) => client.subscriptions.ebpf.validation,
None => false,
},
PeerObserverEvent::RpcExtractor(_) => client.subscriptions.rpc,
PeerObserverEvent::P2pExtractor(_) => client.subscriptions.p2p,
PeerObserverEvent::LogExtractor(_) => client.subscriptions.log,
};

if !is_subscribed {
continue;
}

for (addr, outgoing) in clients.iter_mut() {
if let Err(e) = outgoing.send(TungsteniteMessage::text(message)).await {
if let Err(e) = client
.outgoing
.send(TungsteniteMessage::text(&message))
.await
{

Copilot AI Mar 3, 2026

Copy link

Choose a reason for hiding this comment

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

broadcast_to_clients holds the clients mutex guard while awaiting client.outgoing.send(...). If one client is slow/backpressured, this blocks subscription updates and broadcasting to other clients because the lock stays held across .await. Consider taking a snapshot of the subscribed recipients (or using per-client channels / a separate mutex per client) so the shared clients map lock is not held during network I/O.

Copilot uses AI. Check for mistakes.
Comment thread tools/websocket/README.md Resolved
We want to have the websocket clients to subscribe to the topics
they are interested in, and don't receive the full firehose of
all topics. This implements the filtering part, but not the handling
of the clients actually subscribing.
@m4ycon
m4ycon force-pushed the refactor/subject-sub-websocket branch 2 times, most recently from e3477a7 to d696fd6 Compare March 4, 2026 02:12

@0xB10C 0xB10C left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This looks good, but I haven't tested it much. Plan to do so soon!

Comment thread tools/websocket/src/lib.rs Resolved
@m4ycon
m4ycon force-pushed the refactor/subject-sub-websocket branch from d696fd6 to fa955cb Compare March 4, 2026 02:24
@0xB10C
0xB10C force-pushed the refactor/subject-sub-websocket branch from fa955cb to fc32714 Compare March 5, 2026 16:06
Comment thread tools/websocket/src/lib.rs Resolved
Comment thread tools/websocket/src/lib.rs Resolved
@0xB10C
0xB10C force-pushed the refactor/subject-sub-websocket branch from fc32714 to 94d570b Compare March 5, 2026 16:31
Comment on lines +42 to +53
const SUBSCRIBE_NONE: ClientSubscriptions = ClientSubscriptions {
ebpf: ClientSubscriptionsEbpf {
messages: false,
mempool: false,
validation: false,
connections: false,
addrman: false,
},
p2p: false,
log: false,
rpc: false,
};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

could probably be just ClientSubscriptions::default()

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've tried it at first, but got this:

error[E0015]: cannot call non-const associated function `<ClientSubscriptions as std::default::Default>::default` in constants
  --> tools/websocket/tests/integration.rs:42:45
   |
42 | const SUBSCRIBE_NONE: ClientSubscriptions = ClientSubscriptions::default();
   |                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: calls in constants are limited to constant functions, tuple structs and tuple variants

I could create a const function for it, but between one or another I prefer this one.

@0xB10C 0xB10C left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Pushed to squash two commits and to fix the serde problem. I left a few comments for you, but I think then this is good to go!

Comment thread tools/websocket/README.md Resolved
Comment thread tools/websocket/README.md Resolved
Comment thread tools/websocket/README.md Resolved
Comment thread tools/websocket/README.md Resolved
@m4ycon
m4ycon force-pushed the refactor/subject-sub-websocket branch from 94d570b to 8e1f442 Compare March 5, 2026 23:12
@m4ycon

m4ycon commented Mar 5, 2026

Copy link
Copy Markdown
Collaborator Author

Adjusted the remaining details from comments, with the exception of one. Ubuntu action died by an unrelated reason hehe. And that's it :)

@deadmanoz

Copy link
Copy Markdown
Collaborator

Testing this locally, working great

Screenshot 2026-03-09 at 3 36 33 pm

- We don't print large messages in full
- We disconnect on too large messages
- We always send a Close messages on disconnect

@0xB10C 0xB10C left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Finally got the time to play around with this. Seems to be working well.

I'll fixup these two comments and I think then this is ready for merge.

Comment thread tools/websocket/src/lib.rs Resolved
Comment thread tools/websocket/src/lib.rs Resolved
@0xB10C
0xB10C merged commit c8527df into peer-observer:master Mar 9, 2026
23 of 24 checks passed
@0xB10C

0xB10C commented Mar 9, 2026

Copy link
Copy Markdown
Collaborator

Thank you @m4ycon! This enables us to turn on the log-extractor on all nodes now!

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.

websocket-tool: don't just flood websocket clients with all messages, let them subscribe to the messages they want

4 participants