Skip to content

carlhoerberg/meerkat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Meerkat

Rack middleware for Server-Sent Events (HTML5 SSE).

Features:

  • Realtime events
  • Extremely efficent
  • Broad browser support (both desktop and mobile browsers)
  • Works with all proxies (unlike WebSockets)
  • Subscribe to single events
  • Subscribe to multiple events via patterns
  • Publish messages from the server
  • Publish messages from the client (via POST)

Supported backends:

  • In memory, using EventMachine Channels, good for single server usage.
  • RabbitMQ (AMQP), using the AMQP gem and the Pub/Sub pattern (Topic exchange anonymous queues with pattern matching). AMQP is the most recommened alternative.
  • Redis, using em-hiredis and the Pub/Sub API.
  • Postgres, using the Notify/Listen API.
    • When a message is published the topic and json payload is inserted into the 'meerkat_pubsub' table, and then a NOTIFY is issued.
    • Listening clients recivies the notification and reads the message from the table and writes it to the Event Stream of its clients.
    • On the next publish all messages older than 5 seconds are deleted.
    • No polling is ever done.
    • This works with PostgreSQL 8 and higher (tested with 8.3 and 9.1).

Usage

Put meerkat and amqp, pg or em-hiredis in your Gemfile, depending on which backend you plan to use. Gemfile:

gem 'meerkat'
gem 'amqp'
# or
gem 'pg'
# or
gem 'em-hiredis'

Require meerkat and the backend you would like to use.

Meerkat is based on EventMachine and thus requires Thin or Rainbows (with the EventMachine backend) as web server.

config.ru:

require 'bundler/setup'
require 'meerkat' 
require 'meerkat/backend/amqp' 
#require 'meerkat/backend/pg' 
#require 'meerkat/backend/redis' 
#require 'meerkat/backend/inmemory' 
require './app'

#Meerkat.backend = Meerkat::Backend::InMemory.new 
Meerkat.backend = Meerkat::Backend::AMQP.new 'amqp://guest:guest@localhost'
#Meerkat.backend = Meerkat::Backend::Redis.new 'redis://localhost/0'
#Meerkat.backend = Meerkat::Backend::PG.new :dbname => 'postgres'
map '/' do
  run App
end

map '/stream' do
  run Meerkat::RackAdapter.new
end

On the client:

var source = new EventSource('/stream/foo');
var streamList = document.getElementById('stream');
// Use #onmessage if you only listen to one topic
source.onmessage = function(e) {
  var li = document.createElement('li');
  li.innerHTML = JSON.parse(e.data);
  streamList.appendChild(li);
}

var multiSource = new EventSource('/stream/foo.*');
// You have to add custom event listerns when you 
// listen on multiple topics
multiSource.addEventListener('foo.bar', function(e) {
  // Do something
}, false);
multiSource.addEventListener('foo.foo', function(e) {
  // Do something
}, false);

To push things from the server:

Meerkat.publish "foo.bar", { :any => 'hash' } # the hash will automatically be json encoded
Meerkat.publish "foo.bar", 'any string'
Meerkat.publish "foo.foo", myobj.to_json, true # the third parameter indicates that the message already is json encoded

The published objects will be JSON serialized before sent to the backend. You'll have to deserialize it in the client.

From the client:

$.post('/stream', { topic: 'foo.bar', data: JSON.stringify(my_object) })
$.post('/stream/foo.bar', { data: JSON.stringify(my_object) })

A simple POST request, with a parameter called 'data' (or 'json' or 'msg') containing a JSON string.

The topic can be specified other as a post parameter or in the path.

Read more about Server-Sent Events and the EventSource API on HTML5Rocks.

Examples

A simple demo can be seen here: http://meerkat-demo.herokuapp.com/

It's deployed on Heroku's Cedar stack. It's using the AMQP backend, provided by CloudAMQP and the free Lemur plan.

License

(MIT license)

Copyright (C) 2011 by Carl Hörberg

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Rack middleware for Server-Sent Events (HTML5 SSE)

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages