Skip to content

Instantly share code, notes, and snippets.

@albanpeignier
Created September 11, 2014 13:09
Show Gist options
  • Select an option

  • Save albanpeignier/fb1165bf8148a2225800 to your computer and use it in GitHub Desktop.

Select an option

Save albanpeignier/fb1165bf8148a2225800 to your computer and use it in GitHub Desktop.

Revisions

  1. albanpeignier created this gist Sep 11, 2014.
    39 changes: 39 additions & 0 deletions metalive_client.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    # Prerequires
    #
    # $ gem install eventmachine em-websocket-client json
    # Building native extensions. This could take a while...
    # Successfully installed eventmachine-1.0.3
    # Fetching: em-websocket-client-0.1.2.gem (100%)
    # Successfully installed em-websocket-client-0.1.2
    # Building native extensions. This could take a while...
    # Successfully installed json-1.8.1
    # 3 gems installed
    #
    # Run
    #
    # $ ruby metalive_client.rb
    # {"description"=>{"title"=>"Test"}, "occurred_at"=>"2014-09-11T13:01:04.590205021Z", "uuid"=>"4223e28d-0e4d-4c13-ae34-6c04093dbd4f"}
    # {"description"=>{"album"=>"Afro-Colombian Sound Modernizers", "artist"=>"Son Palenque", "group"=>"MUSIC", "title"=>"Cumbia Africana", "year"=>"YYYY"}, "occurred_at"=>"2014-09-11T13:05:06.071020001Z", "uuid"=>"a514fb08-dfdc-4a64-8fdb-bd323c988367"}
    # ...

    require 'eventmachine'
    require 'em-websocket-client'
    require 'json'

    EM.run do
    conn = EventMachine::WebSocketClient.connect("ws://metalive.tryphon.eu:8080/api/streams/test/events.ws")

    conn.errback do |e|
    puts "Got error: #{e}"
    end

    conn.disconnect do
    puts "gone"
    EM::stop_event_loop
    end

    conn.stream do |msg|
    event = JSON.parse(msg.data)
    puts event.inspect
    end
    end