Tuesday, February 14, 2012

Events

Before reading this please read part 1:
http://randomgoo.blogspot.com/2012/02/release-event-hub-web-30-introduction.html

Quick recap - Web 3.0 is two-way asynchronous programming - events on the way out and another event or a callback on the way back.

So let's look at the event side of Web 3.0.

Event Syntax

  • Events Must have a string name
  • Events Might have an optional JSON-able set of parameters.
  • Events Might have an optional callback.

Event Names


Event names are strings, probably namespaced, separated from the namespace with a colon ':'.  It could also be a good idea to put a version number in there too if ya like, this can help keep your code in sync.  Up to you, play with it.

Here are some good event names:

user:add
user:delete
user:update
user-1.0:add    # module versioned
user:add-1.0    # listener versionsed
session:create

Event Parameters


Event parameters are serialized as JSON text so no function pointers or other funny business.  Toss in there whatever you need.  An important key is a session key.

Event Callback

Providing an optional callback allows event listeners to respond directly, asynchronously, back to the event emitter with data just for it.  The callback function should accept two paramters, a possibly null error object (or string) and a (possibly null) object with data for the emitter.

Check it:

hub.emit('user:add', { username: 'foobie', password: 'bloobie' },
    function(error, resp) {
        if (!error) {
            // sweet
        } else {
            // no dice
        }
    }
);

Look ma - no dependency on a 'user' object!

Event Semantics

Events are prayers that we expect to be answered.  It's possible they don't get answered.  What happens when events do not get answered?  Life goes on.

We'll look at event listeners next time...
(and still to come: the event hub itself, programming languages, and an implementation and experimentation)

No comments:

Post a Comment