Example of JavaScript working with the BR Web Bridge

Discussion about software products provided for the BR community and created by its members. This includes (but is not limited to): MyEdit, File IO, Screen IO, FNSnap, and Utilities written in other languages for use with Business Rules.

Moderators: Susan Smith, admin, Gabriel

Post Reply
Mikhail
Posts: 87
Joined: Tue Jul 07, 2009 10:26 am
Location: Ukraine

Example of JavaScript working with the BR Web Bridge

Post by Mikhail »

Chris, can you provide an example of JavaScript working with the BR Web Bridge?

Maybe you could use the same demo that you used at the conference...

How can I pass data from JavaScript to BR and back?
admin
Site Admin
Posts: 14
Joined: Tue Jul 08, 2008 11:28 pm

Post by admin »

I'll set one up. Probably not today, though.

Here's a quick overview of the process, though.

You need to first understand how a web page sends variables in a request. JavaScript uses the exact same mechanism. But it allow you to do it in code rather than by clicking a link or submitting a form.

The JavaScript language provides a tool for accomplishing this. It is called the XMLHttpRequest object. In short it is a simple API for initiating request to a url and then receiving the response as a string. It also automatically parses a valid XML response into an object.

Once you get this data back you will probably want to display it or change what the user sees somehow. To do this you need to use another JavaScript API, the Document Object Model. This is an abstraction of the page in the form of object called 'document'. You can do just about anything to the page by manipulating this object's properties and invoking it's methods.

Using these concepts in conjunction is referred to DHTML, because you are updating your HTML dynamically rather that regenerating the entire page with each click.

A good place to start learning about this;

http://www.w3schools.com/XML/xml_http.asp
http://www.w3schools.com/htmldom/default.asp
http://www.w3schools.com/dhtml/dhtml_intro.asp

Keep in mind that while it is good to know how it works, in practice you will never use this stuff. There are many excellent tools that simply wrap this functionality into a much simpler API. Once you understand how these things work just move right on to JQuery or similar.

Hope that helps.
Mikhail
Posts: 87
Joined: Tue Jul 07, 2009 10:26 am
Location: Ukraine

Post by Mikhail »

Chris, when you use JavaScript on a page, do you also use the BR PHP bridge?

Or do you pass the data from JavaScript to BR directly bypassing the BR PHP bridge?
Chris Shields
Posts: 22
Joined: Thu Aug 06, 2009 12:19 am

Post by Chris Shields »

JavaScript can only access outside data with an HTTP request. So you still need The Bridge.
Mikhail
Posts: 87
Joined: Tue Jul 07, 2009 10:26 am
Location: Ukraine

Post by Mikhail »

I know that JSON is the format of choice over XML for many developers.

From what I understand, the way to go is to write BR functions that use print statements to generate JSON data.

BR returns this JSON data to adapter.php which in turn returns it to the browser using echo $broutput

The web page accepts JSON and using JavaScript dumps it into some nice looking table or treeview or some other control.

Do I have the right idea? I just want to clear it up in my head before I start.
Mikhail
Posts: 87
Joined: Tue Jul 07, 2009 10:26 am
Location: Ukraine

Post by Mikhail »

Also, is there a problem with unattended copies of BR left running in the background after you close you web page and no longer using the bridge?
Chris Shields
Posts: 22
Joined: Thu Aug 06, 2009 12:19 am

Post by Chris Shields »

I know that JSON is the format of choice over XML for many developers.

From what I understand, the way to go is to write BR functions that use print statements to generate JSON data.

BR returns this JSON data to adapter.php which in turn returns it to the browser using echo $broutput

The web page accepts JSON and using JavaScript dumps it into some nice looking table or treeview or some other control.

Do I have the right idea? I just want to clear it up in my head before I start.
This is all spot on. Sounds like you have the right idea.
Also, is there a problem with unattended copies of BR left running in the background after you close you web page and no longer using the bridge?
No. BR ends before returning any data to the bridge.
Chris Shields
Posts: 22
Joined: Thu Aug 06, 2009 12:19 am

Post by Chris Shields »

Note: This is a response to a mailing list post that was disallowed on the forum because html extensions were not allowed. I enabled them so I am posting the reply on the Forum.
I used the latest version of BR bridge from sourceforge.net.

In the demo directory, I modified index.html to call JavaScript which then should then use PHP bridge to execute a BR program.

I am attaching index.html and the BR program it calls. Those are the only 2 things I added/changed.
Here is your JavaScript test with a few corrections. You were very close.

First thing is you needed was a line numbers in your BR code. They should start at at least 1000 to allow the bridge to add some code to the beginning.

This is only necessary when using source code. I generally call library functions.

The other thing is the 'JSON' object you used is a tool you must include in your page. The Json.org instructions are not clear on this but at the end of the page is a link to the free tool that provides the Json parser.

I replaced this with JavaScript's built-in parser. This function is considered 'unsafe' and inferior to the custom built parsers but is fine for this example.
Attachments
JSON_sample.brs
(81 Bytes) Downloaded 715 times
index.html
(3.43 KiB) Downloaded 805 times
Susan Smith
Posts: 717
Joined: Sun Aug 10, 2008 4:24 am
Location: Southern California

Post by Susan Smith »

Russ, I don't understand very much of the web-scripting bridge biut there is some information about it on the wiki if you haven't seen it:
http://brwiki.ads.net/index.php?title=B ... ing_Bridge

If that link doesn't work, go to the wiki home page and find the link to "Web-Scripting bridge" that is almost at the bottom of the page under the "Not Written in Business Rules!" section.

Hopefully, Chris will see your question and respond. I am moving this question to the new forum so that it gets archived in the proper section since it would be of interest to others.

-- Susan

Russ Seel wrote:
>
> At the risk of asking something everyone knows, what is adapter.php? Anything to do with converting BR to a web based app?
Mikhail
Posts: 87
Joined: Tue Jul 07, 2009 10:26 am
Location: Ukraine

Post by Mikhail »

Chris, I ran the example you sent back, and responseObj.Country is blank and the contents of responseString are:

<?PHP
include ('class/class.brphp.php');
include ('settings.php');

if ( isset ($_REQUEST['BR_MODE']))
{
$mode = $_REQUEST['BR_MODE'];
} else
{
// Default BR execution mode.
// Can be 'libfunc' or 'source'.
$mode = "source";
}

if ( isset ($_REQUEST['BR_SOURCE']))
{
$source = $_REQUEST['BR_SOURCE'];
} else
{
$source = null;
}

if ( isset ($_REQUEST['BR_LIB']))
{
// BR Library Name
$lib = $_REQUEST['BR_LIB'];
} else
{
$lib = null;
}

if ( isset ($_REQUEST['BR_FUNC']))
{
// BR Library function call
$func = $_REQUEST['BR_FUNC'];
} else
{
$func = null;
}

// Create BR Bridge Object
$br = new BrExec();

// Execute br win 'Run' method
$broutput = $br->Run($mode, $source, $lib, $func, $config['debug'], $config['leavetmp'], $config['profile'], $config['cwd']);

// Send BR output back to browser
echo $broutput;

?>


Is this correct?
Chris Shields
Posts: 22
Joined: Thu Aug 06, 2009 12:19 am

Post by Chris Shields »

adapter.php is a script I wrote for arbitrary execution of BR code. You pass it the name of the program or library function and it runs it and returns the output to the.

More specifically, it is a simple PHP script that implements the BR Bridge class. It is executed by an HTTP request from an HTTP client like a web browser with a URL like;

"http:/mydomain.com/adapter.php?BR_SOURCE=myprogram.brs"

Originally I created a new PHP file for every BR program. Quickly realizing how redundant that was, I created one script to rule them all.... muahaha. :twisted:
Chris Shields
Posts: 22
Joined: Thu Aug 06, 2009 12:19 am

Post by Chris Shields »

Mikhail, you are probably just accessing the page incorrectly. Make sure the URL in your address bar starts with http:// rather than file://

This happens when you open a page from Windows Explorer in which case the browser opens the file directly and bypasses the HTTP server (Apache).
Mikhail
Posts: 87
Joined: Tue Jul 07, 2009 10:26 am
Location: Ukraine

Post by Mikhail »

Yes, you were right, I was accessing the page incorrectly.

I got everything working after all.

Thanks!
Post Reply