Skip to main content

PublicStreamClient

認証不要のパブリック WebSocket クライアントです。Socket.IO で wss://stream.bitbank.cc に接続し、リアルタイムデータをストリーミングします。

インスタンス生成

import { PublicStreamClient } from '@pokooo/bb-api';

const stream = new PublicStreamClient();

インスタンス生成時に自動で接続されます。

基本的な使い方

import { PublicStreamClient, PAIR } from '@pokooo/bb-api';

const stream = new PublicStreamClient();

// 接続後にサブスクライブ
stream.on('connect', () => {
stream.subscribeTicker(PAIR.BTC_JPY);
stream.subscribeTransactions(PAIR.BTC_JPY);
stream.subscribeDepthDiff(PAIR.BTC_JPY);
});

stream.on('ticker', (roomName, data) => console.log('ticker', roomName, data));
stream.on('transactions', (roomName, data) => console.log('transactions', roomName, data));
stream.on('depth_diff', (roomName, data) => console.log('depth_diff', roomName, data));

stream.on('disconnect', (reason) => console.log('disconnected', reason));
stream.on('error', (error) => console.error('error', error));

// 接続を切断
// stream.disconnect();

イベント

connect

WebSocket 接続が確立されたときに発火します。

stream.on('connect', () => {
// ここでサブスクライブする
});

disconnect

接続が切断されたときに発火します。

stream.on('disconnect', (reason: string) => {
console.log('切断理由:', reason);
});

error

エラーが発生したときに発火します。

stream.on('error', (error: Error) => {
console.error(error);
});

ticker

ティッカー更新時に発火します。

stream.on('ticker', (roomName: string, data: WsTickerData) => {
console.log(data.sell, data.buy, data.last);
});

WsTickerData フィールド:

フィールド説明
sellstring売値
buystring買値
highstring24時間最高値
lowstring24時間最安値
openstring24時間始値
laststring最終取引価格
volstring24時間出来高
timestampnumberUnix タイムスタンプ (ms)

transactions

約定更新時に発火します。

stream.on('transactions', (roomName: string, data: WsTransactionsData) => {
data.transactions.forEach(t => {
console.log(t.side, t.price, t.amount);
});
});

depth_diff

板差分更新時に発火します。

stream.on('depth_diff', (roomName: string, data: WsDepthDiffData) => {
// data.a: 売り板差分 [価格, 数量][]
// data.b: 買い板差分 [価格, 数量][]
// data.s: シーケンス ID
// data.t: タイムスタンプ (ms)
});

depth_whole

板全量更新時に発火します。接続直後に配信されます。

stream.on('depth_whole', (roomName: string, data: WsDepthWholeData) => {
// data.asks: 売り板 [価格, 数量][]
// data.bids: 買い板 [価格, 数量][]
// data.sequenceId: シーケンス ID
// data.timestamp: タイムスタンプ (ms)
});

circuit_break_info

サーキットブレイカー情報更新時に発火します。

stream.on('circuit_break_info', (roomName: string, data: WsCircuitBreakInfoData) => {
console.log(data.mode); // 'NONE' | 'CIRCUIT_BREAK' | 'RESUMPTION' | ...
});

サブスクライブ / アンサブスクライブ

メソッド説明
subscribeTicker(pair)ティッカーをサブスクライブ
unsubscribeTicker(pair)ティッカーをアンサブスクライブ
subscribeTransactions(pair)約定履歴をサブスクライブ
unsubscribeTransactions(pair)約定履歴をアンサブスクライブ
subscribeDepthDiff(pair)板差分をサブスクライブ
unsubscribeDepthDiff(pair)板差分をアンサブスクライブ
subscribeDepthWhole(pair)板全量をサブスクライブ
unsubscribeDepthWhole(pair)板全量をアンサブスクライブ
subscribeCircuitBreakInfo(pair)サーキットブレイカー情報をサブスクライブ
unsubscribeCircuitBreakInfo(pair)サーキットブレイカー情報をアンサブスクライブ
// サブスクライブ
stream.subscribeTicker(PAIR.BTC_JPY);

// 後からアンサブスクライブ
stream.unsubscribeTicker(PAIR.BTC_JPY);

板の管理(depth_diff + depth_whole)

depth_whole で初期状態を受け取り、以降の depth_diff を適用して板を維持するパターン:

import { WsDepthWholeData, WsDepthDiffData } from '@pokooo/bb-api';

let bids = new Map<string, string>();
let asks = new Map<string, string>();
let currentSeqId = '';

stream.on('connect', () => {
stream.subscribeDepthWhole(PAIR.BTC_JPY);
stream.subscribeDepthDiff(PAIR.BTC_JPY);
});

stream.on('depth_whole', (_, data: WsDepthWholeData) => {
bids = new Map(data.bids);
asks = new Map(data.asks);
currentSeqId = data.sequenceId;
});

stream.on('depth_diff', (_, data: WsDepthDiffData) => {
// sequenceId が連続していることを確認してから適用
for (const [price, amount] of data.b) {
if (amount === '0') bids.delete(price);
else bids.set(price, amount);
}
for (const [price, amount] of data.a) {
if (amount === '0') asks.delete(price);
else asks.set(price, amount);
}
currentSeqId = data.s;
});

切断

stream.disconnect();