Resubscribe to the websocket channel using setTimeout

428 views Asked by At

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);
        
    };
0

There are 0 answers