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?

Abstracted from your specific task, I think you need to fully understand the difference between Functional programming (Javascript/Node.js) and Imperitive programming (Java) before you start trying to actually write anything. Google is (as always) a good place to start and one of the places it took me is here.
–
DaveRandomOct 24 '12 at 11:51

@DaveRandom: While Javascript/Node.js makes using functional constructs much easier than Java, the difference here is between event-driven (Node.js) and threaded (current application).
–
Jan HudecOct 24 '12 at 12:22

Why are you porting? Perhaps there's a better way to solve your problem on the JVM
–
Martijn VerburgOct 24 '12 at 13:11

Hey daverandom, thanks for bringing this up I haven't idea about the differences.
–
PoliquinOct 24 '12 at 13:50

2 Answers
2

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.

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?