0

Current code for opening websocket :

139    this.ws = new WebSocket('wss://ws.blockchain.info/inv')
140    this.ws.on('open', () => {
141        this.ws.send('{"op":"unconfirmed_sub"}')
142        setInterval(() => {
143            this.ws.send('{"op":"ping"}')
144          }, 10000)
145        })

At first theres no issue with this code, it keep the websocket still alive everytime my node js running. But starting yesterday the websocket giving an error like this every around 10 sec of opened.

Error: WebSocket is not open: readyState 3 (CLOSED)
    at WebSocket.send (/root/project/node_modules/ws/lib/websocket.js:31                                                                                                                                                             
at Timeout.setInterval [as _onTimeout] (/root/project/src/master.js:143:17)                                                       
    at ontimeout (timers.js:469:11)
    at tryOnTimeout (timers.js:304:5)
    at Timer.listOnTimeout (timers.js:264:5)

Is that because the websocket is closing by itself? and if yes is theres any way to keep the websocket alive even if its already closed kinda like reopen it. Or maybe theres another alternate way to keep it alive ? The websocket itself is blockchain api. https://blockchain.info/api/api_websocket

Sorry for my newbie question tbh im not a programmer but i want learn how to fix this hope someone on stackoverflow can help me out! :D Thanks Stackoverflow!

Gaben Lounge
  • 35
  • 1
  • 6
  • Add `this.ws.on('close', () => console.log('disconnected'));` And then tell if it disconnects within 10 seconds – Brahma Dev Apr 11 '18 at 12:38
  • I tried to replicate your setup and the API/websocket works fine in my case. https://jsbin.com/hecipafalu/1/edit?html,output – Brahma Dev Apr 11 '18 at 12:50
  • Yes just now i try again and works without any issue again with my current setup, So weird. 2 Hours ago it keep disconnected within 10 sec. is that the problem is on blockchain websocket services ? – Gaben Lounge Apr 11 '18 at 13:04

0 Answers0