Created
May 14, 2015 10:58
-
-
Save assafshomer/5f47ddbff9b0dd5bc31a to your computer and use it in GitHub Desktop.
Toshi Block Subscription on the Bitcoin Network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def url(network) | |
case network.to_sym | |
when :testnet | |
return 'wss://testnet3.toshi.io' | |
when :bitcoin | |
return 'wss://bitcoin.toshi.io' | |
else | |
return "'ws://localhost:5000'" | |
end | |
end | |
init_time=Time.now | |
p "Starting at: #{init_time}" | |
Thread.new{ | |
EM.run { | |
ws = Faye::WebSocket::Client.new(url(:bitcoin)); | |
ws.on :open do |event| | |
p [:open] | |
ws.send('{"subscribe":"blocks"}') | |
p "Subscribed to blocks" | |
end | |
ws.on :message do |event| | |
p [:message, event.data] | |
p "new event: #{event.data}" | |
end | |
EM.add_periodic_timer(5) do | |
sec = "#{(Time.now - init_time).round}" | |
Rails.logger.debug("*** EM [#{EM.__id__}] : Now it is #{Time.now} (Running for [#{sec}]) seconds ***") | |
end | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment