-
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #383 from tiagosiebler/obspotexample
v2.9.1: add typeguard for formatted orderbook events, with example
- Loading branch information
Showing
6 changed files
with
90 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { | ||
WebsocketClient, | ||
DefaultLogger, | ||
isWsPartialBookDepthEventFormatted, | ||
getContextFromWsKey, | ||
} from '../src'; | ||
|
||
// or, with the npm package | ||
/* | ||
import { | ||
WebsocketClient, | ||
DefaultLogger, | ||
isWsFormattedTrade, | ||
} from 'binance'; | ||
*/ | ||
|
||
(async () => { | ||
const logger = { | ||
...DefaultLogger, | ||
// silly: () => {}, | ||
}; | ||
|
||
const wsClient = new WebsocketClient( | ||
{ | ||
beautify: true, | ||
}, | ||
logger, | ||
); | ||
|
||
/** | ||
* Simple example for receiving depth snapshots from spot orderbooks | ||
*/ | ||
wsClient.on('formattedMessage', (data) => { | ||
if (isWsPartialBookDepthEventFormatted(data)) { | ||
const context = getContextFromWsKey(data.wsKey); | ||
|
||
if (!context?.symbol) { | ||
throw new Error(`Failed to extract context from event?`); | ||
} | ||
|
||
console.log(`ws book event for "${context.symbol.toUpperCase()}"`, data); | ||
return; | ||
} | ||
|
||
console.log('log formattedMessage: ', data); | ||
}); | ||
|
||
wsClient.on('open', (data) => { | ||
console.log('connection opened open:', data.wsKey, data.ws.target.url); | ||
}); | ||
wsClient.on('reply', (data) => { | ||
console.log('log reply: ', JSON.stringify(data, null, 2)); | ||
}); | ||
wsClient.on('reconnecting', (data) => { | ||
console.log('ws automatically reconnecting.... ', data?.wsKey); | ||
}); | ||
wsClient.on('reconnected', (data) => { | ||
console.log('ws has reconnected ', data?.wsKey); | ||
}); | ||
|
||
// Request subscription to the following symbol trade events: | ||
const symbols = ['BTCUSDT', 'ETHUSDT', 'BNBUSDT']; | ||
// const symbols = ['BTCUSDT']; | ||
|
||
// Loop through symbols | ||
for (const symbol of symbols) { | ||
console.log('subscribing to trades for: ', symbol); | ||
wsClient.subscribePartialBookDepths(symbol, 20, 1000, 'spot'); | ||
} | ||
})(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters