Yousef Essam Yousef Essam - 3 years ago 140
Node.js Question

AJAX in chat applications

When I read about creating chat applications in Node.js, I found that we must use WebSocket protocol and use socket.io library to do such purpose.

The problem that we can't use HTTP protocol to push data from the server without client requests.

Instead I read that we can regularly send requests using AJAX and wait for response from the server.

So my question is : Can AJAX serve the same purpose of WebSockets and use AJAX in chat applications instead of using WebSockets?

Answer Source

You don't have to use websockets for chat applications, there is actually a wide range of technologies you can use.

AJAX: AJAX, or long polling can be used for web chats, but is considered a primitive and inefficient way to get updated chat information. The client listens for a change on the server-side, then when it get's a response from the server, it then makes another request to listen for requests. The reason this is looked down upon is because they client could be listening for a long time, without a response from the server.

WebSockets: Websockets is a protocol that runs over HTTP that facilitates bi-directional data. Similar to the TCP websocket protocol, there is a 3-way handshake involved in order to make a connection. Socket.io aids in the use of websockets by abstracting a lot of the raw websocket functions. The truth is that Socket.io actually provides backwards compatibility with browsers that only support long-polling or Flash for chat communication. Unlike WebRTC, there is a man-in-the-middle (server) to facilitate who is chatting with who.

WebRTC: WebRTC is a free, open project that provides browsers and mobile applications with Real-Time Communications (RTC) capabilities via simple APIs. These protocols allow peer-to-peer communications (chat included) with little use of a middle-man, or server. To address your question, it's great for "private-chats".

Flash: It is possible to use Flash for chat communications over the web. This is severely outdated, as Flash is slowly dying from the web.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download