Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Event conventions

afcapel edited this page · 1 revision
Clone this wiki locally

Event conventions

Alondra uses convention over configuration to determine the channel to post an event. This means it comes with sensible defaults, but if you don't like default settings you can easily override them.

Publishing events

Say you have a Chat model and you want to push chat changes to the browser. Just write,

  class Chat
    push :changes

This will push the following events:

Event Channel
created /chats/
updated /chats/:id
destroyed /chats

If you want to push certain types of event, you can also specify them

  class Chat
    push :creations, :updates

If, on the other hand, you want to change the channel in which an event is published, you can also choose it

  class Message
    belongs_to :chat

    push :creations, :to => :chat
    push :updated, :to => 'Some custom channel'

Listening to events

To react to certain events in your app, just create an event listener in your app/listener dir. They also follow certain conventions, but if you don't like them you can define your own.

If you use the conventions you can react to some predefined events. As an example, look at the following listener:

   # Since the listener name is ChatListener it will listen to all events published to a channel
   # whose name begin with '/chat' unless you specify otherwise.
  class ChatListener < EventListener

    # React to any client subscribed.
    on :subscribed do

    # React to any client unsubscribed, like when the client leaves or the connection is loose
    on :unsubscribed do

    # React only to clients subscribed to the channel '/chats'
    on :subscribed, :to => :collection do

    # React only to clients unsubbscribed to the channel '/chats'
    on :unsubscribed, :to => :collection do

    # React to subscriptions to individual chats, that is
    # channels of the form, '/chat/:id'
    on :subscribed, :to => :member do

    # React to clients unsubscribed to individual chats, that is
    # channels of the form, '/chat/:id'
    on :unsubscribed, :to => :member do

    # You can also define custom events and listen to them
    # React to any custom event in any channel beginning with '/chat'
    on :custom do

To define your own custom listener you could create an event listener like this:

  class UnconventionalListener < EventListener

    listen_to 'The Real Truth'

    on :haiku do

This UnconventionalListener will only listen to :haiku events in 'The Real Truth' channel.

Something went wrong with that request. Please try again.