Look Ma, no URL Calls!

What if we never had to make any URL calls from our javascript to access our backend APIs. Instead, if we had a model User with the class method create(name) then in JS we would just say user.create("Harry Potter") and that’d run this method on a server and return a result to JS сlient and it’d looked as if a function was defined in JS. Oh, I know you know that it’s called RPC and existed for a thosands of years, got wiped out by REST and generally considered as bad practice. But why? Wouldn’t be more convinient than dealing with URLs and controllers? Web application is just like any other distributed application consists of server and many clients that are talking to each other. Passing messages and making remote procedure calls seems way more natural than hitting URL endpoints. URLs are understandable for people, but why make software talk this language?

To test this idea I made a simple lib https://github.com/yankov/nourl. You define a class on your backend side and it looks like this:

1
2
3
4
5
6
7
8
9
class User
  include Nourl::RPCable

  allow_rpc_for :get

  def self.get(name)
    User.find_by_name(name)
  end
end

Then you run your server and in your JS it can be written like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
var settings = {
  rpcUrl: "http://0.0.0.0/rpc", //host and port where your server is running
  transport: "ajax",
  require: ["user"]                 // list all classes that you wanna access to
}

nourl.run(settings, function(){

   user.get('john', function(result) {
      console.log(result);
   });

});

Nourl automatically creates stubs for your model’s classes and methods, so you can just run a method call from JS, pass arguments and get the result back from your backend server. The point is that all RPC and Ajax logic is hidden and everything just looks like model was already implemented in javascript.

Especially taking in account that websockets probably are gonna be used more and more, I don’t see yet why this pattern wouldn’t work.

Some unnecessary text that tries to make post look cooler

A known fact: sometimes we keep doing things in an old way just because we get used to it even if it’s inconvinient. Handling this inconvinience quickly becomes a part of the standard routine which builds up our comfort zone. Usually it takes time to break the old layer of habbits and adapt to the new ideas. This is why people don’t care about craigslist’s UI although it doesn’t conform to any modern standards. Try to optimize it and people will rage (for a while).

So I was just trying to find things that we are doing in web-development that seem odd or obsolete, but to which we got used to and accepted as standards. One of such things is REST and using URLs for accessing APIs in general. Well, probably saying that REST is bad would be too arrogant, but there just could be other ways to go.

Remember, there was stuff like SOAP, RPC, Corba, RMI probably more familiar for people from the world of enterprise distributed software development. For a bunch of reasons it didn’t kick in web development: it was too cumbersome to deal with and not very natural for HTTP. But since we are moving on, web-applications more and more look like desktop application, websockets are gonna be widely spread out very soon, we probably have to find better ways to communicate clients with the servers rather than hitting URL endpoints.

Comments