3

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.

jwsc
  • 867
  • 8
  • 15

1 Answers1

2

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

underdog
  • 4,447
  • 9
  • 44
  • 89
  • Maybe I am missing something, but think those asynchronous communication frameworks do the same like the websocket lib I used. They "send messages" plainly speaking. If I want to have Methods executed because of that messages I have to write that by myself. Is that right? For clarification: In a low level, RMI also "sends messages". But there are higher layers built on top of that, which execute the functions or methods specified in the RMI message. Is there a higher level Framework which would do this for me? – jwsc Apr 30 '15 at 07:44
  • Receiving websocket messages from the browser is just like the working of an ajax code, you can write methods on the server side to deal with the data in the request object. Also look into messaging task queues, which run an errand on the backend i'e server side asynchronously. Look into the Atmosphere framework, it provides features whatever you require – underdog Apr 30 '15 at 08:02