I want to port an existing app from Java to Node.js – would like to seek your advise.
The java app is a multithreaded socket server where arbitrary number of clients can connect. The app runs on java executors. Each client is given a socket to connect to the app.
The client will connect to the socket server and there will be handshake. After handshake, the data will be transmitted between the client and server. The data transferred is in sequential order because the client sends the data over as such. And I.m able to control the thread that the client is connected on.
Now, Node.js does not have threads so I am confused on how to port the multithreaded Java socket server over. Any ideas?
6
There isn’t really any way to “port” the application. The best approach would be to model the existing behavior then write a completely new Node application that conforms to that behavior.
JavaScript and Java are hugely different programming models. JavaScript isn’t strongly typed and relies heavily on an asynchronous programming model. Trying to do a direct port would give you awful code and you would lose out on a lot the features that make Node good.
1
Node.js does not have threads, but it also does not have blocking sockets or blocking anything for that matter. It all runs in an event loop and dispatches callbacks as data arrive. So you need to restructure the code so that each socket call set a function to continue the processing. This is called “continuation passing style”.
It means really big restructuring. Do you really have a compelling reason to spend that amount of time to port the application to a slower and less mature runtime?
1