Skip to content

Instantly share code, notes, and snippets.

@assafshomer
Last active August 29, 2015 14:21
Show Gist options
  • Save assafshomer/92925b2789f6cebf93b7 to your computer and use it in GitHub Desktop.
Save assafshomer/92925b2789f6cebf93b7 to your computer and use it in GitHub Desktop.
Toshi Block Subscription to Testnet3
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(:testnet));
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