-1

I'm trying to figure out basic websocket communication using node.js, the "ws"-package (which seems to be a very popular websocket package from npmjs.com) and the bitfinex.com (a cryptocurrency exchange) websocket API. I want to read the public Ticker for a certain currency-pair, the docs are here: https://docs.bitfinex.com/v2/reference#ws-public-ticker

My result so far is working but is still much different from what I am supposed to get according to the docs.

I am working with this code snippet taken from the documentation linked above:

const ws = require('ws')
const w = new ws('wss://api.bitfinex.com/ws/2')

w.on('message', (msg) => {
    console.log(msg)
})

let msg = JSON.stringify({ 
  event: 'subscribe', 
  channel: 'ticker', 
  symbol: 'tBTCUSD' 
})

 w.on('open', () => {
    w.send(msg)
 })  

Which works so far by outputting to the console the message from the subscribed channel: [1,[14873,23.49464465,14874,61.09031263,1087,0.0789,14872,56895.20497085,15500,13891]]

But now, and here is the issue, in the docs the response looks different. How would I determine which number is what? I should be able to get all kinds of more information from the response, no? The given example response looks like this:

// response - trading
{
    event: "subscribed",
    channel: "ticker",
    chanId: CHANNEL_ID,
    pair: "BTCUSD"
 }

How does this relate to that array of numbers I get? How would I for example read the "pair:" field ("BTCUSD") or any of the other listed fields, like (BID, BID_PERIOD, VOLUME, HIGH, LOW etc.)? Am I missing something obvious?

I know this is a lot to ask at once but maybe someone knows one or two good examples or hints to enlighten me. Thanks in advance!

Kind regards, s

oystersauce
  • 631
  • 1
  • 6
  • 17

1 Answers1

2

The overall websocket scheme for this API is described in https://bitfinex.readme.io/v2/docs/ws-general If you haven't already read that page, now would be a good time to do it.

For your example program you should have seen info and subscribed events as the first two messages from the websocket. info should have been sent as soon as the websocket connection was established, and subscribed should have been sent in response to your subscribe request.

After that, you should see a ticker snapshot message followed by periodic ticker update messages for the channel that you subscribed to. These are the JSON arrays that you're seeing. The format of these messages for a public ticker channel is is described at https://bitfinex.readme.io/v2/reference#ws-public-ticker -- click the Snapshot and Update headings in the dark green 'details' bar to see the definitions. In this case snapshots and updates use the same format:

[ CHANNEL_ID,
  [ FRR, BID, BID_PERIOD, BID_SIZE, ASK, ASK_PERIOD, ASK_SIZE,
    DAILY_CHANGE, DAILY_CHANGE_PERC, LAST_PRICE, VOLUME, HIGH, LOW
  ]
]

with meanings as described in the 'Stream Fields' table at the above URL. You can parse these messages as JSON strings and access the field values just as you would for any array.

It's a little strange that the API sends these as arrays rather than as objects with named attributes. I imagine they're looking to keep these messages compact because they make up the bulk of the traffic.

ottomeister
  • 5,415
  • 2
  • 23
  • 27