How does Facebook chat avoid continuous polling of the server? [closed] - ajax

Want to improve this post? Add citations from reputable sources by editing the post. Posts with unsourced content may be edited or deleted.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am trying to understand how Facebook's chat feature receives messages without continuously poling the server.
Firebug shows me a single GET XmlHttpRequest continuously sitting there, waiting for a response from the server. After 5 minutes, this never timed out.
How are they preventing timeout?
An AJAX request can just sit there like that indefinitely, waiting for a response?
Can I do this with JSONRequest? I see this at json.org:
JSONRequest is designed to support
duplex connections. This permits
applications in which the server can
asynchronously initiate transmissions.
This is done by using two simultaneous
requests: one to send and the other to
receive. By using the timeout
parameter, a POST request can be left
pending until the server determines
that it has timely data to send.
Or is there another way to let an AJAX call just sit there, waiting, besides using JSONRequest?

Facebook uses a technique which is now called Comet to push messages from the server to the client instead of having the client poll the server.
There are many ways that this can be implemented, with XMLHttpRequest long polling being just one option. The principle behind this method is that the client sends an ordinary XMLHttpRequest but the server doesn't respond until some event happens (such as another user sending a message), so the client is forced to wait. When the client receives a response (or if the request times out) the client simply creates a new request so that it always has one open request to the server.

Related

a way to keep a webapp listening to server [duplicate]

This question already has an answer here:
Notify only specific user(s) through WebSockets, when something is modified in the database
(1 answer)
Closed 1 year ago.
so I am looking for making a web app that gets data from the server periodically.
my initial thought was issuing an ajax call inside a loop that asks the server if there is news every 2sec so when there are new things to show the server will send data otherwise will just reply with an empty payload
I am wondering if there is a better approach so the server won't waste sources replying to all those unnecessary ajax calls but instead it will only send data to the targeted client
you can use
<p:poll>
tag if you are working with Primefaces or use WebSockets

ajax based notification/messaging system

I have a question.
Facebook probably uses ajax to notify user about a new message, is this correct?
If yes, would not this tax db to incredible levels?
I mean millions of users every second of being online requesting message status.
Or am I thinking about this in a wrong way?
You are asking about a technique called polling. And you are correct that it has scalability issues. In general not a good idea.
[rant]I have no idea what facebook does. I hate facebook. It's like a drunk whore who won't stop texting/emailing you and needs to be used.[/rant].
There are better alternatives to polling. One technique is called long polling, and then there is server side push.
See
How do I implement basic "Long Polling"?
and
https://stackoverflow.com/questions/6883540/http-server-to-client-push-technologies-standards-libraries.
In long polling, the client sends a request but does not expect a response immediately; the response could come immediately, in a second, or in an hour. The challenge is for the server to manage the outstanding requests in a non-resource intensive way.
With server side push, the server maintains a connection with clients and can broadcast messages to its connections when events occur.
Which alternative to use depends a bit on your technology stack. For example, node.js has something called socket.io (which I think is server side push using html5 websockets) which I hear good things about.

JMS messages and load balancing of JMS messages [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Have this design problem and trying to find a best way to implement it using JMS.
Web App has single Listener and multiple Producer and multiple WorkerBee. Listener pushes messages from its queue to WorkerBee. All of the messages produced are queued in a Listener. I want to implement a process than can send messages from Listener queue to each WorkerBee using some kind of load balancing process. Only single instance of the message occurs in the Listener queue or in the WorkerBee queue.
Whats the best way to do it? Does JMS sounds like a good choice here? How would I push the message from Listener queue to WorkerBee queue?
Open for suggestion :) And appreciate your response.
Messaging is good choice for such task. I think you should use Apache Camel framework as routing platform for described purposes. It allows to divide business logic from integration level in quite graceful way.
Camel supports plenty of components "out-of-box" including JMS endpoints.
You can delegate to one of your WorkerBee's using Load Balancer pattern.
JMS sounds a good option, since you have multiple instances of WorkerBee, make all WorkerBee listen to a single queue, e.g. to InWorkBeeQueue.
Now you can publish messages from Web App listener to InWorkBeeQueue. Write a simple java JMS producer code to publish messages to this queue. Depending on whichever instance of WorkBee is free, it will read message from InWorkBeeQueue and process it.
If you want to avoid writing new JMS producer code, you can directly map messages from Web app queue to InWorkBeeQueue using Apache Camel routes.

What do you get with HTML5 web sockets that you can't have with AJAX?

Ian Hickson says:
I expect the iframe sandboxing feature
will be a big boon to developers if it
takes off. My own personal favorite
feature is probably the Web Sockets
API, which allows two-way
communication with a server so that
you can implement games, chatting,
remote controls, and so forth.
What can you get with web sockets that you can't get with AJAX? Is it just convenience, or is it somehow more efficient? Is it that the server can send data to the client, without having to wait for a message so it can respond?
Yes, it's all about the server being able to push data to the client. Currently, simulating bi-directional communication without Flash/Silverlight/Java/ActiveX takes the form of one of two workarounds:
Traditional polling: Clients make small requests to the server frequently, checking for updates. Even if no update has occurred, the client doesn't know that and must continuously poll for updates. Though each request may be lightweight, constant polling by many clients can add up quickly.
Long polling: Clients make periodic requests for updates, like regular polling, but if there are no updates yet available then the server does not respond immediately and holds the connection open. When an update is finally available, the server pushes that down to the client, which acts on it and then repeats that process. Long polling offers push-like update resolution, but is basically a self-inflicted DDoS attack and can be very resource intensive for many types of web servers.
With WebSockets, you get all of the responsiveness advantages of long polling, with dramatically less server-side overhead.
WebSockets are more efficient (and "more real-time") than AJAX calls because you keep connection open and don't send extra protocol headers and other stuff after each request and response. Look at this article:
During making connection with
WebSocket, client and server exchange
data per frame which is 2 bytes each,
compared to 8 kilo bytes of http
header when you do continuous polling.

How can you imitate pushing data to a web application?

Obviously, you can't push data to a web application, as HTTP works in a request-response cycle.
But what hacks/methods do you know of that can imitate pushing data to a client?
You could use what is known as Comet:
http://en.wikipedia.org/wiki/Comet_(programming), https://stackoverflow.com/search?q=comet
Basically, javascript in the browser makes a request to the server right away (using XmlHttpRequest). The server does not respond until it has some data to serve.
From the article:
The browser makes an asynchronous request of the server, which may wait for data to be available before responding. The response can contain encoded data (typically XML or JSON) or javascript to be executed by the client. At the end of the processing of the response, the browser creates and sends another XHR, to await the next event. Thus the browser always keeps a request outstanding with the server, to be answered as each event occurs.
In some instances polling i.e. sending a request at a regular (short) interval might suffice. But the answer given above - Comets - is the closest thing to the real deal as far as sending data without a client request is concerned.
You're limited to polling in HTTP. One of the early Netscape browsers did implement HTTP push I seem to recall, back at the start of the century but it didn't get anywhere.
Good ol' Wikipedia page on HTTP Push (with lots of [clarification needed])
You can't use raw sockets with Flash, Javascript/Xml, Silverlight. With Java, and Active-X you can, but you'll need a certificate. The Quake Live shows this off, all the networking is obviously still UDP based but inside a browser plugin for IE or Firefox.
So polling, polling, polling.

Resources