Skip to content

Instantly share code, notes, and snippets.

@assafshomer
Created May 14, 2015 10:57
Show Gist options
  • Save assafshomer/225862b0531beeb1a628 to your computer and use it in GitHub Desktop.
Save assafshomer/225862b0531beeb1a628 to your computer and use it in GitHub Desktop.
Toshi Transaction Subscription on 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":"transactions"}')
p "Subscribed to transactions"
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