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,350 points

retagged Nov 23, 2018 by Kalgi 570 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
• 26,740 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 4, 2018 in Blockchain by Omkar
• 69,210 points
447 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,090 points
3,281 views
+1 vote
1 answer
0 votes
1 answer

How do I query Blockchain bitcoin locally?

You could use json RPC provided by ...READ MORE

answered Apr 19, 2018 in Blockchain by Christine
• 15,790 points
1,326 views
+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
• 26,740 points
43,726 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,211 views
0 votes
1 answer

How do I add multiple recipients for transactions via Blockchain API?

Convert the recipes into JSON objects. x = ...READ MORE

answered Jul 6, 2018 in Blockchain by Perry
• 17,100 points
669 views
+1 vote
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
• 26,740 points
565 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
• 26,740 points
1,025 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP