Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Mongrel2 Connection Adapter - Ruby Encrusted Steel-Reinforced Fist
Fetching latest commit...
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


A Mongrel2 backend handler written in Ruby. Also includes Rack adpater to get you up and running quickly.

Build Status Dependency Status



You'll need following prerequisites first:

Next, in your Gemfile add:

gem 'm2r'

And finally run:

bundle install


Running Rack Application


Add m2r to Gemfile and run bundle install

Mongrel 2

Configure Handler for your application:

rack_example = Handler(
  send_spec  = "tcp://",
  send_ident = "14fff75f-3474-4089-af6d-bbd67735ab89",
  recv_spec  = "tcp://",
  recv_ident = ""


[bundle exec] rackup -s mongrel2

Add -O option_name to provide options for m2r handler:

[bundle exec] rackup -s mongrel2 -O recv_addr=tcp:// -O send_addr=tcp://


  • recv_addr - This is the send_spec option from Handler configuration in mongrel2.conf. Default: tcp://
  • send_addr - This is the recv_spec option from Handler configuration in your mongrel2.conf. Default: tcp://
  • factory - Use it to load custom ConnectionFactory that implements rules for ZMQ connections to Mongrel 2.

Custom Connection Factory

ZMQ allows to set multiple options and connect to large number of endpoints. Providing every ZMQ option for handler connections would be troublesome. Instead you can use your custom implementation that deals only with that fact.

Automatic require of custom connection factory

The first way to do it is to implement custom class in a file that can be required with m2r/connection_factory/custom_name. The location of such file might depends on how $LOAD_PATH is configured but for standard Rails application or gem that would like to depend on m2r it would be: lib/m2r/connection_factory/custom_name.

Implement the Factory in the file:

module M2R
  class ConnectionFactory
    # Just exemplary implementation ...
    class CustomName < ConnectionFactory
      def initialize(options)
        # OpenStruct with rackup options for the handler (added with -O)
        @options = options

      def connection
        request_socket = @context.socket(ZMQ::PULL)
        request_socket.setsockopt(ZMQ::RECONNECT_IVL, 5)

        response_socket = @context.socket(ZMQ::PUB)
        response_socket.setsockopt(ZMQ::HWM, 100)
        response_socket.setsockopt(ZMQ::RECONNECT_IVL, 5), response_socket)

Use connection_factory option to select it.

[bundle exec] rackup -s mongrel2 -O connection_factory=custom_name
Manual require of factory

Implement custom factory in a file like in a previous paragraph.

Load the file using -r option for rackup and use connection_factory option.

[bundle exec] rackup -r custom_name.rb -s mongrel2 -O connection_factory=custom_name

Processing HTTPS requests from Mongrel2 1.7

Set HTTPS env to true.

HTTPS=true [bundle exec] rackup -s mongrel2

For Mongrel2 1.8 and newer this is not necessary.

Developing custom bare Handler



Starting from version 0.1.0 this gem follows semantic versioning policy.


  • examples/http_0mq.rb is a test little servlet thing (based on what comes with mongrel2)
  • examples/ is a rackup file using the Rack handler that'll serve Rack's funny little lobster app


In the spirit of free software, everyone is encouraged to help improve this project.

Here are some ways you can contribute:

  • by using alpha, beta, and prerelease versions
  • by reporting bugs
  • by suggesting new features
  • by writing or editing documentation
  • by writing tests
  • by writing code (no patch is too small: fix typos, add comments, clean up inconsistent whitespace)
  • by refactoring code
  • by closing issues
  • by reviewing patches

Read Contributing page before sending Pull Request :)

Submitting an Issue

We use the GitHub issue tracker to track bugs and features. Before submitting a bug report or feature request, check to make sure it hasn't already been submitted. When submitting a bug report, please include a Gist that includes a stack trace and any details that may be necessary to reproduce the bug, including your gem version, Ruby version, and operating system. Ideally, a bug report should include a pull request with failing tests.

Submitting a Pull Request

  1. Read Contributing page
  2. Fork the repository.
  3. Create a topic branch.
  4. Add tests for your unimplemented feature or bug fix.
  5. Run bundle exec rake. If your test pass, return to step 3.
  6. Implement your feature or bug fix.
  7. Run bundle exec rake. If your tests fail, return to step 5.
  8. Add, commit, and push your changes.
  9. Submit a pull request.

Supported Ruby Versions

This library aims to support and is tested against the following Ruby implementations:

  • Ruby 1.9.2
  • Ruby 1.9.3
  • JRuby
  • Rubinius


Mongrel2 is a registered trademark of Zed A. Shaw who wrote it. And it is awesome.

Something went wrong with that request. Please try again.