@nevuamarkets/poly-websockets 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -90,6 +90,12 @@ Clears all subscriptions and state:
90
90
  - Closes all WebSocket connections
91
91
  - Clears the internal order book cache
92
92
 
93
+ ##### `getStatistics(): { openWebSockets: number; subscribedAssetIds: number }`
94
+
95
+ Returns statistics about the current state of the subscription manager:
96
+ - `openWebSockets`: The number of websockets that are currently in OPEN state
97
+ - `subscribedAssetIds`: The number of unique asset IDs that are currently subscribed
98
+
93
99
  ### WebSocketHandlers
94
100
 
95
101
  Interface defining event handlers for different WebSocket events.
@@ -13,6 +13,10 @@ declare class WSSubscriptionManager {
13
13
  addSubscriptions(assetIdsToAdd: string[]): Promise<void>;
14
14
  removeSubscriptions(assetIdsToRemove: string[]): Promise<void>;
15
15
  private reconnectAndCleanupGroups;
16
+ getStatistics(): {
17
+ openWebSockets: number;
18
+ subscribedAssetIds: number;
19
+ };
16
20
  private createWebSocketClient;
17
21
  }
18
22
  export { WSSubscriptionManager, WebSocketHandlers };
@@ -171,6 +171,16 @@ class WSSubscriptionManager {
171
171
  await ((_b = (_a = this.handlers).onError) === null || _b === void 0 ? void 0 : _b.call(_a, err));
172
172
  }
173
173
  }
174
+ /*
175
+ Returns statistics about the current state of the subscription manager.
176
+
177
+ Returns an object with:
178
+ - openWebSockets: The number of websockets that are currently in OPEN state
179
+ - subscribedAssetIds: The number of unique asset IDs that are currently subscribed
180
+ */
181
+ getStatistics() {
182
+ return this.groupRegistry.getStatistics();
183
+ }
174
184
  async createWebSocketClient(groupId, handlers) {
175
185
  var _a, _b;
176
186
  const group = this.groupRegistry.findGroupById(groupId);
@@ -36,6 +36,17 @@ export declare class GroupRegistry {
36
36
  * Returns the group if found, otherwise undefined.
37
37
  */
38
38
  findGroupById(groupId: string): WebSocketGroup | undefined;
39
+ /**
40
+ * Get statistics about the current state of the registry.
41
+ *
42
+ * Returns an object with:
43
+ * - openWebSockets: The number of websockets that are currently in OPEN state
44
+ * - subscribedAssetIds: The number of unique asset IDs that are currently subscribed
45
+ */
46
+ getStatistics(): {
47
+ openWebSockets: number;
48
+ subscribedAssetIds: number;
49
+ };
39
50
  /**
40
51
  * Atomically remove **all** groups from the registry and return them so the
41
52
  * caller can perform any asynchronous cleanup (closing sockets, etc.)
@@ -7,6 +7,7 @@ exports.GroupRegistry = void 0;
7
7
  const async_mutex_1 = require("async-mutex");
8
8
  const lodash_1 = __importDefault(require("lodash"));
9
9
  const uuid_1 = require("uuid");
10
+ const ws_1 = __importDefault(require("ws"));
10
11
  const WebSocketSubscriptions_1 = require("../types/WebSocketSubscriptions");
11
12
  const logger_1 = require("../logger");
12
13
  /*
@@ -85,6 +86,31 @@ class GroupRegistry {
85
86
  findGroupById(groupId) {
86
87
  return wsGroups.find(g => g.groupId === groupId);
87
88
  }
89
+ /**
90
+ * Get statistics about the current state of the registry.
91
+ *
92
+ * Returns an object with:
93
+ * - openWebSockets: The number of websockets that are currently in OPEN state
94
+ * - subscribedAssetIds: The number of unique asset IDs that are currently subscribed
95
+ */
96
+ getStatistics() {
97
+ let openWebSockets = 0;
98
+ const uniqueAssetIds = new Set();
99
+ for (const group of wsGroups) {
100
+ // Count open websockets
101
+ if (group.wsClient && group.wsClient.readyState === ws_1.default.OPEN) {
102
+ openWebSockets++;
103
+ }
104
+ // Collect unique asset IDs
105
+ for (const assetId of group.assetIds) {
106
+ uniqueAssetIds.add(assetId);
107
+ }
108
+ }
109
+ return {
110
+ openWebSockets,
111
+ subscribedAssetIds: uniqueAssetIds.size,
112
+ };
113
+ }
88
114
  /**
89
115
  * Atomically remove **all** groups from the registry and return them so the
90
116
  * caller can perform any asynchronous cleanup (closing sockets, etc.)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nevuamarkets/poly-websockets",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Plug-and-play Polymarket WebSocket price alerts",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -199,6 +199,20 @@ class WSSubscriptionManager {
199
199
  }
200
200
  }
201
201
 
202
+ /*
203
+ Returns statistics about the current state of the subscription manager.
204
+
205
+ Returns an object with:
206
+ - openWebSockets: The number of websockets that are currently in OPEN state
207
+ - subscribedAssetIds: The number of unique asset IDs that are currently subscribed
208
+ */
209
+ public getStatistics(): {
210
+ openWebSockets: number;
211
+ subscribedAssetIds: number;
212
+ } {
213
+ return this.groupRegistry.getStatistics();
214
+ }
215
+
202
216
  private async createWebSocketClient(groupId: string, handlers: WebSocketHandlers) {
203
217
  const group = this.groupRegistry.findGroupById(groupId);
204
218
 
@@ -1,6 +1,7 @@
1
1
  import { Mutex } from 'async-mutex';
2
2
  import _ from 'lodash';
3
3
  import { v4 as uuidv4 } from 'uuid';
4
+ import WebSocket from 'ws';
4
5
  import { WebSocketGroup, WebSocketStatus } from '../types/WebSocketSubscriptions';
5
6
  import { OrderBookCache } from './OrderBookCache';
6
7
  import { logger } from '../logger';
@@ -81,6 +82,38 @@ export class GroupRegistry {
81
82
  return wsGroups.find(g => g.groupId === groupId);
82
83
  }
83
84
 
85
+ /**
86
+ * Get statistics about the current state of the registry.
87
+ *
88
+ * Returns an object with:
89
+ * - openWebSockets: The number of websockets that are currently in OPEN state
90
+ * - subscribedAssetIds: The number of unique asset IDs that are currently subscribed
91
+ */
92
+ public getStatistics(): {
93
+ openWebSockets: number;
94
+ subscribedAssetIds: number;
95
+ } {
96
+ let openWebSockets = 0;
97
+ const uniqueAssetIds = new Set<string>();
98
+
99
+ for (const group of wsGroups) {
100
+ // Count open websockets
101
+ if (group.wsClient && group.wsClient.readyState === WebSocket.OPEN) {
102
+ openWebSockets++;
103
+ }
104
+
105
+ // Collect unique asset IDs
106
+ for (const assetId of group.assetIds) {
107
+ uniqueAssetIds.add(assetId);
108
+ }
109
+ }
110
+
111
+ return {
112
+ openWebSockets,
113
+ subscribedAssetIds: uniqueAssetIds.size,
114
+ };
115
+ }
116
+
84
117
  /**
85
118
  * Atomically remove **all** groups from the registry and return them so the
86
119
  * caller can perform any asynchronous cleanup (closing sockets, etc.)