Recently, I want to collect Binance trading ticker to simulate a trade setup of a specific pair and was researched for how to do it programmatically and found Binance offered a WSS API to actully subscribe a price ticker.
This is a rough step-by-step i follow through Binance docs:
Step #1 - Install Binance Typescript connector
npm install @binance/connector-typescript
Step #2 - Exporting Binance API key to your shell session (or add it to environment variable).
export API_KEY=<your_api_key>
export API_SECRET=<you_api_secret>
Step #3 - Define callaback for Binance websocket events and subscribe to ticker.
import {
WebsocketAPI,
WsMarketTypes,
WebsocketStream,
} from "@binance/connector-typescript";
const callbacks = {
open: () => console.debug("Connected to WebSocket server"),
close: () => console.debug("Disconnected from WebSocket server"),
message: (data: string) => console.info(data),
};
const websocketStreamClient = new WebsocketStream({ callbacks });
websocketStreamClient.ticker("bnbbtc");
setTimeout(() => websocketStreamClient.pingServer(), 4000);
setTimeout(() => websocketStreamClient.disconnect(), 6000);
Optional - Reconnecting to Binance WSS
setTimeout(() => {
websocketStreamClient.ticker("bnbbtc");
}, 9000);
setTimeout(() => websocketStreamClient.disconnect(), 12000);