github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

igrigorik / em-http-request

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 171
    • 20
  • Source
  • Commits
  • Network (20)
  • Issues (5)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (3)
    • errback_handler
    • master ✓
    • websocket
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Asynchronous HTTP Client (EventMachine + Ruby) — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Merge branch 'master' of git://github.com/careo/em-http-request 
Ilya Grigorik (author)
Sat Jan 16 17:37:10 -0800 2010
commit  ecd6756fd7b19024df8c5371d3598afe1ebb5b91
tree    09f3a95ac26cffacae342dffc9f5717d1505c733
parent  29af8d4f63541c7bcdcba73348a5a73763314575 parent  04de4cebbc7891cb4704750c552924c76b566d67
em-http-request /
name age
history
message
file .gitignore Sun Feb 01 10:47:43 -0800 2009 Ignore build products and extconf artifacts. [careo]
file LICENSE Fri Aug 22 10:56:57 -0700 2008 initial import [Ilya Grigorik]
file README.rdoc Tue Dec 29 20:11:46 -0800 2009 oauth example & updated readme [Ilya Grigorik]
file Rakefile Sun Jan 10 16:46:36 -0800 2010 fix rake spec task [Ilya Grigorik]
file VERSION Wed Dec 30 21:45:27 -0800 2009 Version bump to 0.2.6 [Ilya Grigorik]
directory examples/ Tue Dec 29 20:11:46 -0800 2009 oauth example & updated readme [Ilya Grigorik]
directory ext/ Sun Jan 10 11:31:45 -0800 2010 fixed rb_hash_lookup check on 1.8.7 [Edgars Beigarts]
directory lib/ Sat Jan 16 17:37:10 -0800 2010 Merge branch 'master' of git://github.com/careo... [Ilya Grigorik]
directory spec/ Wed Dec 30 21:44:16 -0800 2009 stop the hash test sillyness [Ilya Grigorik]
README.rdoc

EM-HTTP-Client

EventMachine based HTTP Request interface. Supports streaming response processing, uses Ragel HTTP parser.

  • Simple interface for single & parallel requests via deferred callbacks
  • Automatic gzip & deflate decoding
  • Basic-Auth & OAuth support
  • Custom timeouts
  • Proxy support (with SSL Tunneling)
  • Bi-directional communication with web-socket services

Screencast / Demo of using EM-HTTP-Request:

  • everburning.com/news/eventmachine-screencast-em-http-request/

Getting started

  # install & configure gemcutter repos
  gem update --system
  gem install gemcutter
  gem tumble

  gem install em-http-request

  irb:0> require 'em-http'

Simple client example

  EventMachine.run {
    http = EventMachine::HttpRequest.new('http://127.0.0.1/').get :query => {'keyname' => 'value'}, :timeout => 10

    http.callback {
      p http.response_header.status
      p http.response_header
      p http.response

      EventMachine.stop
    }
  }

Multi request example

Fire and wait for multiple requess to complete via the MultiRequest interface.

  EventMachine.run {
    multi = EventMachine::MultiRequest.new

    # add multiple requests to the multi-handler
    multi.add(EventMachine::HttpRequest.new('http://www.google.com/').get)
    multi.add(EventMachine::HttpRequest.new('http://www.yahoo.com/').get)

    multi.callback  {
      p multi.responses[:succeeded]
      p multi.responses[:failed]

      EventMachine.stop
    }
  }

Basic-Auth example

Full basic author support. For OAuth, check examples/oauth-tweet.rb file.

  EventMachine.run {
    http = EventMachine::HttpRequest.new('http://www.website.com/').get :head => {'authorization' => ['user', 'pass']}

    http.errback { failed }
    http.callback {
      p http.response_header
      EventMachine.stop
    }
  }

POST example

  EventMachine.run {
    http1 = EventMachine::HttpRequest.new('http://www.website.com/').post :body => {"key1" => 1, "key2" => [2,3]}
    http2 = EventMachine::HttpRequest.new('http://www.website.com/').post :body => "some data"

    # ...
  }

Streaming body processing

Allows you to consume an HTTP stream of content in real-time. Each time a new piece of conent is pushed to the client, it is passed to the stream callback for you to operate on.

  EventMachine.run {
    http = EventMachine::HttpRequest.new('http://www.website.com/').get
    http.stream { |chunk| print chunk }

    # ...
  }

Proxy example

Full transparent proxy support with support for SSL tunneling.

  EventMachine.run {
    http = EventMachine::HttpRequest.new('http://www.website.com/').get :proxy => {
      :host => 'www.myproxy.com',
      :port => 8080,
      :authorization => ['username', 'password'] # authorization is optional
  }

WebSocket example

Bi-directional communication with WebSockets: simply pass in a ws:// resource and the client will negotiate the connection upgrade for you. On successfull handshake the callback is invoked, and any incoming messages will be passed to the stream callback. The client can also send data to the server at will by calling the "send" method!

  • www.igvita.com/2009/12/22/ruby-websockets-tcp-for-the-browser/

    EventMachine.run {

      http = EventMachine::HttpRequest.new("ws://yourservice.com/websocket").get :timeout => 0
    
      http.errback { puts "oops" }
      http.callback {
        puts "WebSocket connected!"
        http.send("Hello client")
      }
    
      http.stream { |msg|
        puts "Recieved: #{msg}"
        http.send "Pong: #{msg}"
      }
    

    }

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server