Skip to content

Commit 030e6a4

Browse files
committed
feat(v3.0.0-beta.13): add methods to unsubscribe user data streams (close connection), add example with demonstrations for each, fix(): insolated margin reconnection workflow, fix(): REST sign encoding with ed25519 keys
1 parent 32f652d commit 030e6a4

File tree

7 files changed

+303
-164
lines changed

7 files changed

+303
-164
lines changed

examples/WebSockets/ws-userdata-listenkey.ts

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
WebsocketClient,
1919
WsUserDataEvents,
2020
} from '../../src/index';
21+
import { WsConnectionStateEnum } from '../../src/util/websockets/WsStore.types';
2122

2223
(async () => {
2324
const key = process.env.API_KEY_COM || 'APIKEY';
@@ -154,13 +155,86 @@ import {
154155
*
155156
* Once subscribed, you don't need to do anything else. Listen-key keep-alive, refresh, reconnects, etc are all automatically handled by the SDK.
156157
*/
158+
159+
// Example 1: Spot, by default, routes to the "main" wss domain "wss://stream.binance.com:9443".
160+
// No parameters needed, just call the subscribe function.
157161
wsClient.subscribeSpotUserDataStream();
158-
// wsClient.subscribeCrossMarginUserDataStream();
159-
// wsClient.subscribeIsolatedMarginUserDataStream('BTCUSDT');
160-
// wsClient.subscribeUsdFuturesUserDataStream();
161-
162-
// setTimeout(() => {
163-
// console.log('killing all connections');
164-
// wsClient.closeAll();
165-
// }, 1000 * 15);
162+
163+
// // Example 2: Optional: subscribe to spot via other wss domains
164+
wsClient.subscribeSpotUserDataStream('main2'); // routed to "wss://stream.binance.com:443"
165+
166+
// // Example 3: cross margin
167+
wsClient.subscribeCrossMarginUserDataStream();
168+
169+
// Example 4: isolated margin
170+
wsClient.subscribeIsolatedMarginUserDataStream('BTCUSDC');
171+
172+
/**
173+
* Futures
174+
*/
175+
176+
// Example 5: usdm futures
177+
wsClient.subscribeUsdFuturesUserDataStream();
178+
179+
// Example 6: coinm futures
180+
wsClient.subscribeCoinFuturesUserDataStream();
181+
182+
// Example 7: portfolio margin
183+
// wsClient.subscribePortfolioMarginUserDataStream();
184+
185+
// Example 8: portfolio margin pro
186+
// wsClient.subscribePortfolioMarginUserDataStream('portfolioMarginProUserData');
187+
188+
// after 15 seconds, kill user data connections one by one (or all at once)
189+
setTimeout(() => {
190+
// console.log('killing all connections at once');
191+
// wsClient.closeAll();
192+
193+
// or:
194+
console.log('killing individual connections');
195+
196+
try {
197+
// console.log('killing all connections');
198+
// wsClient.closeAll();
199+
// Example 1:
200+
wsClient.unsubscribeSpotUserDataStream();
201+
// Example 2: use the wsKey to route to another domain
202+
wsClient.unsubscribeSpotUserDataStream('main2');
203+
// Example 3: cross margin
204+
wsClient.unsubscribeCrossMarginUserDataStream();
205+
// Example 4: isolated margin
206+
wsClient.unsubscribeIsolatedMarginUserDataStream('BTCUSDC');
207+
// Example 5: usdm futures
208+
wsClient.unsubscribeUsdFuturesUserDataStream();
209+
// Example 6: coinm futures
210+
wsClient.unsubscribeCoinFuturesUserDataStream();
211+
// // Example 7: portfolio margin
212+
// wsClient.unsubscribePortfolioMarginUserDataStream();
213+
// // Example 8: portfolio margin pro
214+
// wsClient.unsubscribePortfolioMarginUserDataStream(
215+
// 'portfolioMarginProUserData',
216+
// );
217+
} catch (e) {
218+
console.error('Exception trying to close a user data stream: ', e);
219+
}
220+
}, 1000 * 15);
221+
222+
// after 20 seconds, list the remaining open connections
223+
setTimeout(() => {
224+
try {
225+
console.log(
226+
'remaining connections:',
227+
wsClient
228+
.getWsStore()
229+
.getKeys()
230+
.filter(
231+
(key) =>
232+
wsClient.getWsStore().get(key)?.connectionState ===
233+
WsConnectionStateEnum.CONNECTED,
234+
),
235+
);
236+
} catch (e) {
237+
console.error('Exception trying to close a user data stream: ', e);
238+
}
239+
}, 1000 * 20);
166240
})();

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "binance",
3-
"version": "3.0.0-beta.12",
3+
"version": "3.0.0-beta.13",
44
"description": "Professional Node.js & JavaScript SDK for Binance REST APIs & WebSockets, with TypeScript & end-to-end tests.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/util/node-support.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ export async function signMessage(
2020
}
2121

2222
// fallback to ed25519
23+
// ed25519 requires b64 encoding
24+
const ed25519Method: SignEncodeMethod = 'base64';
2325
return sign(null, Buffer.from(message), {
2426
key: secret,
2527
padding: constants.RSA_PKCS1_PSS_PADDING,
2628
saltLength: constants.RSA_PSS_SALTLEN_DIGEST,
27-
}).toString(pemEncodeMethod);
29+
}).toString(ed25519Method);
2830
}
2931

3032
// fallback to hmac

src/util/websockets/user-data-stream-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class UserDataStreamManager {
118118
miscState?: MiscUserDataConnectionState,
119119
): Promise<WSConnectedResult | undefined> {
120120
const streamName = 'userData';
121-
const symbol = undefined;
121+
const symbol = miscState?.symbol;
122122

123123
this.logger.trace('subscribeGeneralUserDataStreamWithListenKey(): ', {
124124
wsKey,

src/util/websockets/websocket-util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ export function parseRawWsMessage(event: any): any {
645645
export interface MiscUserDataConnectionState {
646646
isReconnecting?: boolean;
647647
respawnAttempt?: number;
648+
symbol?: string; // isolated margin is per symbol
648649
}
649650

650651
interface WsContext {

0 commit comments

Comments
 (0)