Remote Procedure Calls (RPC)

As fun as transferring simple strings from one domain to another might sound; is this really what you need? While some applications (like the resize iframe example) might need nothing more, most often what you want is to be able to call methods, with arguments (strings, numbers, JSON objects etc) across the domain boundary like if no boundary existed at all, and guess what, easyXDM offers just that!


The easyXDM.Rpc class is an easy-to-use class that lets you define an interface to expose, and that lets you call methods exposed by the remote end as easy as doing

rpc.someMethod(arg1, arg2, function(result) {
    alert(result);
});

This actually means that the complexity of using cross domain RPC has been reduced to the same level as calling regular methods!
The concrete implementations can be both synchronous or asynchronous (for instance using AJAX to retrieve data), an example of this is from the xhr sample.

new easyXDM.Rpc({}, {
    local: {
        post: {
            method: function(url, data, fn, fnError){
                // do something... 
                // if it is synchronous use 'return foo;'
                // if is it asynchronous use 'fn(foo);'
            }
        }
    }
});

This Rpc object exposes a method ‘post’, that accepts two arguments, a url and a data object.
To make use of this exposed method on the consumer all you need to do is to set up the Rpc object

xhr = new easyXDM.Rpc({
    remote: remoteUrl + "/../xhr.html"
}, {
    remote: {
        post: {}
    }
});

and when ready do

xhr.post("example/glossary.php", {
    param1: "foo",
    param2: "bar"
}, function(data){
    alert(data.glossary.title);
});

Note that if there is a return value then the result is returned using the callback function.
Check out the example here.

This entry was posted in Examples/How-to's and tagged , , , , , , , , . Bookmark the permalink.