• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Javascript threading question

So im still thinking about technologies for the website im gonna put together, im becoming sold on JavaScript for the front and the backend with node.js and using JSON to communicate between the two. JavaScript is a language I don't know a lot about (learned java at uni) except from the codecademy course I did which gave me a basic understanding of it.

My question is, what is the JavaScript equivalent for threading? In Java to serve many users at once a ServerSocket creates a Socket object for that user and then that Socket is passed off onto its own newly created Thread so the ServerSocket can go back to listening for clients. Whats the JavaScript equivalent to that behavior?
 
I know some versions of JavaScript have Web Workers. I really don't understand how to use them, though - especially how to get information back from them - and I have no idea how/if they work on the server side.
 
i've never done anything thread specific in javascript, and a quick google search basically comes to the conclusion that there is no such thing as threads in javascript and it all runs in one thread.

however there are ways to get work done similar to threading, by using either setTimeout, or the concept known as promises.

i'm not sure what exactly you need to do and have never done any serversocket stuff (and have no clue what it is), but what exactly are you trying to do that you think you need threads for?

promises are a very powerful tool in javascript that can kind of give you functionality similar to a thread because it can do stuff asynchronously.
 
I think im thinking about this all wrong tbh :|

The webserver aka apache tomcat or whatever deals with all the concurrency stuff right? Im just in the planning stages of making a website, im gonna try and get a simple login system working just to see how client and server are supposed to communicate, i.e. what methods im meant to use. If I can get that down then I can build the rest of it, database and all the other stuff.

As for javascript yeah seems it uses one thread very intelligently and ignores slow I/O operations until its finished with other stuff. Callbacks or something.
 
In my experience. None of the promises of "use the same language on the backend and frontend" really pan out. What the backend does and what the front end does are fundamentally different. As a result, very little logic and even objects are shared.

In particular, I find javascript to be a poor language for backend development. I think node js running on the server is a mistake TBH. But if you really want to do it, then the way most do it is by pretty much doing no threading in node js and instead spinning up several procs and reverse proxying onto it (ala nginx). It is a little bulky, but it is effective and scales really well.

Here are some examples and suggestions on how to get this done

https://www.digitalocean.com/commun...n-a-single-vps-with-nginx-forever-and-crontab

https://gist.github.com/joemccann/644282

http://www.sitepoint.com/configuring-nginx-ssl-node-js/
 
Last edited:
Yeah im becoming less sold on javascript tbh, it seems like its much easier for a newb like me to screw up should I mismanage the callback thing.

Ive been following this guys book on making a basic website (well webserver) with javascript: http://www.nodebeginner.org/

Are there any resources like this for java EE? Ive been making a "Java Web Application" in netbeans in parallel with following that book so I can get a feel for both technologies but im hitting brick walls on what classes/objects/methods to use in java EE or even how the thing actually works. I mean do I need a servlet to sit and listen for requests on a port? Or does apache tomcat do that and communicate to a request handler servlet? Basic stuff eludes me 😱

Also regarding JSPs... I can put java code in <% %> tags and I can put javascript in <script> </script> tags right? Any reasons to pick one over the other?
 
I mean do I need a servlet to sit and listen for requests on a port? Or does apache tomcat do that and communicate to a request handler servlet?
Tomcat handles all the low-level sockets stuff. It'll run your code if it gets a GET or POST with a matching URL.

Also regarding JSPs... I can put java code in <% %> tags and I can put javascript in <script> </script> tags right? Any reasons to pick one over the other?

Java code runs on the server. JavaScript code runs on the client machines. You can make all the computers that visit your website do work for you! :twisted: But you're limited in how much evil you can do by things like sandboxing and the same-origin policy.
 
Yeah im becoming less sold on javascript tbh, it seems like its much easier for a newb like me to screw up should I mismanage the callback thing.

Ive been following this guys book on making a basic website (well webserver) with javascript: http://www.nodebeginner.org/

Are there any resources like this for java EE? Ive been making a "Java Web Application" in netbeans in parallel with following that book so I can get a feel for both technologies but im hitting brick walls on what classes/objects/methods to use in java EE or even how the thing actually works. I mean do I need a servlet to sit and listen for requests on a port? Or does apache tomcat do that and communicate to a request handler servlet? Basic stuff eludes me 😱

Also regarding JSPs... I can put java code in <% %> tags and I can put javascript in <script> </script> tags right? Any reasons to pick one over the other?

The trend has been towards doing less and less on the server and more and more on the client side when it comes down to "how the is this displayed".

The likes of Angular, Ember, React, Knockout, etc, are all dedicated to taking data from the server and making it look right.

On the server side, the server is pretty much dedicated to only pushing out JSON data.

If I were to make a webpage today, on the backend I would probably have nginx on the front serving up static content but also reverse proxying to my backend server/s. My backend servers would be Java + Jersey + Jetty ( http://dropwizard.io/ ) and my frontend (static files) would be written in either Angular, Ember, or React.
 
Back
Top