0.0
No commit activity in last 3 years
No release in over 3 years
DrunkMonkey is a rack middleware providing realtime two-way http communication with API for Portal, a javascript messaging library. It provides just two protocols currently; websocket and longpoll comet. You can write code once for each protocols.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 2.14

Runtime

>= 0.15
>= 1.5
>= 1.1
 Project Readme

DrunkMonkey

Build Status

DrunkMonkey is a rack middleware providing realtime two-way http communication with API for Portal.

Supported servers

You should use servers which supports Rack hijacking API, such as follows:

  • Puma

Installation

Add this line to your application's Gemfile:

gem 'drunkmonkey'

And then execute:

$ bundle

Or install it yourself as:

$ gem install drunkmonkey

Usage

Add DrunkMonkey::Builder to Rack application by use method.

Plain Rack:

app = Rack::Builder.new do
  use DrunkMonkey::Middleware do
    on :message do |socket, message|
      socket.push "ECHO: #{message}"
    end
  end
end

run app

Sinatra:

require "sinatra"

use DrunkMonkey::Middleware do
  on :message do |socket, message|
    socket.push "ECHO: #{message}"
  end
end

Note: the passed block will be executed once when use is called at first.

Then, include Portal to HTML and initialize it as follows.

<script src="/portal.js"></script>
<script>
/*
 * Following option is a hack to switch protocols automatically
 * when the connection cannot be established.
 */
var options = {
    transports:["ws","longpollajax"],
    reconnect:function(lastDelay,attempts){
      if(options.transports.length > 1)
        options.transports.shift();
      return 2 * (lastDelay || 100);},
    prepare:function(connect,disconnect,opts){
      opts.transports = options.transports;connect();}
};

//Add above options and drunkmonkey's path; default is "/drunkmonkey".
var socket = portal.open("/drunkmonkey",options).on("open",function(){
  //Add you event.
  socket.send("Hello!");
});
</script>

Note: in current version, supported transports by Drunkmonkey are websocket and long-poll ajax only.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request