Skip to content

Instantly share code, notes, and snippets.

@arttaylor
Created May 19, 2011 08:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arttaylor/980399 to your computer and use it in GitHub Desktop.
Save arttaylor/980399 to your computer and use it in GitHub Desktop.
Simple Mt. Gox trades websocket script
#!/usr/bin/env ruby
require 'rubygems'
require 'eventmachine'
require 'em-http-request'
require 'json'
class Channel
def Channel.unsubscribe_message(channel)
uuid = case channel
when :trades
"dbf1dee9-4f2e-4a08-8cb7-748919a71b21" # trades (each time a trade happens, you get something here)
when :ticker
"d5f06780-30a8-4a48-a2f8-7ed181b4a13f" # the mtgox ticker (lots of updates, with often the same data)
when :depth
"24e67e0d-1cad-4cc0-9e7a-f8523ef460fe" # depth information in realtime (price + amount + type... type=1=Ask, type=2=Bid)
end
{'op' => 'unsubscribe', 'channel' => uuid}.to_json
end
end
EventMachine.run {
http = EventMachine::HttpRequest.new("ws://websocket.mtgox.com/mtgox").get :timeout => 0
http.errback { puts "oops" }
http.callback {
puts "connected"
http.send Channel.unsubscribe_message(:depth)
http.send Channel.unsubscribe_message(:trades)
}
http.stream { |msg|
puts JSON.parse(msg)['ticker']
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment