Day 8 – Raise Web Events From JavaScript

The goal of this post is to rock your socks! : ) Everything that has been shown up to this point has been a one way communication with the Kynetx platform and the Kynetx app user’s browser. The bookmarklet, when clicked, would raise a web event letting the Kynetx server that a pageview event had occurred. The Kynetx server would then respond, based on the rules in the ruleset, with JavaScript.

I am going to show you how you can now have your Kynetx application carry on a CONVERSATION with the Kynetx server, greatly enhancing the breadth and power of what you can do with a Kynetx application. Here is a fun little diagram I had fun making to illustrate what we are going to be doing:

ruleset a60x488 {
  meta {
    name "raising-custom-web-events"
    description <<
      raising-custom-web-events
    >>
    author "Mike Grace"
    logging on
  }

  rule run_on_a_pageview {
    select when pageview ".*"
    {
      notify("Hello","I ran on a pageview") with sticky = true;
      emit <|
        app = KOBJ.get_application("a60x488");
        app.raise_event("custom_event_just_for_me", {"answer":42});
      |>;
    }
  }

  rule respond_to_custom_event_raised_from_emitted_js {
    select when web custom_event_just_for_me
    pre {
      answer = event:param("answer");
    }
    {
      notify("What is the answer?",answer) with sticky = true;
    }
  }
}
  • 12 first rule selects on all URLS
  • 14 will show a simple notify on the page to let us know the JavaScript from the first event has returned
  • 15-18 emitted JavaScript to raise custom web event
  • 16 getting the app object that has method for raising web events
  • 17 calling raise_event() function to raise ‘custom_event_just_for_me’ web event with the parameter ‘answer’ with a value of ’42’
  • 23 second rule only responds to web domain events with the name of ‘custom_event_just_for_me’ so this is why it didn’t run on the pageview event
  • 24-26 pre blocks run on the Kynetx server
  • 25 setting the value of ‘answer’ to the parameter passed in with the event with the name of ‘answer’
  • 28 showing notify with value passed in raised event

SO COOL!*

So what happened?

Clicking on the bookmarklet raised a web pageview event with the following URL:

http://cs.kobj.net/blue/event/web/pageview/a60x488/129247673335346563160978257656?kvars=%7B%7D&caller=http%3A%2F%2Fexample.com%2F&referer=&title=Example+Web+Page&endpoint=[object%20Object]&a60x488:kynetx_app_version=dev

This event was processed by the Kynetx server and only the first rule in the ruleset was selected. The JavaScript to show the first notification was returned along with the JavaScript to raise the custom web event. Once the JavaScript loaded on the page and executed, a custom web event was raised with the following URL:

http://cs.kobj.net/blue/event/web/custom_event_just_for_me/a60x488/1292476734735036077062832191586?answer=42&element=unknown&kvars=%7B%7D&endpoint=[object%20Object]&a60x488:kynetx_app_version=dev

The custom web event went to the Kynetx server, was executed, and then returned the second notify with the value that was passed to it.

App run on example.com with a bookmarklet:

Get the bookmarklet to try it out yourself!

*Gratuitous day 8 crazy face was hidden in todays post. Did you find it?

About these ads
This entry was posted in Kynetx and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s