Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A Sinatra app that uses the keen gem to publish events asynchronously
Ruby JavaScript CSS
branch: master

Merge pull request #2 from simplyaubs/master

Add a redirect for example app
latest commit 6a78470742
@simplyaubs simplyaubs authored


This example Sinatra app shows how to use the Keen IO Ruby gem to publish asynchronous events via the Keen IO API.

See it in action at

The app lets you compare synchronous and asynchronous response times, and vote for your favorite Star Trek TNG character!


To play with your own copy, follow these steps:

Make sure you have a Keen IO Account. Create a new project and note its Project ID and API key.

Clone this repository. cd into the new directory. Run bundle install to install dependencies.

Set the KEEN_API_KEY and KEEN_PROJECT_ID variables in a file called .env in the project root. This file is used by foreman.


Use foreman start to start the server.

Navigate to http://localhost:5000/ and start sending events!

Bonus: If you watch the server log as you send events, you can see the exact moment that calls return, both in synchronous and asynchronous cases.

How it works

When publishing events via publish_async, the Keen IO Ruby gem uses em-http-request to make non-blocking calls. This shifts the calls to the event loop, removing them from the critical path for the request.


Do I need an EventMachine server like thin to use the asynchronous calls?

No. In that case, just add eventmachine and em-http-request to your Gemfile and start EventMachine in a separate thread. Put this code in an initiaizer: {

For more background, here's a blog article about using EventMachine and Passenger.


If you have any questions, bugs, or suggestions, please report them via Github Issues. Or, come chat with us anytime at We'd love to hear your feedback and ideas!

Something went wrong with that request. Please try again.