user3091275
user3091275

Reputation: 1023

communication between node.js and java server on localhost

I have a web application built using nodejs. The backend is calling several java command lines to answer queries. Those java programs are relying on models that are quite huge (around 50Mo), and for each java command line executed the model needs to be reloaded. In the end, most of the answer time is spent on loading these models in RAM.

What I would like to do is to set-up a java server along with the nodejs one, and make those two servers communicate. The java server would keep the models in RAM and handle all the java processing. The problem is that node is asynchronous and I would like to fire a callback when my java server ended processing the data. Do you know any good way to do this? Would it be possible to send some sort of POST request from node to java on localhost?

EDIT:

Ok since jfriend00 and Pelit Mamani answered me I've made some progress. I decided to go with jfriend00's solution to use socket.io. Here is exactly what I would like to do (here in javascript):

The client send a query, the server answer the query, simple. I'm using netty-socketio to implement the server part in java. Here is what I have right now:

import java.io.UnsupportedEncodingException;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.Transport;
import com.corundumstudio.socketio.listener.*;

public class App {

    public static void main(String[] args) throws InterruptedException, UnsupportedEncodingException {

        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(8080);

        final SocketIOServer server = new SocketIOServer(config);

        server.addConnectListener(new ConnectListener() {
            @Override
            public void onConnect(SocketIOClient client) {
                System.out.println("client connected !: "+client.toString());
            }
        });

        server.addEventListener("myEvent", String.class, new DataListener<String>() {
            @Override
            public void onData(final SocketIOClient client, String data, final AckRequest ackRequest) {
                System.out.println("myEvent triggered");

                System.out.println("Here is the query from the client: \n"+data);

                ackRequest.sendAckData("I am the answer from the Server!");
            }
        });

        server.addDisconnectListener(new DisconnectListener() {
            @Override
            public void onDisconnect(SocketIOClient client) {
                System.out.println("client disconnected !: "+client.toString());
            }
        });

        server.start();
        System.out.println("server started");

        Thread.sleep(20000);//Integer.MAX_VALUE);

        server.stop();
        System.out.println("server stopped");
    }

}

When running this server with my javascript client, everything is working:

But I don't think AckRequest should be used to send data like that. I will probably change for another implementation. To be continued...

Upvotes: 3

Views: 8649

Answers (1)

Pelit Mamani
Pelit Mamani

Reputation: 2381

  1. If you use http or tcp, the fact that node.js is asynchronous doesn't prevent the java side from working "synchronously"... e.g. if you have a standard Servlet that reads a request and writes a reply (on the same thread) , it would still translate to standard asynchronous code on node.js side. It would send the request, then get a callback when java answers...

  2. As a side note, you might also want to have a look on asynchronous Queuing protocols such as Rabbit MQ, but that goes beyond the basic requirements you described. E.g. it depends on how reliable you need the communication to be, what happens if the java server crashes, etc.

Upvotes: 1

Related Questions