javajavascriptjsonrmiinter-process-communicat

javascript to java remote method interface


Background:

Some time ago I worked on a Java Server-Client application (Building automatization was the scope). After a first try, where we wrote both the server and the client in java and connected them over java RMI (Remote Method Interface), we found that writing the client in Javascript would be better for several reasons (I won't give them here, do not matter).

As RMI does not work between Javascript and Java, We then used WebSockets to connect them and get a nearly realtime connection. This was about 3 years ago, websockets were still in early beta with absolutely no documentation and many "not yet implemented" functions. For the protocool part, I wrote something like an own JSON based RMI. I deserialized the JSON objects coming from the client and queried my own server application using java.lang.reflection to get the methods the client wanted to execute.

All that worked good, I had it fairly generic, so I could insert other subclasses dynamically at runtime and my RMI would still be able to access them.

TL;DR:

I always wondered if there is a standard way to connect java to javascript in something like a RMI (like having to extend some interface and being then able to access those interface methods with javascript) I can't be the first one having that issue.

To myself: I am an electrical engineer, so Java, Javascript or Web technologies in general are nothing I have learned in university. I do know what I talk about when it comes to java, but I am a complete noob to javascript.


Solution

  • For achieving asynchronous communication between client & server there are some of the frameworks available like Atmosphere, Spring Reactor, DWF, Spring websockets.

    The basic approach is these frameworks have client side js file which acts as a stub to the server side methods. The frameworks do not allow the request response cycle to close from the browser. They keep on sending the blank messages periodically to keep the connection open & keep the data in sync.

    There are also some of the proprietary API's available in the market to achieve this like Pusher