Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 277897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T01:09:08+00:00 2026-05-12T01:09:08+00:00

I have read some posts about this topic and the answers are comet, reverse

  • 0

I have read some posts about this topic and the answers are comet, reverse ajax, http streaming, server push, etc.

How does incoming mail notification on Gmail works?

How is GMail Chat able to make AJAX requests without client interaction?

I would like to know if there are any code references that I can follow to write a very simple example. Many posts or websites just talk about the technology. It is hard to find a complete sample code. Also, it seems many methods can be used to implement the comet, e.g. Hidden IFrame, XMLHttpRequest. In my opinion, using XMLHttpRequest is a better choice. What do you think of the pros and cons of different methods? Which one does Gmail use?

I know it needs to do it both in server side and client side.
Is there any PHP and Javascript sample code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T01:09:08+00:00Added an answer on May 12, 2026 at 1:09 am

    The way Facebook does this is pretty interesting.

    A common method of doing such notifications is to poll a script on the server (using AJAX) on a given interval (perhaps every few seconds), to check if something has happened. However, this can be pretty network intensive, and you often make pointless requests, because nothing has happened.

    The way Facebook does it is using the comet approach, rather than polling on an interval, as soon as one poll completes, it issues another one. However, each request to the script on the server has an extremely long timeout, and the server only responds to the request once something has happened. You can see this happening if you bring up Firebug’s Console tab while on Facebook, with requests to a script possibly taking minutes. It is quite ingenious really, since this method cuts down immediately on both the number of requests, and how often you have to send them. You effectively now have an event framework that allows the server to ‘fire’ events.

    Behind this, in terms of the actual content returned from those polls, it’s a JSON response, with what appears to be a list of events, and info about them. It’s minified though, so is a bit hard to read.

    In terms of the actual technology, AJAX is the way to go here, because you can control request timeouts, and many other things. I’d recommend (Stack overflow cliche here) using jQuery to do the AJAX, it’ll take a lot of the cross-compability problems away. In terms of PHP, you could simply poll an event log database table in your PHP script, and only return to the client when something happens? There are, I expect, many ways of implementing this.

    Implementing:

    Server Side:

    There appear to be a few implementations of comet libraries in PHP, but to be honest, it really is very simple, something perhaps like the following pseudocode:

    while(!has_event_happened()) {
       sleep(5);
    }
    
    echo json_encode(get_events());
    
    • The has_event_happened function would just check if anything had happened in an events table or something, and then the get_events function would return a list of the new rows in the table? Depends on the context of the problem really.

    • Don’t forget to change your PHP max execution time, otherwise it will timeout early!

    Client Side:

    Take a look at the jQuery plugin for doing Comet interaction:

    • Project homepage: http://plugins.jquery.com/project/Comet
    • Google Code: https://code.google.com/archive/p/jquerycomet/ – Appears to have some sort of example usage in the subversion repository.

    That said, the plugin seems to add a fair bit of complexity, it really is very simple on the client, perhaps (with jQuery) something like:

    function doPoll() {
       $.get("events.php", {}, function(result) {
          $.each(result.events, function(event) { //iterate over the events
              //do something with your event
          });
          doPoll(); 
          //this effectively causes the poll to run again as
          //soon as the response comes back
       }, 'json'); 
    }
    
    $(document).ready(function() {
        $.ajaxSetup({
           timeout: 1000*60//set a global AJAX timeout of a minute
        });
        doPoll(); // do the first poll
    });
    

    The whole thing depends a lot on how your existing architecture is put together.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 212k
  • Answers 212k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer do you really need an array bash $ ipAddress="10.78.90.137;10.78.90.149" $… May 12, 2026 at 10:20 pm
  • Editorial Team
    Editorial Team added an answer Not sure if you have come across an answer for… May 12, 2026 at 10:20 pm
  • Editorial Team
    Editorial Team added an answer SAP is a company and offers a full Enterprise Resource… May 12, 2026 at 10:20 pm

Related Questions

Alright...I've given the site a fair search and have read over many posts about
I've read several stackoverflow posts about this topic, particularly this one: Secure hash and
I'm working on a CMS as my hobby project. Because I'm not paid for
I'm looking into setting up a CI environment for our flex projects. I have

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.