feat: move perps streaming ownership to background (Phase 2 POC)#40600
Merged
gambinish merged 8 commits intoperps/poc-controller-integration-tempfrom Mar 4, 2026
Merged
Conversation
Implements Path 2 streaming architecture: background PerpsController owns WebSocket connections and emits perpsStreamUpdate notifications to the UI. The UI PerpsController instance is removed entirely, eliminating dual WebSocket connections and split controller authority. Changes: - metamask-controller.js: subscribe background PerpsController to all 6 channels (positions, orders, account, prices, orderBook, candles); emit perpsStreamUpdate notifications per-connection; track subscriber count via perpsSubscriberChange to gate emission - ui/index.js: route perpsStreamUpdate notifications to PerpsStreamManager - PerpsStreamManager: add prices/orderBook channels; add handleBackgroundUpdate() for all 6 channels; add initial REST fetch connectFn for positions/orders/account; clear caches on account switch - getPerpsController: remove UI PerpsController instantiation; simplify to facade-only; add markPerpsControllerInitialized/isInitialized helpers - createPerpsControllerFacade: remove streaming methods; all operations delegate to background via submitRequestToBackground - usePerpsLivePrices/usePerpsLiveOrderBook: migrate from direct controller subscribe to usePerpsChannel pattern - CandleStreamChannel: add pushFromBackground() method - usePerpsStreamManager: clear caches eagerly on account switch to prevent stale data flash - All related tests updated (193 passing)
Contributor
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
Contributor
✨ Files requiring CODEOWNER review ✨👨🔧 @MetaMask/perps (26 files, +1169 -1958)
|
Author
|
I have read the CLA Document and I hereby sign the CLA |
Contributor
Builds ready [fedc086]
⚡ Performance Benchmarks
🌐 Dapp Page Load BenchmarksCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
7 tasks
…etamask-controller
30bd5e1
into
perps/poc-controller-integration-temp
5 of 8 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR implements Path 2 from the streaming architecture discussion: move WebSocket ownership from the UI to the background process, eliminating the dual-controller problem flagged in #40078.
The Problem (from #40078 review)
The current architecture creates two
PerpsControllerinstances — one in the background and one in the UI — resulting in:Path 2: Custom JSON-RPC Notification Channel
Background subscribes to HyperLiquid WebSocket via
PerpsController, then emitsperpsStreamUpdatenotifications over the existingoutStreamconnection. The UI handles them alongsidesendUpdateinui/index.js.What Changed
metamask-controller.jsperpsStreamUpdatenotifications; track per-connection subscriber count viaperpsSubscriberChangeui/index.jsperpsStreamUpdate→PerpsStreamManager.handleBackgroundUpdate()getPerpsController.tsPerpsControllerinstantiation entirely — nonew PerpsController(), no messenger, no streaming controllercreatePerpsControllerFacade.tsPerpsStreamManager.tsprices/orderBookchannels;handleBackgroundUpdate()for all 6 channels; initial RESTconnectFnfor positions/orders/account; eager cache clear on account switchCandleStreamChannel.tspushFromBackground()usePerpsLivePrices/usePerpsLiveOrderBookusePerpsChannelpatternusePerpsStreamManagerKey Properties
PerpsDataChannel.pushData()perpsSubscriberCount > 0receive stream dataTests
193 tests passing across 12 suites. All perps provider and hook tests updated to match new architecture.
Changelog
CHANGELOG entry: null
Related
Note
High Risk
High risk because it rewires the Perps streaming lifecycle and introduces new background→UI notification plumbing in
metamask-controller.js, so regressions could break live prices/order book/candles or leave subscriptions active unexpectedly.Overview
Perps streaming is moved to the background process. A new per-connection
PerpsStreamBridgesubscribes the backgroundPerpsControllerto positions/orders/account and dynamically switches prices/orderBook/candles streams, emittingperpsStreamUpdatenotifications only when the UI reports active subscribers.The UI now routes
perpsStreamUpdateinui/index.jsintoPerpsStreamManager.handleBackgroundUpdate().PerpsStreamManager,CandleStreamChannel, and stream hooks/pages are refactored to stop using a UI-side streaming controller; they instead (1) fetch initial snapshots via background RPC (perpsGetPositions,perpsGetAccountState, etc.), (2) request background streaming viaperpsActivateStreaming, and (3) consume pushed updates from the manager.Perps routing/provider wiring is adjusted to lazy-load a new
PerpsLayoutthat wraps all Perps routes withPerpsControllerProvider, removingPerpsRouteWrapper. Misc cleanup includes removing mock-based Perps exports/usages in UI components/tests and updating affected tests/baselines.Written by Cursor Bugbot for commit 11b8756. This will update automatically on new commits. Configure here.