@@ -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} ) ( ) ;
0 commit comments