0

I have written a code in vue.js which creates a websocket connection and subscribe to a channel to get the data of BTC in USD.

But that channel one sends the updated data of size for some prices but I need a way to resubscribe to the channel to get the update prices as well.

Here is the link for bybit api documentation

Here is the code I tried:

function GetData(that) {
     

        var ws_bybit_ita = new WebSocket("wss://stream.bybit.com/realtime");

            ws_bybit_ita.onopen = function () {


                ws_bybit_ita.send(
                    JSON.stringify({ op: "subscribe", args: ["orderBook_200.100ms.BTCUSD"] })
                )


            }

            ws_bybit_ita.onmessage = function (msgEvent) {
                let response = JSON.parse(msgEvent.data)

                const data = response;

                if (data.data && data.type == "snapshot") {
                    
                    console.log(data.data);
                    

                } else if (data.type == "delta") {
                    

            }

        

        CheckState(that,ws_bybit_ita)

    }; 
    function CheckState(that, ws_bybit_ita) {
        if (ws_bybit_ita.readyState === 1) {
            ws_bybit_ita.send(
                JSON.stringify({ op: "unsubscribe", args: ["orderBook_200.100ms.BTCUSD"] })
            )
            ws_bybit_ita.close();
        } 
        
        
        setTimeout(function () {

           GetData(that);

        }, 500);
        
    };
Engr Umair
  • 130
  • 10
  • Why do you close the socket in `CheckState`? Why not keeping it open if you expect to receive updated data? – Sam R. Sep 12 '20 at 18:28
  • @Sam because it is sending the update of sizes for price but not the new prices but I need to refresh the socket to get new prices. – Engr Umair Sep 14 '20 at 07:32
  • Actually you are connecting to bybit not bitmex. Second, you cannot just open and close the bitmex websocket since per API you are limited to about ~30 times - after that you'll get time-limited – Maximilian Haindl Sep 14 '20 at 07:43
  • @kinoth I didn't need to connect with bitmex, as I am interested in bybit not bitmex. I have another program for bitmex and bitmex is working differently than bybit. – Engr Umair Sep 16 '20 at 04:30

0 Answers0