@nevuamarkets/poly-websockets 0.0.1 → 0.0.2
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 +21 -81
- package/package.json +1 -1
- package/src/types/PolymarketWebSocket.ts +1 -1
package/README.md
CHANGED
|
@@ -10,11 +10,11 @@ npm install poly-websockets
|
|
|
10
10
|
|
|
11
11
|
## Features
|
|
12
12
|
|
|
13
|
-
- 🔄 **Automatic Connection Management**: Handles WebSocket connections, reconnections, and cleanup for grouped subscriptions
|
|
14
|
-
- 📊 **Real-time
|
|
15
|
-
- 🎯 **
|
|
13
|
+
- 🔄 **Automatic Connection Management**: Handles WebSocket connections, reconnections, and cleanup for grouped assetId (i.e. clobTokenId) subscriptions
|
|
14
|
+
- 📊 **Real-time Market Updates**: Get `book` , `price_change`, `tick_size_change` and `last_trade_price` real-time market events from Polymarket WSS
|
|
15
|
+
- 🎯 **Derived Future Price Event**: Implements Polymarket's [price calculation logic](https://docs.polymarket.com/polymarket-learn/trading/how-are-prices-calculated#future-price) (midpoint vs last trade price based on spread)
|
|
16
16
|
- 🚦 **Rate Limiting**: Built-in rate limiting to respect Polymarket API limits
|
|
17
|
-
- 🔗 **Group Management**: Efficiently manages multiple asset subscriptions across connection groups
|
|
17
|
+
- 🔗 **Group Management**: Efficiently manages multiple asset subscriptions across connection groups **without losing events** when subscribing / unsubscribing assets.
|
|
18
18
|
- 💪 **TypeScript Support**: Full TypeScript definitions for all events and handlers
|
|
19
19
|
|
|
20
20
|
## Quick Start
|
|
@@ -64,14 +64,7 @@ new WSSubscriptionManager(handlers: WebSocketHandlers, options?: SubscriptionMan
|
|
|
64
64
|
- `options` - Optional configuration object:
|
|
65
65
|
- `maxMarketsPerWS?: number` - Maximum assets per WebSocket connection (default: 100)
|
|
66
66
|
- `reconnectAndCleanupIntervalMs?: number` - Interval for reconnection attempts (default: 10s)
|
|
67
|
-
- `burstLimiter?: Bottleneck` - Custom rate limiter instance
|
|
68
|
-
|
|
69
|
-
**Connection Management:**
|
|
70
|
-
The WSSubscriptionManager automatically:
|
|
71
|
-
- Groups asset subscriptions into efficient WebSocket connections
|
|
72
|
-
- Handles reconnections when connections drop
|
|
73
|
-
- Manages connection lifecycle and cleanup
|
|
74
|
-
- Balances load across multiple WebSocket groups
|
|
67
|
+
- `burstLimiter?: Bottleneck` - Custom rate limiter instance. If none is provided, one will be created and used internally in the component.
|
|
75
68
|
|
|
76
69
|
#### Methods
|
|
77
70
|
|
|
@@ -84,7 +77,7 @@ Adds new asset subscriptions. The manager will:
|
|
|
84
77
|
|
|
85
78
|
##### `removeSubscriptions(assetIds: string[]): Promise<void>`
|
|
86
79
|
|
|
87
|
-
Removes asset subscriptions. Connections are kept alive to avoid missing events
|
|
80
|
+
Removes asset subscriptions. **Connections are kept alive to avoid missing events**, and unused groups are cleaned up during the next reconnection cycle.
|
|
88
81
|
|
|
89
82
|
##### `clearState(): Promise<void>`
|
|
90
83
|
|
|
@@ -105,7 +98,7 @@ interface WebSocketHandlers {
|
|
|
105
98
|
onPriceChange?: (events: PriceChangeEvent[]) => Promise<void>;
|
|
106
99
|
onTickSizeChange?: (events: TickSizeChangeEvent[]) => Promise<void>;
|
|
107
100
|
|
|
108
|
-
//
|
|
101
|
+
// Derived polymarket price update event
|
|
109
102
|
onPolymarketPriceUpdate?: (events: PolymarketPriceUpdateEvent[]) => Promise<void>;
|
|
110
103
|
|
|
111
104
|
// Connection lifecycle events
|
|
@@ -117,24 +110,22 @@ interface WebSocketHandlers {
|
|
|
117
110
|
|
|
118
111
|
#### Key Event Types
|
|
119
112
|
|
|
120
|
-
**PolymarketPriceUpdateEvent** (Recommended)
|
|
121
|
-
- Aggregated price update following Polymarket's display logic
|
|
122
|
-
- Uses midpoint when spread < $0.10, otherwise uses last trade price
|
|
123
|
-
- Includes full order book context
|
|
124
|
-
|
|
125
113
|
**BookEvent**
|
|
126
|
-
-
|
|
127
|
-
- Triggered on significant order book changes
|
|
114
|
+
- See // https://docs.polymarket.com/developers/CLOB/websocket/market-channel#book-message
|
|
128
115
|
|
|
129
116
|
**PriceChangeEvent**
|
|
130
|
-
-
|
|
131
|
-
|
|
117
|
+
- See https://docs.polymarket.com/developers/CLOB/websocket/market-channel#price-change-message
|
|
118
|
+
|
|
119
|
+
**onTickSizeChange**
|
|
120
|
+
- See https://docs.polymarket.com/developers/CLOB/websocket/market-channel#tick-size-change-message
|
|
132
121
|
|
|
133
122
|
**LastTradePriceEvent**
|
|
134
|
-
-
|
|
135
|
-
- Includes trade side, size, and price
|
|
123
|
+
- Currently undocumented, but is emitted when a trade occurs
|
|
136
124
|
|
|
137
|
-
|
|
125
|
+
**PolymarketPriceUpdateEvent**
|
|
126
|
+
- Derived price update following Polymarket's display logic
|
|
127
|
+
- Uses midpoint when spread <= $0.10, otherwise uses last trade price
|
|
128
|
+
- Includes full order book context
|
|
138
129
|
|
|
139
130
|
### Custom Rate Limiting
|
|
140
131
|
|
|
@@ -142,10 +133,10 @@ interface WebSocketHandlers {
|
|
|
142
133
|
import Bottleneck from 'bottleneck';
|
|
143
134
|
|
|
144
135
|
const customLimiter = new Bottleneck({
|
|
145
|
-
reservoir:
|
|
146
|
-
reservoirRefreshAmount:
|
|
136
|
+
reservoir: 10,
|
|
137
|
+
reservoirRefreshAmount: 10,
|
|
147
138
|
reservoirRefreshInterval: 1000,
|
|
148
|
-
maxConcurrent:
|
|
139
|
+
maxConcurrent: 10
|
|
149
140
|
});
|
|
150
141
|
|
|
151
142
|
const manager = new WSSubscriptionManager(handlers, {
|
|
@@ -153,55 +144,6 @@ const manager = new WSSubscriptionManager(handlers, {
|
|
|
153
144
|
});
|
|
154
145
|
```
|
|
155
146
|
|
|
156
|
-
### Connection Group Configuration
|
|
157
|
-
|
|
158
|
-
```typescript
|
|
159
|
-
const manager = new WSSubscriptionManager(handlers, {
|
|
160
|
-
maxMarketsPerWS: 50, // Smaller groups for more granular control
|
|
161
|
-
reconnectAndCleanupIntervalMs: 5000 // More frequent reconnection checks
|
|
162
|
-
});
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
### Handling All Event Types
|
|
166
|
-
|
|
167
|
-
```typescript
|
|
168
|
-
const comprehensiveHandlers: WebSocketHandlers = {
|
|
169
|
-
onPolymarketPriceUpdate: async (events) => {
|
|
170
|
-
// Primary price updates for UI display
|
|
171
|
-
events.forEach(event => {
|
|
172
|
-
updatePriceDisplay(event.asset_id, event.price);
|
|
173
|
-
});
|
|
174
|
-
},
|
|
175
|
-
|
|
176
|
-
onBook: async (events) => {
|
|
177
|
-
// Order book depth for trading interfaces
|
|
178
|
-
events.forEach(event => {
|
|
179
|
-
updateOrderBook(event.asset_id, event.bids, event.asks);
|
|
180
|
-
});
|
|
181
|
-
},
|
|
182
|
-
|
|
183
|
-
onLastTradePrice: async (events) => {
|
|
184
|
-
// Real-time trade feed
|
|
185
|
-
events.forEach(event => {
|
|
186
|
-
logTrade(event.asset_id, event.price, event.size, event.side);
|
|
187
|
-
});
|
|
188
|
-
},
|
|
189
|
-
|
|
190
|
-
onWSOpen: async (groupId, assetIds) => {
|
|
191
|
-
console.log(`Connected group ${groupId} with ${assetIds.length} assets`);
|
|
192
|
-
},
|
|
193
|
-
|
|
194
|
-
onWSClose: async (groupId, code, reason) => {
|
|
195
|
-
console.log(`Disconnected group ${groupId}: ${reason} (${code})`);
|
|
196
|
-
},
|
|
197
|
-
|
|
198
|
-
onError: async (error) => {
|
|
199
|
-
console.error('WebSocket error:', error);
|
|
200
|
-
// Implement your error handling/alerting logic
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
```
|
|
204
|
-
|
|
205
147
|
## Examples
|
|
206
148
|
|
|
207
149
|
Check the [examples](./examples) folder for complete working examples including:
|
|
@@ -211,10 +153,8 @@ Check the [examples](./examples) folder for complete working examples including:
|
|
|
211
153
|
|
|
212
154
|
## Error Handling
|
|
213
155
|
|
|
214
|
-
The library includes
|
|
156
|
+
The library includes error handling:
|
|
215
157
|
- Automatic reconnection on connection drops
|
|
216
|
-
- Rate limiting to prevent API blocking
|
|
217
|
-
- Graceful handling of malformed messages
|
|
218
158
|
- User-defined error callbacks for custom handling
|
|
219
159
|
|
|
220
160
|
## Rate Limits
|
package/package.json
CHANGED
|
@@ -221,7 +221,7 @@ export type WebSocketHandlers = {
|
|
|
221
221
|
and denotes the probability of an event happening. Read more about it here:
|
|
222
222
|
https://docs.polymarket.com/polymarket-learn/trading/how-are-prices-calculated#future-price
|
|
223
223
|
|
|
224
|
-
This is
|
|
224
|
+
This is a derived event that is not emmited by the Polymarket WebSocket directly.
|
|
225
225
|
*/
|
|
226
226
|
onPolymarketPriceUpdate?: (events: PolymarketPriceUpdateEvent[]) => Promise<void>;
|
|
227
227
|
|