Open moll opened 10 years ago
Doing that would break the rpc.methodList
built-in mechanism that the client uses to auto-populate the methods you can call.
I suppose I don't see what's so onerous about binding all of your methods to their parent objects and attaching them to a hash table? You could even do it with an IIFE:
new JsonRpc.server(transport, (function() {
var scope = {};
// All methods to be added
Object.keys(obj1).forEach(function(key) {
scope[key] = obj1[key].bind(obj1);
});
// Subset of methods to be added
scope.foo = obj2.foo.bind(obj2);
scope.bar = obj2.bar.bind(obj2);
return scope;
})());
Also, once you're making your JSON-RPC server in the fashion you've described, you're not really doing anything "RPC" at all, and you may as well use a simple REST interface with an express / browser-request pair.
REST has some overhead involved, but its more universally compatible than JSON-RPC, you can automatically load-balance with HAProxy and cache with varnish, so the horizontal scaling story is better.
I view JSON-RPC as primarily for when you want to affect the state of this particular server. The entire server can be viewed as an object inside your code that you instantiate (and it has some state on "initialization" in your code) and the object has methods you can call. What these methods are should be explicit just like any other object in your code, and that's why I don't really understand the desire for a generic request handler for such a beast.
I'm willing to be convinced, though, and it should be trivial-enough to add the functionality. :)
Hey again. Thanks for the prompt reply.
I'm exposing my whole domain's repositories via RPC and there are too many methods and functions (incl. deeply nested) to bother iterating over them. Plus they're promise based and not in the callback style, so that would mean a buttload of pre-generated functions that might never be needed.
I'm not too keen on the overhead of HTTP just so I could tag it REST and drink the koolaid alone. :) JSON-RPC seems more or less also something one could use from different internal services regardless of languages. At least it's not a custom binary format. (Although Multitransport's implementation of TCP seems to be. :)) I'm going for internal RPC for SOA.
Anyways, I managed to subclass the server and rewrite handleJSON with a simpler implementation that did just what I wanted and beautifully so. Disabled the autoregistering thing as well. So far so good.
It's up to you if you feel like adding it to the core, though. :)
Ahh, so you're one of those promise people... ;)
So on a few points you bring up:
Thank you for a great project, I was up and running with the express middleware transport in a short time.
I don't understand your point 3. You seem to be implying that json-rpc should only be used in a corba-style rpc call that targets a stateful model instance, but certainly that's not the case, is it?
I like json-rpc as the core SOA protocol because it allows me to easily expose the stateless service facades of my back-end without having to do semantic contortions to fit all the calls into the CRUDish POST/GET/PUT/DELETE semantics of a REST-style interface. I still plan on exposing a rest API, as a convenience to the web services consumer, in those cases when the semantics make sense.
Sorry for the digression in this bug report. Thanks again for a great project.
Hey,
How about skipping the whole
scope
limitation and just allowing a single function to be passed to the server to handle method calls? That would make it easier to delegate to preexisting objects and handle larger API more conveniently.Something like:
I do see it's possible to overwrite
handleJSON
with some trickery (given that it's bound tothis
in theJSONRPC
constructor), but that would require also duplicating the error handling functionality.