Jump to content

Recommended Posts

Hi, surely all of us have seen the auto-instant notifications in Facebook, or that comments appear automatically in a page while we are still on that page without page refresh. Can anybody give me an outline of how is it accomplished? If it were the other way around, like the user inputs something in the page, that data could be sent to the server through Ajax and a server side PHP script could update the database accordingly. But how can it be accomplished in the reverse manner also, that when some user does something in one page of a website, its notification is propagated to another page of the site open on some other computer instantly?

It's still ajax. The page user 1 is on is checking to see if there are any updates. It doesn't happen only when user 2 posts something, it happens every X interval, and checks for anything new that user 1 can see.

It's AJAX just the same. A function is repeatedly called in the background that utilizes AJAX to check for updates (stuff that wasn't there the last time the function was called). It's probably something like every 2 or 3 seconds it's called (or maybe every second - I don't know just how much Facebook servers can handle but it's probably a lot more than I imagine).

It's AJAX polling which is pull technology, basically the client sends requests to the server every x seconds, if the server responds with data, the data is processed and the DOM is manipulated accordingly.

 

 

There are two types of polling, short polling and long polling.

 

 

In short polling, the client sends a request, the server processes the request, outputs a response and the client handles the response accordingly, then after x seconds another request is sent.

 

 

In long polling, the client sends a request, the server queries the database, if a result is found, the response is sent, otherwise the server continues querying until data is found and a request is sent, or a certain time period has expired, the response is sent to the client, and the client (usually immediately repeats the process).

 

 

http://boardreader.com/thread/Short_polling_vs_Long_polling_for_real_t_5ft4kX2ri92.html

 

 

There's a relatively new implementation of server-side javascript called node.js, which has a web-socket library called socketIO which can be used to push data to the client.

Pushing means that suppose there are two pages, suppose www://website/blah/a and www://website/blah/b, and client has kept page 1 open in browser, whereas another client does something in page 2, so the first client can see that response in page 1 without page refresh, right? Plus does it involve sending an Ajax request every, say, 1 second, like others said here? That looks like a overkill, plus wastage of bandwidth on the user's part, for suppose that the page is kept open for 2 hours, and there is no update, but it will keep sending requests to ensure that.

No, it doesn't involve polling, lets say 3 clients are in a chat room using push technology, the server keeps an open socket to each client, client 1 sends a chat message to the server, the server processes the data and streams output to the sockets of each client, and the client is updated in real-time.

 

 

Take a look at

video.

Facebook's chat is coded in erlang which allows great use of WebSockets and Comet. That is what pushes the the notifications of chat messages to each tab in your browser. I would assume they use a similar method for notifications.  Erlang is effective with distributed databases so I have no doubt their notifications are done the same way.

No, it doesn't involve polling, lets say 3 clients are in a chat room using push technology, the server keeps an open socket to each client, client 1 sends a chat message to the server, the server processes the data and streams output to the sockets of each client, and the client is updated in real-time.

 

But the other people here said that this involves polling? And regarding the process you mentioned, the server may stream output to the socket, what code will listen at that socket to manipulate the DOM accordingly? PHP or Javascript or Node? Sorry, I hardly know anything about sockets and stuff, so the question might seem naive.

No, it doesn't involve polling, lets say 3 clients are in a chat room using push technology, the server keeps an open socket to each client, client 1 sends a chat message to the server, the server processes the data and streams output to the sockets of each client, and the client is updated in real-time.

 

But the other people here said that this involves polling? And regarding the process you mentioned, the server may stream output to the socket, what code will listen at that socket to manipulate the DOM accordingly? PHP or Javascript or Node? Sorry, I hardly know anything about sockets and stuff, so the question might seem naive.

 

 

Javascript listens on the client-side.

 

 

As well as the nodejs server side socketIO library, there is also a client side socketIO script that listens for the servers pushed data. Take a look at the socket.io website.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.