feat(): examples for v5 websockets with and without auth

This commit is contained in:
tiagosiebler
2023-02-27 11:37:49 +00:00
parent d6534a438c
commit a0dc9c811d
4 changed files with 424 additions and 209 deletions

View File

@@ -6,67 +6,65 @@ import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src';
// or
// import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from 'bybit-api';
(async () => {
const logger = {
...DefaultLogger,
silly: () => {},
};
const logger = {
...DefaultLogger,
silly: () => {},
};
const key = process.env.API_KEY;
const secret = process.env.API_SECRET;
const key = process.env.API_KEY;
const secret = process.env.API_SECRET;
// USDT Perps:
// const market = 'linear';
// Inverse Perp
// const market = 'inverse';
// const market = 'spotv3';
// Contract v3
const market = 'contractUSDT';
// const market = 'contractInverse';
// USDT Perps:
// const market = 'linear';
// Inverse Perp
// const market = 'inverse';
// const market = 'spotv3';
// Contract v3
const market = 'contractUSDT';
// const market = 'contractInverse';
// Note: the WebsocketClient defaults to testnet. Set `livenet: true` to use live markets.
const wsClient = new WebsocketClient(
{
key: key,
secret: secret,
market: market,
// testnet: true,
restOptions: {
// enable_time_sync: true,
},
// Note: the WebsocketClient defaults to testnet. Set `livenet: true` to use live markets.
const wsClient = new WebsocketClient(
{
key: key,
secret: secret,
market: market,
// testnet: true,
restOptions: {
// enable_time_sync: true,
},
logger
);
},
logger
);
wsClient.on('update', (data) => {
console.log('raw message received ', JSON.stringify(data, null, 2));
});
wsClient.on('update', (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('ws 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);
});
wsClient.on('open', (data) => {
console.log('connection opened open:', data.wsKey);
});
wsClient.on('response', (data) => {
console.log('ws 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);
});
// subscribe to private endpoints
// check the api docs in your api category to see the available topics
// wsClient.subscribe(['position', 'execution', 'order', 'wallet']);
// subscribe to private endpoints
// check the api docs in your api category to see the available topics
// wsClient.subscribe(['position', 'execution', 'order', 'wallet']);
// Contract v3
wsClient.subscribe([
'user.position.contractAccount',
'user.execution.contractAccount',
'user.order.contractAccount',
'user.wallet.contractAccount',
]);
})();
// Contract v3
wsClient.subscribe([
'user.position.contractAccount',
'user.execution.contractAccount',
'user.order.contractAccount',
'user.wallet.contractAccount',
]);