Skip to content
This repository was archived by the owner on Apr 12, 2022. It is now read-only.

Websocket API

Connor Giles edited this page Jul 15, 2017 · 5 revisions

When your application requires real time access to data, using the Websocket API is recommended.

Websocket support is implemented for all exchanges listed with a ✅ next to them.

Open a Connection

const xchange = require('xchange-js')
const client = new xchange.Gdax.WebsocketClient();
client.on('open', () => {
    // From here you can subscribe to various streams
});
client.on('error', (err) => {
    // All errors will be passed through this event
});
client.on('close', (data) => {
    // Event on close of socket
});

Orderbook Streams

Subscribe to Orderbook

// Called after connection is open
client.subscribe('BTCUSD');   // returns created Market of pair
client.subscribe('ETHBTC');   // Can subscribe to as many pairs as you want
client.on('change', (market) => {
   // Access updated market book for the pair that triggered the change
   console.log(market.book.state());
});

Subscribe to Orderbook Depth Changes

Allows you to only recieve change events when the orderbook changes below a depth

// Called after connection is open
let market = client.subscribe('BTCUSD');   // returns created Market of pair
market.book.subscribe(5);    // subscribes to book changes within top 5 layers
market.on('change', (book) => {
   // Access updated book state of depth 5
   console.log(book);
});

Clone this wiki locally