Repository navigation
refactor: Websocket clients can now subscribe to specific subjects - #364
Conversation
|
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. |
|
Seems like GitHub's infrastructure is having issues... (see ubuntu-build)! |
|
Thank you @m4ycon! Only had a very superficial look, but I think a different approach could possibly be:
This might have a few benefits over the proposed approach such as:
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. |
|
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. peer-observer/tools/websocket/src/lib.rs Lines 205 to 221 in 78e6d5b 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. peer-observer/tools/websocket/src/lib.rs Lines 183 to 192 in 2f2b7a9 A third commit could update the JS client code to support this. The tests need to be updated too. |
4dcd411 to
f2f685b
Compare
|
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. |
84fe330 to
ee4a8eb
Compare
|
Updated PR's description and I think now it's ready for review. |
There was a problem hiding this comment.
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.
| 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 | ||
| { |
There was a problem hiding this comment.
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.
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.
e3477a7 to
d696fd6
Compare
0xB10C
left a comment
There was a problem hiding this comment.
This looks good, but I haven't tested it much. Plan to do so soon!
d696fd6 to
fa955cb
Compare
fa955cb to
fc32714
Compare
fc32714 to
94d570b
Compare
| const SUBSCRIBE_NONE: ClientSubscriptions = ClientSubscriptions { | ||
| ebpf: ClientSubscriptionsEbpf { | ||
| messages: false, | ||
| mempool: false, | ||
| validation: false, | ||
| connections: false, | ||
| addrman: false, | ||
| }, | ||
| p2p: false, | ||
| log: false, | ||
| rpc: false, | ||
| }; |
There was a problem hiding this comment.
could probably be just ClientSubscriptions::default()
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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!
94d570b to
8e1f442
Compare
|
Adjusted the remaining details from comments, with the exception of one. Ubuntu action died by an unrelated reason hehe. And that's it :) |
- We don't print large messages in full - We disconnect on too large messages - We always send a Close messages on disconnect
0xB10C
left a comment
There was a problem hiding this comment.
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.
|
Thank you @m4ycon! This enables us to turn on the log-extractor on all nodes now! |

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
wwwfolder pages to this new strategy.closes #276