Asynchronous HTTP Client (EventMachine + Ruby)
Pull request Compare This branch is 345 commits behind igrigorik:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Asynchronous HTTP client for Ruby, based on EventMachine runtime.

  • Ragel HTTP parser for speed & performance
  • Simple interface for single & parallel requests via deferred callbacks
  • Automatic gzip & deflate decoding
  • Basic-Auth & OAuth support
  • Custom timeout support
  • Stream response processing
  • Proxy support (with SSL Tunneling): CONNECT, direct & SOCKS5
  • Auto-follow 3xx redirects with custom max depth
  • Bi-directional communication with web-socket services
  • Native mocking support and through Webmock

Getting started

  gem install em-http-request
  irb:0> require 'em-http'

Or checkout screencast / demo of using EM-HTTP-Request.

Libraries & Applications using em-http

  • chirpstream - EM client for Twitters Chirpstream API
  • RDaneel - Ruby crawler which respects robots.txt
  • rsolr-async - An asynchronus connection adapter for RSolr
  • PubSubHubbub - Asynchronous PubSubHubbub ruby client
  • Firering - Eventmachine powered Campfire API
  • and many others.. drop me a link if you want yours included!

Simple client example {
    http ='').get :query => {'keyname' => 'value'}, :timeout => 10

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


Multi-request example

Fire and wait for multiple requests to complete via the MultiRequest interface. {
    multi =

    # add multiple requests to the multi-handler

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


Basic-Auth example

Full basic author support. For OAuth, check examples/oauth-tweet.rb file. {
    http ='').get :head => {'authorization' => ['user', 'pass']}

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

POSTing data example

For multi-part uploads, please see this gist. {
    http1 ='').post :body => {"key1" => 1, "key2" => [2,3]}
    http2 ='').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 content is pushed to the client, it is passed to the stream callback for you to operate on. {
    http ='').get { |chunk| print chunk }

Streaming files from disk

Allows you to efficiently stream a (large) file from disk via EventMachine's FileStream interface. {
    http ='').post :file => 'largefile.txt'
    http.callback { |chunk| puts "Upload finished!" }

Proxy example

Full transparent proxy support with support for SSL tunneling. {
    http ='').get :proxy => {
      :host => '',
      :port => 8080,
      :authorization => ['username', 'password'] # authorization is optional

SOCKS5 Proxy example

Tunnel your requests via connect via SOCKS5 proxies (ssh -D port somehost). {
  http ='').get :proxy => {
    :host => '',
    :port => 8080,
    :type => :socks

Auto-follow 3xx redirects

Specify the max depth of redirects to follow, default is 0. {
    http ='').get :redirects => 1
    http.callback { p http.last_effective_url }

WebSocket example

Bi-directional communication with WebSockets: simply pass in a ws:// resource and the client will negotiate the connection upgrade for you. On successful 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! {
    http ="ws://").get :timeout => 0

    http.errback { puts "oops" }
    http.callback {
      puts "WebSocket connected!"
      http.send("Hello client")
    } { |msg|
      puts "Recieved: #{msg}"
      http.send "Pong: #{msg}"

    http.disconnect { puts "oops, dropped connection?" }


(MIT License) - Copyright (c) 2011 Ilya Grigorik