Stream Orderbook

Stream the order book

The order book is a dynamic electronic list of buy and sell orders for an asset (in this case, cryptocurrencies) organised by price level. You can receive real-time updates on the Buycoins order book by connecting to our order book WebSockets API.

General considerations

  • All messages sent and received via WebSockets are encoded in binary format.

  • Please use the getPairs query to retrieve the list of pairs which you can subscribe to updates for using the WebSockets API.

    • Our getPairs query returns pairs in this format ${baseCurrency}_${quoteCurrency} . The WebSockets API requires pairs in this format ${baseCurrency}/${quoteCurrency}

    • For example, (GraphQL API) btc_usdt -> (WebSockets API) BTC/USDT

Streaming order book updates

Endpoint: wss://markets.buycoins.tech/ws?pair=${PAIR}

You should immediately start receiving a stream of binary-encoded protocol buffer messages once you successfully connect to the endpoint provided above with a valid currency pair. To decode the binary-encoded protocol buffer messages, you'll need to use one of our protocol buffer client libraries:

Connecting to the WebSocket API

// JavaScript example
import {Orderbook} from 'public-protos-js/proto/orderbook_socket/v1/orderbook_pb';
const baseUrl = `wss://markets.buycoins.tech/ws?pair=BTC/USDT`;
const orderbookSocket = new WebSocket(baseUrl);
orderbookSocket.binaryType = 'arraybuffer';
window.orderbookSocket.addEventListener('open', () => {
console.log('Disconnected from orderbook WebSocket API');
});
window.orderbookSocket.addEventListener('close', () => {
console.log('Disconnected from orderbook WebSocket API');
// Implement reconnection logic here..
});
window.orderbookSocket.addEventListener('message', ({ data }) => {
console.log('Order Book update received');
const market = Orderbook.deserializeBinary(data).toObject();
console.log(market);
});