With the introduction of the Kynetx Sky Event API you can now raise events to all the rulesets installed in your Kynetx Personal Cloud without the need to specify a ruleset. Prior to the release of the Sky Event API events had to be raised to a specific ruleset using the Blue API. This feature of the Sky Event API extends the late binding nature of the Kynetx platform. As a developer you do not need to hard code a ruleset ID. For the end-user you have the freedom to choose which ruleset you want to respond to an event when it is raised.


For the developer you can still raise explicit events with the Sky Event API. Simply add "_api" = "sky" to the attributes of your raise explicit statement. Raising an explicit Sky Event in this manner will result in the event being raised to all of the rulesets that are installed in the end-users Personal Cloud. (Aside: At this time the end-users Personal Cloud is that same thing as their Kynetx account.) As a result all installed rulesets with matching select statements will trigger their rules to fire.

The power of the Sky Event API really comes into focus with the ability to raise events into other Personal Clouds! While broadcasting an event within your own Personal Cloud is cool, raising events into other Personal Clouds over event channels is were the magic of the Sky Event API really excels. Sending events in Parallel is an excellent example of using the Sky Event API in this manner.

The pattern that I want to share with you today is how to run javascript when raising a Sky Event into a Personal Cloud. The starting point for this pattern is provided by the send_event defaction developed by Phil Windley in Sending Events in Parallel. There are two important extensions we will make to send_event. First, in order to return javascript, instead of directives, we must raise our event in the "web" domain. Second, to have the javascript executed we must inject the Sky event signal URL into the DOM. This defaction has been release a part of the public module SkyKit.

raise_delegate = defaction(type, token) {
  configure using attrs = {}

  dom = "web";
  eid = math:random(9999999);
  arg = attrs.keys().map(function(x) {
          uri:escape(x) + "=" + uri:escape(attrs{x});
       }).join("&");
   esl = "http://cs.kobj.net/sky/event/" +
         "#{token}/#{eid}/#{dom}/#{type}?#{arg}";
   {
     emit <<
       var r = document.createElement("script");
       r.src = esl;
       r.type = "text/javascript";
       r.onload = r.onreadystatechange = KOBJ.url_loaded_callback;
       var body = document.getElementsByTagName("body")[0] ||
       document.getElementsByTagName("frameset")[0];
       body.appendChild(r);
     >>;
  }
};

To demostrate this pattern we will install a ruleset into a Personal Cloud with a single notify() action:

rule sayHello {
  select when web sayHello
    notify("Kynetx SkyKit Demo", "Hi, I'm App #1") with sticky = true;
}

Then create an Event Channel Identifier for the Personal Cloud:

f6511bf0-9ba1-012f-7a7a-00163e64d091

And finally we will create a ruleset that use raise_delegate():

rule testme {
  select when pageview
    pre {
      eci = "f6511bf0-9ba1-012f-7a7a-00163e64d091";
    }
    {
      notify("Kynetx SkyKit Demo", "Hello, World!") with sticky = true;
      SkyKit:raise_delegate("sayHello", eci);
    }
}

This will not be installed into the Personal Cloud, but will be exeucted as a bookmarket. You can take it for a test drive on Heroku.



Enjoy!