Connect to socket.io server in Ruby
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Looking for a way to connect to the icbit.se API using Ruby.

Deliverable:
Source code in Ruby 1.9.3 that does the following:
1. Create connection
2. Send message
3. Receive and parse message

https://icbit.se/api

awarded to dierbro

Crowdsource coding tasks.

1 Solution

Winning solution

Make sure to use my patched version of socket.io-ruby-client and replace YOUR_AUTH_KEY and YOUR_USER_ID

Gemfile

source 'https://rubygems.org'
gem 'socketio-client', github: "dierbro/socket.io-ruby-client"

wrapper.rb (implements the example on the ICBIT api documentation)

require 'SocketIO'
require 'json'

AuthKey = "YOUR_AUTH_KEY"
UserId = YOUR_USER_ID

client = SocketIO.connect("https://api.icbit.se:443/icbit?AuthKey=#{AuthKey}&UserId=#{UserId}") do
    after_start do
      emit('message', { op: 'subscribe', channel: 'orderbook_BUH3' })
    end

    on_json_message do |message| 
      puts JSON.parse(message)
    end
end

Install process

mkdir icbit_api
cd icbit_api
create Gemfile and paste content
create wrapper.rb, paste the content and replace YOUR_AUTH_KEY  YOUR_USER_ID
bundle install
bundle exec ruby wrapper.rb

I tested the code with bundler 1.2.4 and ruby 1.9.3

Doesn't work on my machine. What is the install process? What gems are required? I created the Gemfile, and installed bundler. bundle throws error: The source :rubygems is deprecated because HTTP requests are insecure. Please change your source to 'https://rubygems.org' if possible, or 'http://ru... Fetching gem metadata from http://rubygems.org/......... There was a LoadError while evaluating socketio-client.gemspec: cannot load such file -- rest_client from socket.io-ruby-client-daa9d81eb12a /socketio-client.gemspec:3:in `' Does it try to require a relative path
georgeu2000 almost 7 years ago
Error continued: Does it try to require a relative path? That's been removed in Ruby 1.9.
georgeu2000 almost 7 years ago
check the updated solution above
dierbro almost 7 years ago
It worked. FWIW, I needed to install rest-client. And it only works using bundle exec ruby (not ruby). Thank you!
georgeu2000 almost 7 years ago
How do you get the message out of the SocketIO.connect do loop?
georgeu2000 almost 7 years ago