@binance/derivatives-trading-portfolio-margin 4.0.2 → 5.0.1

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
@@ -6,11 +6,13 @@
6
6
  [![npm Downloads](https://img.shields.io/npm/dm/@binance/derivatives-trading-portfolio-margin.svg)](https://www.npmjs.com/package/@binance/derivatives-trading-portfolio-margin)
7
7
  ![Node.js Version](https://img.shields.io/badge/Node.js-%3E=22.12.0-brightgreen)
8
8
  [![Known Vulnerabilities](https://snyk.io/test/github/binance/binance-connector-js/badge.svg)](https://snyk.io/test/github/binance/binance-connector-js)
9
+ [![Docs](https://img.shields.io/badge/docs-online-blue?style=flat-square)](https://binance.github.io/binance-connector-js/modules/_binance_derivatives-trading-portfolio-margin.html)
9
10
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
10
11
 
11
12
  This is a client library for the Binance Derivatives Trading Portfolio Margin API, enabling developers to interact programmatically with Binance's API to suit their derivative trading needs, through three distinct endpoints:
12
13
 
13
14
  - [REST API](./src/rest-api/rest-api.ts)
15
+ - [Websocket Stream](./src/websocket-streams/websocket-streams-connection.ts)
14
16
 
15
17
  ## Table of Contents
16
18
 
@@ -18,6 +20,7 @@ This is a client library for the Binance Derivatives Trading Portfolio Margin AP
18
20
  - [Installation](#installation)
19
21
  - [Documentation](#documentation)
20
22
  - [REST APIs](#rest-apis)
23
+ - [Websocket Streams](#websocket-streams)
21
24
  - [Testing](#testing)
22
25
  - [Migration Guide](#migration-guide)
23
26
  - [Contributing](#contributing)
@@ -156,6 +159,114 @@ const client = new DerivativesTradingPortfolioMargin({ configurationRestAPI });
156
159
 
157
160
  If `basePath` is not provided, it defaults to `https://papi.binance.com`.
158
161
 
162
+ ### Websocket Streams
163
+
164
+ WebSocket Streams in `derivatives-trading-portfolio-margin` is used for subscribing to user data streams. Use the [websocket-streams](./src/websocket-streams/websocket-streams.ts) module to interact with it.
165
+
166
+ #### Configuration Options
167
+
168
+ The WebSocket Streams API supports the following advanced configuration options:
169
+
170
+ - `reconnectDelay`: Specify the delay between reconnection attempts (default: 5000 ms).
171
+ - `compression`: Enable or disable compression for WebSocket messages (default: true).
172
+ - `agent`: Customize the WebSocket agent for advanced configurations.
173
+ - `mode`: Choose between `single` and `pool` connection modes.
174
+ - `single`: A single WebSocket connection.
175
+ - `pool`: A pool of WebSocket connections.
176
+ - `poolSize`: Define the number of WebSocket connections in pool mode.
177
+
178
+ #### Subscribe to User Data Streams
179
+
180
+ You can consume the user data stream, which sends account-level events such as account and order updates. First create a listen-key via REST API; then:
181
+
182
+ ```typescript
183
+ import { DerivativesTradingPortfolioMargin, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL } from '@binance/derivatives-trading-portfolio-margin';
184
+
185
+ const configurationWebsocketStreams = {
186
+ wsURL: DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL,
187
+ };
188
+ const client = new DerivativesTradingPortfolioMargin({ configurationWebsocketStreams });
189
+
190
+ client.websocketStreams
191
+ .connect()
192
+ .then((connection) => {
193
+ const stream = connection.userData('listenKey');
194
+ stream.on('message', (data) => {
195
+ switch (data.e) {
196
+ case 'ACCOUNT_UPDATE':
197
+ console.log('account update stream', data);
198
+ break;
199
+ case 'ORDER_TRADE_UPDATE':
200
+ console.log('order trade update stream', data);
201
+ break;
202
+ // …handle other variants…
203
+ default:
204
+ console.log('unknown stream', data);
205
+ break;
206
+ }
207
+ });
208
+ })
209
+ .catch((err) => console.error(err));
210
+ ```
211
+
212
+ #### Unsubscribing from Streams
213
+
214
+ You can unsubscribe from the user data streams using the `unsubscribe` method. This is useful for managing active subscriptions without closing the connection.
215
+
216
+ ```typescript
217
+ import { DerivativesTradingPortfolioMargin, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL } from '@binance/derivatives-trading-portfolio-margin';
218
+
219
+ const configurationWebsocketStreams = {
220
+ wsURL: DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL,
221
+ };
222
+ const client = new DerivativesTradingPortfolioMargin({ configurationWebsocketStreams });
223
+
224
+ client.websocketStreams
225
+ .connect()
226
+ .then((connection) => {
227
+ const stream = connection.userData('listenKey');
228
+ stream.on('message', (data) => {
229
+ switch (data.e) {
230
+ case 'ACCOUNT_UPDATE':
231
+ console.log('account update stream', data);
232
+ break;
233
+ case 'ORDER_TRADE_UPDATE':
234
+ console.log('order trade update stream', data);
235
+ break;
236
+ // …handle other variants…
237
+ default:
238
+ console.log('unknown stream', data);
239
+ break;
240
+ }
241
+ });
242
+
243
+ setTimeout(() => {
244
+ stream.unsubscribe();
245
+ console.log('Unsubscribed from user data streams');
246
+ }, 10000);
247
+ })
248
+ .catch((err) => console.error(err));
249
+ ```
250
+
251
+ #### Testnet
252
+
253
+ Websocket Streams also support a testnet environment for development and testing. Update the `wsURL` in your configuration:
254
+
255
+ ```typescript
256
+ import { DerivativesTradingPortfolioMargin, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_TESTNET_URL } from '@binance/derivatives-trading-portfolio-margin';
257
+
258
+ const configurationWebsocketStreams = {
259
+ wsURL: DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_TESTNET_URL,
260
+ };
261
+ const client = new DerivativesTradingPortfolioMargin({ configurationWebsocketStreams });
262
+ ```
263
+
264
+ If `wsURL` is not provided, it defaults to `wss://fstream.binance.com/pm`.
265
+
266
+ ### Automatic Connection Renewal
267
+
268
+ The WebSocket connection is automatically renewed for both WebSocket API and WebSocket Streams connections, before the 24 hours expiration of the API key. This ensures continuous connectivity.
269
+
159
270
  ## Testing
160
271
 
161
272
  To run the tests: