Event

Edit this Page

Programs can send and receive messages to each other. One way to work with this functionality is to implement the notion of events. With events, you can write code that says "when this happens, run this function".

The larger paradigm of writing code that sends and receives events is called event-driven programming, and is closely related to other messaging patterns like the publish-subscribe pattern.

In this article, we will cover the concept of events using the language of subscribing and publishing, which can be thought of as roughly equivalent to event handlers and event firing, respectively.

Why would you want to use an evented model?

Say you have one program that manages news stories, and another program that reads these bulletins. When a new story comes in, you want the reading program to be notified immediately.

In other words, you want one program to publish stories, and the other program to subscribe to the first. That way, you can satisfy the use case of "when a new story is published, it should appear on the news reader".

Below is an implementation of the publish subscribe pattern:

(in Ruby)

$subscriptions = [] # a store for subscriptions

# create a new subscription for a certain of event
def subscribe(event_type, &callback)
  $subscriptions.push({ :event => event_type, :callback => callback })
end

# distribute event data to all subscriptions for
# the given kind of event
def publish(event_type, event_data = "")
  $subscriptions.each do |subscription|
    if subscription[:event] == event_type
      subscription[:callback].call(event_data)
    end
  end
end

publish(:news)
# Huh, doesn't do much

subscribe(:news) { puts "New story received." }

publish(:news, "President declares national holiday")
# => New story received

subscribe(:news) do |story|
  puts "This just in: #{story}"
end

publish(:news, "Gophers are psychic!")
# => New story received.
# => This just in: Gophers are psychic!

publish(:gossip, "Pope dances tango with Richard Dawkins")
# Hmm, nobody seems to be listening

(in JavaScript)

var subscriptions = []; // a store for subscriptions

// Create a new subscription for a certain of event
function subscribe(eventType, callback) {
  subscriptions.push({ event: eventType, callback: callback });
}

// Distribute event data to all subscriptions for
// the given kind of event.
function publish(eventType, eventData) {
  eventData = eventData || "";
  subscriptions.forEach(function(subscription) {
    if (subscription.event == eventType) {
      subscription.callback(eventData);
    }
  });
}

publish("news");
// Huh, doesn't do much

subscribe("news", function() {
  console.log("New story received.");
});

publish("news", "President declares national holiday");
// => New story received

subscribe("news", function(story) {
  console.log("This just in: " + story);
});

publish("news", "Gophers are psychic!");
// => New story received.
// => This just in: Gophers are psychic!

publish("gossip", "Pope dances tango with Richard Dawkins");
// Hmm, nobody seems to be listening