I want to use websockets to get the exchange rate from the Bitfex exchange. But in the browser I get an error

<html> <div id="btc"></div> <script> var ws = new WebSocket("wss://api2.bitfinex.com:3000/ws"); ws.onopen = function(){ ws.send(JSON.stringify({"event":"subscribe", "channel":"ticker", "pair":"BTCUSD"})) }; ws.onmessage = function(msg){ var response = JSON.parse(msg.data); var hb = response[1]; if(hb != "hb"){ document.getElementById("btc").innerHTML = "$" + response[7]; } }; </script> </html> 

Mistake

 failed: Error in connection establishment: net::ERR_NAME_NOT_RESOLVED 

Table output using php

 $req = json_decode(file_get_contents("https://api.bitfinex.com/v1/book/BTCUSD"), true); echo "<table><tr><td>Bids</td><td>Asks</td></tr>"; $bids = $req["bids"]; echo "<tr><td valign='top'>"; foreach($bids as $details){ echo "$".$details["price"]." - ".$details["amount"]; echo "<br>"; } echo "</td><td valign='top'>"; $asks = $req["asks"]; foreach($asks as $askDetails){ echo "$".$askDetails["price"]." - ".$askDetails["amount"]; echo "<br>"; } echo "</td></tr></table>"; 

Orderbuk

  <script> var ws = new WebSocket("wss://api-pub.bitfinex.com/ws/2"); ws.onopen = function(){ ws.send(JSON.stringify({ event: 'conf', flags: 131072 })) let msg = JSON.stringify({ event: 'subscribe', channel: 'book', pair: 'tBTCUSD', prec: 'P0' }) ws.send(msg) ws.onmessage = function(msg){ var response = JSON.parse(msg.data); console.log(response); } } </script> 

    2 answers 2

    Judging by the answers hb is the state when you do not need to update the value? Judging by the answers hb is the state when you do not need to update the value?

    Perhaps you have a problem with the <div id="btc"></div> element

    • and how can I display information in this case? - Lamer
    • You have not quite correctly recorded access to the 7th element. Here is the correct one. document.getElementById("btc").innerHTML = "$" + response[1][7]; - Alexey Kapustsin
    • Thank you, there is another question. On php written script that displays the order of the beech, also with bitfex, how can this be implemented using webocards? Completed the answer with your php code - Lamer
    • docs.bitfinex.com/v2/reference#ws-public-order-books You just need to change the parameters in ws.send a bit and then correctly parse the answer into a table, like you did in php - Alexey Kapustsin
    • And how can I get the current btc rate? paired with the dollar - Lamer

    https://docs.bitfinex.com/v2/docs/ws-general

    The Docs API specifies:

     // For public channels: wss://api-pub.bitfinex.com/ws/2 // For authenticated channels: wss://api.bitfinex.com/ws/2 

    Use the first URL for your requests.

    • Changed the url, now $ undefined is displayed on the page and all - Lamer
    • I did not check further response. Now I'll see - Alexey Kapustsin
    • Could you please see? - Lamer
    • Can you show how to withdraw a beech order? I added the answer to the code, I can not get it out. - Lamer