Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does NodeJS handle persistent connections without WebSockets?

I'm really new to NodeJS (and I'm sorry if I sound naive about some stuff) and I've been digging into the source code of the example Chat Application.

However, I'm having trouble understanding one thing. I know that WebSockets helps handle persistent full-duplex bi-directional connections. But how does NodeJS manage a persistent connection in the aforementioned chat application without the use of WebSockets? And if NodeJS can handle a persistent bi-directional connection, what exactly is the function of integrating something like Socket.IO in Node?

like image 325
0xff0000 Avatar asked Mar 25 '11 04:03

0xff0000


People also ask

Does Node use WebSockets?

js actually supports WebSockets on both the server and client sides. Next, we'll have you create a WebSocket server in Node. js that will accept connections and feed data to clients. Finally, we'll implement a WebSocket client in Node.

How many WebSocket connections can NodeJS handle?

The theoretical limit is 65k connections per IP address but the actual limit is often more like 20k, so we use multiple addresses to connect 20k to each (50 * 20k = 1 mil).

What is SockJS Node?

SockJS-node is a Node. js server side counterpart of SockJS-client browser library.

When should you use NodeJS and when should you use MongoDB?

MongoDB and NodeJS are two different technologies. MonogDB is a database system which gives you a chance to efficiently store documents in a database and to perform operations like data updates, or to search documents by some criterias. NodeJS's responsibilty is especially to execute your application. Save this answer.


2 Answers

Besides WebSockets you can use long polling technique to create persistent connection between server and client.

Long polling is a variation of the traditional polling technique and allows emulation of an information push from a server to a client. With long polling, the client requests information from the server in a similar way to a normal poll. However, if the server does not have any information available for the client, instead of sending an empty response, the server holds the request and waits for some information to be available. Once the information becomes available (or after a suitable timeout), a complete response is sent to the client.

However it's not as efficient as WebSockets, but it's used because it works in every browser unlike WebSockets. Try to look at this article:

Reducing kilobytes of data to 2 bytes…and reducing latency from 150ms to 50ms is far more than marginal. In fact, these two factors alone are enough to make WebSocket seriously interesting to Google.

like image 188
yojimbo87 Avatar answered Sep 23 '22 08:09

yojimbo87


I am not familiar with the chat application. And I don't see a link for a hosted demo.

NodeJS is a server-side technology. It has no trouble with native sockets. Socket.IO is a combination client-side and server-side technology. It just happens to use NodeJS for the server side. The beauty of it is that it presents a common communications API on the client side, regardless of what features the browser actually has.

like image 42
Fantius Avatar answered Sep 21 '22 08:09

Fantius