Open
Conversation
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Create a WebSocket server handler that accepts client connections, manages subscriptions per metric stream, and broadcasts updates on data change. Support graceful disconnects.
Generated by OpenDev AI
The implementation is split into three concerns:
src/types/metrics.ts— Shared wire-protocol types (MetricData,ClientMessage,ServerMessage) kept in their own file so both the server and tests can import them without circular dependencies.src/ws-server.ts— Two exported classes:MetricBroadcaster: pure subscription-management logic (no I/O). Maintains aMap<stream, Set<TrackedClient>>and a reverseSet<string>on each client so both subscribe/unsubscribe and full removeClient are O(n) in subscribers rather than O(streams × subscribers).MetricWebSocketServer: wraps anhttp.Server+WebSocketServerso the port is injected atlisten()time (port 0 works for tests). Handles the JSON wire protocol (subscribe / unsubscribe / ping), enforces a 64 KB message cap, validates stream names against a strict regex to block path-traversal strings, and attaches to NATS via thedev.metrics.>wildcard subject. A 30-second ping/pong heartbeat loop terminates clients whose TCP connections dropped silently. Graceful shutdown sends a1001 Going Awayclose frame before tearing down the server.package.json— Addsws(^8.17.0) todependenciesand@types/wstodevDependencies, the only new dependency required.Key design decisions:
MetricBroadcaster/MetricWebSocketServersplit makes unit-testing the subscription logic trivial with plain mock objects, while integration tests exercise real WebSocket connections on an OS-assigned port.dev.metrics.<stream>mirrors the existingdev.agent.*anddev.deploy.*naming conventions in the codebase..unref()'d so it never prevents the Node process from exiting cleanly in tests or during shutdown.