From e54ba0fc935335124933338463b6936b39fb77cb Mon Sep 17 00:00:00 2001 From: "Tiago Siebler @ Siebly.io" Date: Mon, 19 Oct 2026 15:46:54 +0100 Subject: [PATCH] fix: add a public Bybit ticker quickstart --- examples/Websocket/Public/ws-public-ticker.md | 18 ++++++++ examples/Websocket/Public/ws-public-ticker.ts | 46 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 examples/Websocket/Public/ws-public-ticker.md create mode 100644 examples/Websocket/Public/ws-public-ticker.ts diff --git a/examples/Websocket/Public/ws-public-ticker.md b/examples/Websocket/Public/ws-public-ticker.md new file mode 100644 index 00000000..9c324824 --- /dev/null +++ b/examples/Websocket/Public/ws-public-ticker.md @@ -0,0 +1,18 @@ +Subscribe to the public `tickers.BTCUSDT` topic with `WebsocketClient` and the +`linear` product category. No API key or secret is needed. + +After installing `bybit-api`, save the displayed code as `ws-public.mjs` and run +`node ws-public.mjs`. + +Register the `exception`, `response`, and `update` handlers before calling +`subscribeV5()`. The example prints the first received price and closes the +connection. It exits with a nonzero status if the subscription fails or no +price arrives within 20 seconds. + +The output contains `symbol`, `lastPrice` as a decimal string, and the exchange's +`timestamp` in milliseconds. For a continuous feed, keep the connection open +and merge ticker deltas into the preceding snapshot. Bybit omits unchanged +fields from linear ticker deltas, so a message without `lastPrice` does not +mean the price is zero or unavailable. See the +[Bybit ticker stream documentation](https://bybit-exchange.github.io/docs/v5/websocket/public/ticker) +for the message contract. diff --git a/examples/Websocket/Public/ws-public-ticker.ts b/examples/Websocket/Public/ws-public-ticker.ts new file mode 100644 index 00000000..3b5dfd57 --- /dev/null +++ b/examples/Websocket/Public/ws-public-ticker.ts @@ -0,0 +1,46 @@ +import { WebsocketClient } from '../../../src/index'; + +const wsClient = new WebsocketClient(); +const topic = 'tickers.BTCUSDT'; +let stopped = false; + +const timeout = setTimeout(() => { + console.error('No BTCUSDT price received within 20 seconds.'); + stop(1); +}, 20_000); + +function stop(exitCode = 0) { + if (stopped) return; + stopped = true; + clearTimeout(timeout); + wsClient.closeAll(); + process.exitCode = exitCode; +} + +wsClient.on('exception', (error) => { + console.error('WebSocket error:', error); + stop(1); +}); + +wsClient.on('response', (response) => { + if (response.op === 'subscribe' && response.success === false) { + console.error('Subscription rejected:', response); + stop(1); + } +}); + +wsClient.on('update', (message) => { + if (stopped || message.topic !== topic || !message.data?.lastPrice) return; + + console.log({ + symbol: message.data.symbol, + lastPrice: message.data.lastPrice, + timestamp: message.ts, + }); + stop(); +}); + +Promise.all(wsClient.subscribeV5(topic, 'linear')).catch((error) => { + console.error('Could not subscribe:', error); + stop(1); +});