title: communication between node.js and java server on localhost
body: <p>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. </p>
  
  <p>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? </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Ok since <a href="https://stackoverflow.com/users/816620/jfriend00">jfriend00</a> and <a href="https://stackoverflow.com/users/2991594/pelit-mamani">Pelit Mamani</a> answered me I've made some progress. I decided to go with <a href="https://stackoverflow.com/users/816620/jfriend00">jfriend00</a>'s solution to use socket.io. Here is exactly what I would like to do (here in javascript):</p>
  
  <ul>
  <li><p>I have a server (the one that I would like to have in Java, here in javascript)</p>
  
  <pre><code>var http = require('http');
  
  console.log('Creating HTTP server');
  
  var server = http.createServer(function(req, res){
  });
  
  var io = require('socket.io').listen(server);
  
  io.sockets.on('connection', function(socket) { 
  
      console.log('Client connected.');
  
      // Disconnect listener
      socket.on('disconnect', function() {
          console.log('Client disconnected.');
      });
  
      socket.on('myEvent', function (data, fn) {
          console.log(data);
          console.log('\tSending answer ...');
          fn('\tI am the answer');
      });
  });
  
  server.listen(8080);
  </code></pre></li>
  <li><p>And I have a client (in my case it will be my nodejs server):</p>
  
  <pre><code>// Connect to server
  var io = require('socket.io-client')
  var socket = io.connect('http://localhost:8080');
  
  socket.on('connect', function () {
  
      console.log('Connected!\n\tSending query ...');
  
      socket.emit('myEvent', '\tI am the query', function (data) {
        console.log(data); 
      });
  
  });
  </code></pre></li>
  </ul>
  
  <p>The client send a query, the server answer the query, simple. I'm using <a href="https://github.com/mrniko/netty-socketio" rel="nofollow">netty-socketio</a> to implement the server part in java. Here is what I have right now: </p>
  
  <pre><code>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&lt;String&gt;() {
              @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");
      }
  
  }
  </code></pre>
  
  <p>When running this server with my javascript client, everything is working:</p>
  
  <ul>
  <li><p>On the client side:</p>
  
  <pre><code>Connected!
      Sending query ...
  I am the answer from the Server!
  </code></pre></li>
  <li><p>On the server side:</p>
  
  <pre><code>server started
  client connected !: com.corundumstudio.socketio.transport.NamespaceClient@ecac7898
  myEvent triggered
  Here is the query from the client: 
      I am the query
  client disconnected !: com.corundumstudio.socketio.transport.NamespaceClient@ecac7898
  </code></pre></li>
  </ul>
  
  <p>But I don't think AckRequest should be used to send data like that. I will probably change for another implementation. To be continued... </p>