Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
2  
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. –  DaveRandom Oct 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 Hudec Oct 24 '12 at 12:22
    
Why are you porting? Perhaps there's a better way to solve your problem on the JVM –  Martijn Verburg Oct 24 '12 at 13:11
    
Hey daverandom, thanks for bringing this up I haven't idea about the differences. –  Poliquin Oct 24 '12 at 13:50
    
Hey jan hudec, you are right. It's between event driven vs threaded. –  Poliquin Oct 24 '12 at 13:51
show 1 more comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
Hey Tom,I am not looking at a direct port with threads and all. I m looking to implement my app in nodejs, using nodejs programming constructs. –  Poliquin Oct 24 '12 at 13:56
add comment

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?

share|improve this answer
    
It's a challenge. ;) –  Poliquin Oct 24 '12 at 13:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.