Sending keep-alive packet, bitcoin.

0 votes

I want to send a keep-alive packet in ruby on rails. I am using the following code for this:

ws = WebSocket::Client::Simple.connect 'wss://bitcoin.toshi.io/'


ws.on :message do |msg|

  rawJson = msg.data

  message_response = JSON.parse(rawJson)

end


ws.on :open do

  ws.send "{\"subscribe\":\"blocks\"}"

end


ws.on :close do |e|

  puts "WEBSOCKET HAS CLOSED #{e}"

  exit 1

end


ws.on :error do |e|

  puts "WEBSOCKET ERROR #{e}"

end

But the connect gets close. Cant keep it alive. Any solution?

Aug 20, 2018 in Blockchain by slayer
• 29,050 points

retagged Nov 22, 2018 by Kalgi 47 views

1 answer to this question.

0 votes

You can use Websocket Eventmachine Client (https://github.com/imanel/websocket-eventmachine-client) gem to send hearbeat:

require 'websocket-eventmachine-client'


EM.run do
  ws = WebSocket::EventMachine::Client.connect(:uri => 'wss://bitcoin.toshi.io/')
  puts ws.comm_inactivity_timeout
  ws.onopen do
    puts "Connected"
  end


  ws.onmessage do |msg, type|
    puts "Received message: #{msg}"
  end


  ws.onclose do |code, reason|
    puts "Disconnected with status code: #{code}"
  end


  EventMachine.add_periodic_timer(15) do
    ws.send "{}"
  end
end
answered Aug 20, 2018 by digger
• 27,640 points

Related Questions In Blockchain

0 votes
1 answer

Should I encrypt my address before sending bitcoin?

Hey. No, you dont have to encrypt ...READ MORE

answered Dec 3, 2018 in Blockchain by Omkar
• 67,460 points
42 views
+1 vote
1 answer

Getting issues while Sending signTransaction with web3.js

The chain ID for ropsten is 3. ...READ MORE

answered Mar 28, 2018 in Blockchain by Johnathon
• 9,080 points
436 views
+1 vote
1 answer
0 votes
1 answer
+1 vote
3 answers

Removing double quotes from a string from JSON response in PHP

Just remove the json_encode call, and it should work: $resp ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 27,640 points
3,010 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

What is blockchain apart from bitcoin?

Blockchain is a decentralized database where the ...READ MORE

answered Jul 9, 2018 in Blockchain by digger
• 27,640 points
21 views
0 votes
1 answer

How i can use nodejs to watch transactions in bitcoin network?

you can use  const Socket = require('blockchain.info/Socket'); const mySocket ...READ MORE

answered Jul 9, 2018 in Blockchain by digger
• 27,640 points
46 views