Cupidvogel Posted April 12, 2012 Share Posted April 12, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/260829-how-to-enable-auto-updation-through-php-like-in-facebook/ Share on other sites More sharing options...
Jessica Posted April 12, 2012 Share Posted April 12, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260829-how-to-enable-auto-updation-through-php-like-in-facebook/#findComment-1336847 Share on other sites More sharing options...
Masna Posted April 12, 2012 Share Posted April 12, 2012 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). Quote Link to comment https://forums.phpfreaks.com/topic/260829-how-to-enable-auto-updation-through-php-like-in-facebook/#findComment-1336848 Share on other sites More sharing options...
Andy-H Posted April 13, 2012 Share Posted April 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260829-how-to-enable-auto-updation-through-php-like-in-facebook/#findComment-1336877 Share on other sites More sharing options...
Cupidvogel Posted April 13, 2012 Author Share Posted April 13, 2012 Can Node.js exactly accomplish what Facebook does? Quote Link to comment https://forums.phpfreaks.com/topic/260829-how-to-enable-auto-updation-through-php-like-in-facebook/#findComment-1337035 Share on other sites More sharing options...
Andy-H Posted April 13, 2012 Share Posted April 13, 2012 Yes, you can do both polling or push with node, although if you're going to be using polling, may-as-well use PHP. Quote Link to comment https://forums.phpfreaks.com/topic/260829-how-to-enable-auto-updation-through-php-like-in-facebook/#findComment-1337071 Share on other sites More sharing options...
Cupidvogel Posted April 13, 2012 Author Share Posted April 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260829-how-to-enable-auto-updation-through-php-like-in-facebook/#findComment-1337109 Share on other sites More sharing options...
Andy-H Posted April 13, 2012 Share Posted April 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260829-how-to-enable-auto-updation-through-php-like-in-facebook/#findComment-1337197 Share on other sites More sharing options...
creata.physics Posted April 14, 2012 Share Posted April 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260829-how-to-enable-auto-updation-through-php-like-in-facebook/#findComment-1337249 Share on other sites More sharing options...
Cupidvogel Posted April 14, 2012 Author Share Posted April 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260829-how-to-enable-auto-updation-through-php-like-in-facebook/#findComment-1337253 Share on other sites More sharing options...
Andy-H Posted April 14, 2012 Share Posted April 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260829-how-to-enable-auto-updation-through-php-like-in-facebook/#findComment-1337403 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.