Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How client side Handle node.js with multiple response from the server

How client side Handle node.js with multiple response from the server


It seems that the server(node) has a thread like behavior since it handles synchronous requests, but we know that javascript on the client side doesnt support multi-threading. Im just asking how the client side(javascript) Handle node.js especially with multiple responses from the server? for an example i want to render two objects at the same time.

Is it appropriate to make a fast phased realtime game app with node.js?

Are there ways or patterns to handle this?

Problem courtesy of: Jude Calimbas


Think of node as a commander, it handles the monitoring of task without any blocking. So if you think of it like that, node does not need to be multi threaded, it executes tasks without any wait time, therefore there is no need to create a new thread as nothing is blocked.

You mention about sockets in your comment, think of this, Node has a network thread that monitors the TCP/UDP connection for data, when it receives a packet it mealy ask's your node script what you would like done with this packet, you instantly tell the network thread to send this new packet out as a reply and then the network thread goes of to do that while then next event is called.

Take a look at the following image and paragraph, this should explain

In node.js, you aren’t supposed to worry about what happens in the backend: just use callbacks when you are doing I/O; and you are guaranteed that your code is never interrupted and that doing I/O will not block other requests without having to incur the costs of thread/process per request (e.g. memory overhead in Apache).

Having asynchronous I/O is good, because I/O is more expensive than most code and we should be doing something better than just waiting for I/O.

enter image description here

An event loop is “an entity that handles and processes external events and converts them into callback invocations”. So I/O calls are the points at which Node.js can switch from one request to another. At an I/O call, your code saves the callback and returns control to the node.js runtime environment. The callback will be called later when the data actually is available.


Be sure to read that article it was the first article that made me fully understand what was going on with the back-end architecture.

Solution courtesy of: RobertPitt


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How client side Handle node.js with multiple response from the server


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription