feat(): examples for v5 websockets with and without auth
This commit is contained in:
103
examples/ws-private-v5.ts
Normal file
103
examples/ws-private-v5.ts
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-empty-function */
|
||||||
|
import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src';
|
||||||
|
|
||||||
|
// or
|
||||||
|
// import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from 'bybit-api';
|
||||||
|
|
||||||
|
// Create & inject a custom logger to disable the silly logging level (empty function)
|
||||||
|
const logger = {
|
||||||
|
...DefaultLogger,
|
||||||
|
silly: () => {},
|
||||||
|
};
|
||||||
|
|
||||||
|
const key = process.env.API_KEY;
|
||||||
|
const secret = process.env.API_SECRET;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare an instance of the WebSocket client. This client handles all aspects of connectivity for you:
|
||||||
|
* - Connections are opened when you subscribe to topics
|
||||||
|
* - If key & secret are provided, authentication is handled automatically
|
||||||
|
* - If you subscribe to topics from different v5 products (e.g. spot and linear perps),
|
||||||
|
* subscription events are automatically routed to the different ws endpoints on bybit's side
|
||||||
|
* - Heartbeats/ping/pong/reconnects are all handled automatically.
|
||||||
|
* If a connection drops, the client will clean it up, respawn a fresh connection and resubscribe for you.
|
||||||
|
*/
|
||||||
|
const wsClient = new WebsocketClient(
|
||||||
|
{
|
||||||
|
key: key,
|
||||||
|
secret: secret,
|
||||||
|
market: 'v5',
|
||||||
|
testnet: true,
|
||||||
|
},
|
||||||
|
logger
|
||||||
|
);
|
||||||
|
|
||||||
|
wsClient.on('update', (data) => {
|
||||||
|
console.log('raw message received ', JSON.stringify(data));
|
||||||
|
// console.log('raw message received ', JSON.stringify(data, null, 2));
|
||||||
|
});
|
||||||
|
|
||||||
|
wsClient.on('open', (data) => {
|
||||||
|
console.log('connection opened open:', data.wsKey);
|
||||||
|
});
|
||||||
|
wsClient.on('response', (data) => {
|
||||||
|
console.log('log response: ', JSON.stringify(data, null, 2));
|
||||||
|
});
|
||||||
|
wsClient.on('reconnect', ({ wsKey }) => {
|
||||||
|
console.log('ws automatically reconnecting.... ', wsKey);
|
||||||
|
});
|
||||||
|
wsClient.on('reconnected', (data) => {
|
||||||
|
console.log('ws has reconnected ', data?.wsKey);
|
||||||
|
});
|
||||||
|
// wsClient.on('error', (data) => {
|
||||||
|
// console.error('ws exception: ', data);
|
||||||
|
// });
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For private V5 topics, us the subscribeV5() method on the ws client or use the original subscribe() method.
|
||||||
|
*
|
||||||
|
* Note: for private endpoints the "category" field is ignored since there is only one private endpoint
|
||||||
|
* (compared to one public one per category).
|
||||||
|
* The "category" is only needed for public topics since bybit has one endpoint for public events per category.
|
||||||
|
*/
|
||||||
|
|
||||||
|
wsClient.subscribeV5('position', 'linear');
|
||||||
|
wsClient.subscribeV5('execution', 'linear');
|
||||||
|
wsClient.subscribeV5(['order', 'wallet', 'greek'], 'linear');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The following has the same effect as above, since there's only one private endpoint for V5 account topics:
|
||||||
|
*/
|
||||||
|
// wsClient.subscribe('position');
|
||||||
|
// wsClient.subscribe('execution');
|
||||||
|
// wsClient.subscribe(['order', 'wallet', 'greek']);
|
||||||
|
|
||||||
|
// To unsubscribe from topics (after a 5 second delay, in this example):
|
||||||
|
// setTimeout(() => {
|
||||||
|
// console.log('unsubscribing');
|
||||||
|
// wsClient.unsubscribeV5('execution', 'linear');
|
||||||
|
// }, 5 * 1000);
|
||||||
|
|
||||||
|
// Topics are tracked per websocket type
|
||||||
|
// Get a list of subscribed topics (e.g. for public v3 spot topics) (after a 5 second delay)
|
||||||
|
setTimeout(() => {
|
||||||
|
const activePrivateTopics = wsClient
|
||||||
|
.getWsStore()
|
||||||
|
.getTopics(WS_KEY_MAP.v5Private);
|
||||||
|
console.log('Active private v5 topics: ', activePrivateTopics);
|
||||||
|
|
||||||
|
const activePublicLinearTopics = wsClient
|
||||||
|
.getWsStore()
|
||||||
|
.getTopics(WS_KEY_MAP.v5LinearPublic);
|
||||||
|
console.log('Active public linear v5 topics: ', activePublicLinearTopics);
|
||||||
|
|
||||||
|
const activePublicSpotTopis = wsClient
|
||||||
|
.getWsStore()
|
||||||
|
.getTopics(WS_KEY_MAP.v5SpotPublic);
|
||||||
|
console.log('Active public spot v5 topics: ', activePublicSpotTopis);
|
||||||
|
|
||||||
|
const activePublicOptionsTopics = wsClient
|
||||||
|
.getWsStore()
|
||||||
|
.getTopics(WS_KEY_MAP.v5OptionPublic);
|
||||||
|
console.log('Active public option v5 topics: ', activePublicOptionsTopics);
|
||||||
|
}, 5 * 1000);
|
||||||
@@ -6,7 +6,6 @@ import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src';
|
|||||||
// or
|
// or
|
||||||
// import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from 'bybit-api';
|
// import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from 'bybit-api';
|
||||||
|
|
||||||
(async () => {
|
|
||||||
const logger = {
|
const logger = {
|
||||||
...DefaultLogger,
|
...DefaultLogger,
|
||||||
silly: () => {},
|
silly: () => {},
|
||||||
@@ -69,4 +68,3 @@ import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src';
|
|||||||
'user.order.contractAccount',
|
'user.order.contractAccount',
|
||||||
'user.wallet.contractAccount',
|
'user.wallet.contractAccount',
|
||||||
]);
|
]);
|
||||||
})();
|
|
||||||
|
|||||||
116
examples/ws-public-v5.ts
Normal file
116
examples/ws-public-v5.ts
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src';
|
||||||
|
|
||||||
|
// or
|
||||||
|
// import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from 'bybit-api';
|
||||||
|
|
||||||
|
const logger = {
|
||||||
|
...DefaultLogger,
|
||||||
|
silly: (...params) => console.log('silly', ...params),
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare an instance of the WebSocket client. This client handles all aspects of connectivity for you:
|
||||||
|
* - Connections are opened when you subscribe to topics
|
||||||
|
* - If key & secret are provided, authentication is handled automatically
|
||||||
|
* - If you subscribe to topics from different v5 products (e.g. spot and linear perps),
|
||||||
|
* subscription events are automatically routed to the different ws endpoints on bybit's side
|
||||||
|
* - Heartbeats/ping/pong/reconnects are all handled automatically.
|
||||||
|
* If a connection drops, the client will clean it up, respawn a fresh connection and resubscribe for you.
|
||||||
|
*/
|
||||||
|
const wsClient = new WebsocketClient(
|
||||||
|
{
|
||||||
|
// key: key,
|
||||||
|
// secret: secret,
|
||||||
|
market: 'v5',
|
||||||
|
},
|
||||||
|
logger
|
||||||
|
);
|
||||||
|
|
||||||
|
wsClient.on('update', (data) => {
|
||||||
|
console.log('raw message received ', JSON.stringify(data));
|
||||||
|
// console.log('raw message received ', JSON.stringify(data, null, 2));
|
||||||
|
});
|
||||||
|
|
||||||
|
wsClient.on('open', (data) => {
|
||||||
|
console.log('connection opened open:', data.wsKey);
|
||||||
|
});
|
||||||
|
wsClient.on('response', (data) => {
|
||||||
|
console.log('log response: ', JSON.stringify(data, null, 2));
|
||||||
|
});
|
||||||
|
wsClient.on('reconnect', ({ wsKey }) => {
|
||||||
|
console.log('ws automatically reconnecting.... ', wsKey);
|
||||||
|
});
|
||||||
|
wsClient.on('reconnected', (data) => {
|
||||||
|
console.log('ws has reconnected ', data?.wsKey);
|
||||||
|
});
|
||||||
|
// wsClient.on('error', (data) => {
|
||||||
|
// console.error('ws exception: ', data);
|
||||||
|
// });
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For public V5 topics, use the subscribeV5 method and include the API category this topic is for.
|
||||||
|
* Category is required, since each category has a different websocket endpoint.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Linear v5
|
||||||
|
// -> Just one topic per call
|
||||||
|
// wsClient.subscribeV5('orderbook.50.BTCUSDT', 'linear');
|
||||||
|
|
||||||
|
// -> Or multiple topics in one call
|
||||||
|
// wsClient.subscribeV5(
|
||||||
|
// ['orderbook.50.BTCUSDT', 'orderbook.50.ETHUSDT'],
|
||||||
|
// 'linear'
|
||||||
|
// );
|
||||||
|
|
||||||
|
// Inverse v5
|
||||||
|
// wsClient.subscribeV5('orderbook.50.BTCUSD', 'inverse');
|
||||||
|
|
||||||
|
// Spot v5
|
||||||
|
// wsClient.subscribeV5('orderbook.50.BTCUSDT', 'spot');
|
||||||
|
|
||||||
|
// Option v5
|
||||||
|
// wsClient.subscribeV5('publicTrade.BTC', 'option');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For private V5 topics, just call the same subscribeV5() method on the ws client or use the original subscribe() method.
|
||||||
|
*
|
||||||
|
* Note: for private endpoints the "category" field is ignored since there is only one private endpoint
|
||||||
|
* (compared to one public one per category)
|
||||||
|
*/
|
||||||
|
|
||||||
|
wsClient.subscribeV5('position', 'linear');
|
||||||
|
wsClient.subscribeV5('execution', 'linear');
|
||||||
|
wsClient.subscribeV5(['order', 'wallet', 'greek'], 'linear');
|
||||||
|
|
||||||
|
// Other example topics
|
||||||
|
|
||||||
|
// To unsubscribe from topics (after a 5 second delay, in this example):
|
||||||
|
// setTimeout(() => {
|
||||||
|
// console.log('unsubscribing');
|
||||||
|
// wsClient.unsubscribeV5('orderbook.50.BTCUSDT', 'linear);
|
||||||
|
// }, 5 * 1000);
|
||||||
|
|
||||||
|
// Topics are tracked per websocket type
|
||||||
|
// Get a list of subscribed topics (e.g. for public v3 spot topics) (after a 5 second delay)
|
||||||
|
setTimeout(() => {
|
||||||
|
const activePrivateTopics = wsClient
|
||||||
|
.getWsStore()
|
||||||
|
.getTopics(WS_KEY_MAP.v5Private);
|
||||||
|
|
||||||
|
console.log('Active private v5 topics: ', activePrivateTopics);
|
||||||
|
|
||||||
|
const activePublicLinearTopics = wsClient
|
||||||
|
.getWsStore()
|
||||||
|
.getTopics(WS_KEY_MAP.v5LinearPublic);
|
||||||
|
console.log('Active public linear v5 topics: ', activePublicLinearTopics);
|
||||||
|
|
||||||
|
const activePublicSpotTopis = wsClient
|
||||||
|
.getWsStore()
|
||||||
|
.getTopics(WS_KEY_MAP.v5SpotPublic);
|
||||||
|
console.log('Active public spot v5 topics: ', activePublicSpotTopis);
|
||||||
|
|
||||||
|
const activePublicOptionsTopics = wsClient
|
||||||
|
.getWsStore()
|
||||||
|
.getTopics(WS_KEY_MAP.v5OptionPublic);
|
||||||
|
console.log('Active public option v5 topics: ', activePublicOptionsTopics);
|
||||||
|
}, 5 * 1000);
|
||||||
@@ -3,7 +3,6 @@ import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src';
|
|||||||
// or
|
// or
|
||||||
// import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from 'bybit-api';
|
// import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from 'bybit-api';
|
||||||
|
|
||||||
(async () => {
|
|
||||||
const logger = {
|
const logger = {
|
||||||
...DefaultLogger,
|
...DefaultLogger,
|
||||||
silly: (...params) => console.log('silly', ...params),
|
silly: (...params) => console.log('silly', ...params),
|
||||||
@@ -170,4 +169,3 @@ import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src';
|
|||||||
.getTopics(WS_KEY_MAP.spotV3Private);
|
.getTopics(WS_KEY_MAP.spotV3Private);
|
||||||
console.log('private spot topics: ', privateSpotTopics);
|
console.log('private spot topics: ', privateSpotTopics);
|
||||||
}, 5 * 1000);
|
}, 5 * 1000);
|
||||||
})();
|
|
||||||
|
|||||||
Reference in New Issue
Block a user