Using easyXDM to enable cross-domain json-rpc

291 views
Skip to first unread message

Dave

unread,
Aug 31, 2010, 6:28:03 PM8/31/10
to easyxdm
I'm looking for a solution to enable simple cross-domain json-rpc, and
easy XDM would seem to fit the bill, but I can't quite wrap myself
around the correct way to use it in my case. Most of the examples show
sending of function calls / data between browser windows/frames, but
that's not quite what I want to do.

Here is a description of what I'm trying to do:
-I have a web service that speaks json-rpc (it's actually an embedded
web server)
-I want to be able to call the web service from javascript on any
arbitrary web page (no restrictions on origin/domain)

I can see how to use XDM to make an rpc call from a javascript file on
one domain to a javascript file on the other domain, but I don't see
how I would get it to call my web service directly.
I could see a scenario where I had a javascript file on my webservice
domain that used XHR to make json-rpc request to the now "local" web
service, and returned the result via XDM, however it seems like to
implement that I'd have to write a "wrapper" for every json-rpc
function I wanted to proxy. I was hoping there would be a way to
transparently proxy json-rpc requests/responses directly to my web
service without writing a function-by-function wrapper.

Hopefully I'm just missing something simple. If you could point me in
the right direction it would be appreciated.

Øyvind Sean Kinsey

unread,
Aug 31, 2010, 6:38:45 PM8/31/10
to eas...@googlegroups.com
Inline
On Wed, Sep 1, 2010 at 12:28 AM, Dave <wri...@gmail.com> wrote:
I'm looking for a solution to enable simple cross-domain json-rpc, and
easy XDM would seem to fit the bill, but I can't quite wrap myself
around the correct way to use it in my case. Most of the examples show
sending of function calls / data between browser windows/frames, but
that's not quite what I want to do.

Here is a description of what I'm trying to do:
-I have a web service that speaks json-rpc (it's actually an embedded
web server)
-I want to be able to call the web service from javascript on any
arbitrary web page (no restrictions on origin/domain)

I can see how to use XDM to make an rpc call from a javascript file on
one domain to a javascript file on the other domain, but I don't see
how I would get it to call my web service directly.
That is not possible either, but see below.
 
I could see a scenario where I had a javascript file on my webservice
domain that used XHR to make json-rpc request to the now "local" web
service, and returned the result via XDM,
This is the straight-forward approach which is shown by such examples as the cross domain ajax sample.
 
however it seems like to
implement that I'd have to write a "wrapper" for every json-rpc
function I wanted to proxy. 
I see how that would seem unnecessary, and easyXDM is actually prepared for such events

I was hoping there would be a way to
transparently proxy json-rpc requests/responses directly to my web
service without writing a function-by-function wrapper.

Hopefully I'm just missing something simple. If you could point me in
the right direction it would be appreciated.
 This isn't an official feature (yet) but instead of the 'local' section in the RPC config, you can add a method called 'handle'.
This will be passed the raw JSON-RPC object, and a callback function for you to use (do not use it if it's a notification).

This should let you create a simple wrapper that forwards arbitrary JSON-RPC request to the server and returns the response.

Does this fit the bill?

Sean

Øyvind Sean Kinsey

unread,
Aug 31, 2010, 6:52:54 PM8/31/10
to eas...@googlegroups.com
On Wed, Sep 1, 2010 at 12:28 AM, Dave <wri...@gmail.com> wrote:
... 
I could see a scenario where I had a javascript file on my webservice
domain that used XHR to make json-rpc request to the now "local" web
service, and returned the result via XDM, however it seems like to
implement that I'd have to write a "wrapper" for every json-rpc
function I wanted to proxy.
 
This isn't completely true either - many are currently using it similarly to what you are describing by wrapping the entire call.
Instead of exposing each server side method as a client side method in the rpc config, just expose a single 'jsonRpc' method, which takes a 'method' and 'arguments' as arguments - pass these along to the server as a properly formatted request and return the response.  

Sean

Dave

unread,
Aug 31, 2010, 8:39:38 PM8/31/10
to easyxdm

>
> This isn't completely true either - many are currently using it similarly to
> what you are describing by wrapping the entire call.
> Instead of exposing each server side method as a client side method in the
> rpc config, just expose a single 'jsonRpc' method, which takes a 'method'
> and 'arguments' as arguments - pass these along to the server as a properly
> formatted request and return the response.
>

While this would simplify the "middle" layer, it would seem to
complicate the client-side now, since it would be responsible for
properly formatting the json-rpc request and parsing the response,
defeating much of the purpose of having an rpc layer abstraction.
I really just want to be able to write:
server.method(param1, param2, param3)... on the client, and have
everything "just work". The pointer you sent about the "handle" method
sounds like it might allow me to accomplish that, so I'll play around
with that a bit. Thanks.

-Dave

Øyvind Sean Kinsey

unread,
Sep 1, 2010, 12:22:04 PM9/1/10
to eas...@googlegroups.com
Did you find an approach here that fits with your architecture? (There a multiple ways of doing this).


Øyvind Sean Kinsey
oyv...@kinsey.no
http://kinsey.no/blog/index.php/about/

Dave

unread,
Sep 1, 2010, 12:33:55 PM9/1/10
to easyxdm
I think I've got things working using the undocumented "handle"
method. For reference, here's what I'm using, dropped into xhr.html on
"remote" server. Let me know if you see anything that looks incorrect.


var remote = new easyXDM.Rpc({
local: "name.html"
}, {
handle : function(data, _send)
{
var used = false, success, error;
if (data.id) {
success = function(result){
if (used) {
return;
}
used = true;
_send(result);
};
error = function(message){
if (used) {
return;
}
used = true;
var msg = {
id: data.id,
error: {
code: -32099,
message: "Application error: " + message
}
};
if (typeof message == "object" && "data" in message)
{
msg.error.data = message.data;
}
_send(msg);
};
}
else {
success = error = emptyFn;
}
easyXDM.ajax({
url : "MYWEBSERVICEENDPOINT",
method: "POST",
data: easyXDM.getJSONObject().stringify(data),
type: "json",
success: success,
error: error
});
}
}
});


On Sep 1, 12:22 pm, Øyvind Sean Kinsey <oyv...@kinsey.no> wrote:
> Did you find an approach here that fits with your architecture? (There a
> multiple ways of doing this).
>
> Øyvind Sean Kinsey
> oyv...@kinsey.nohttp://kinsey.no/blog/index.php/about/

Øyvind Sean Kinsey

unread,
Sep 2, 2010, 3:55:34 AM9/2/10
to eas...@googlegroups.com
The only thing I see is the ref to 'emptyFn', which isn't defined anywhere.(Function.prototype can be used).
So if it works for you, then great :)
Reply all
Reply to author
Forward
0 new messages