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 8507319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:50:15+00:00 2026-06-11T02:50:15+00:00

I implement long polling on my web site using php. All work fine, but

  • 0

I implement long polling on my web site using php. All work fine, but have one problem. When i open several pages by one user, i have problem with recognize, what event i should to run on that page. If i block event after it work on one page, that event don’t work on another. If i don’t block, they continue working infinite on every page. If i check event on page that is worked, they still work again after refresh page. I missed right solution, can you help me?

code not very good but working

i just need an idea how to do this

function loadEventAll($uid=0){
    if (isset($_POST['event_log'])){
        $event_log = json_decode($_POST['event_log']); //here event already worked on page
    }
    else $event_log = array();
    for($i=0;$i<25;$i++){            
        $now = time();
        $time = $now;//-$this->triggered_timeout;   

        //sql to cut already worked event
        $noid = array();
        foreach($event_log as $eve){
            if(isset($eve->id)) $noid[] = 'id<>'.$eve->id;
        }
        if (count($noid)) $noid = ' AND ('.implode(' AND ', $noid).') ';
        else $noid = '';   

        //don't try to understand this :)
        $q="SELECT * FROM ig_event WHERE (user_id='$uid' OR user_id=0) AND (((triggered+2>=$time OR triggered=0) AND infinite=0) OR (infinite=1 AND triggered+10<=$time)) AND ($now<time_end or infinite=1) AND time_start<=$time $noid";            
        $link = mysql_query($q); 
        $arr = array();
        if(!$link){ 
            sleep(1);
            continue; // if no event occurring start next iteration
        }
        if (mysql_num_rows($link)){   
            while ($ddd = mysql_fetch_assoc($link)){
                $id = $ddd['id'];
                if ($ddd['direct_call']!=""){ //call js function on page
                    $arr[] = $ddd['direct_call'].':'.$id.':'.$ddd['param'];

                    if ($ddd['success']!=1){
                        if (!$ddd['infinite']) $succ = ' success=1';else $succ='';
                        $ctime = time();
                        $q = "UPDATE ig_event SET $succ,triggered='$ctime' WHERE id='$id'";
                        mysql_query($q);
                    }
                }
                else{ // usually not used
                    if ($this->load->module($ddd['module'])){
                        if (method_exists($this->$ddd['module'], $ddd['action'])){

                            $time = time();                                
                            if (($tarr = $this->$ddd['module']->$ddd['action']($ddd['param'])) and !$ddd['infinite']){                                    
                                $q = "UPDATE ig_event SET success=1 WHERE id='$id'";
                                mysql_query($q);
                            }
                        }
                        else{
                            $q = "UPDATE ig_event SET success=1 WHERE id='$id'";
                            mysql_query($q);
                        }
                        $q = "UPDATE ig_event SET triggered='$time' WHERE id='$id'";
                        mysql_query($q);
                        if (!$ddd['infinite']) $arr[] = 'blank'.':'.$id.':';
                    }    
                }
            }    
            return $arr;
        }  
        sleep(1);
    }  
    return $arr;
}
  • 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-06-11T02:50:17+00:00Added an answer on June 11, 2026 at 2:50 am

    Here’s an idea that we use in a live sysyem. When an event happens, add a timestamp to it. E.g.

    id | col1 | col2 | timestamp
    ------------------------------
     1 | herp | derp | 1347373151
     2 | herp | derp | 1347373152
     3 | herp | derp | 1347373153
    

    Now, when we start long polling we send out the time of the last event we caught or if it’s the first request – current time. Then we build the query:

    $q = "SELECT * FROM `table` WHERE `timestamp` < ".$lastCaughtEventTime;
    

    If you get any results – return them and exit the php script. The js side should repeat the request as soon as it gets an answer with the highest timestamp from the result set. This way your queries will be minimal and fast. An example code:

    function poll() {
        $results = [];
        $start = microtime(true);
        $q = "SELECT * FROM `table` WHERE `timestamp` < " . (int) $_POST['timestamp'];
        while (true) {
            $result = mysql_query($q);
            if (mysql_num_rows($result) > 0) {
                $result = returnResults($result);
                break;
            }
            else {
                if (time() - $start > POLLING_TIMEOUT) {
                    die('timeout');
                }
                sleep(2);
            }
        }
        echo json_encode($result);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement long polling using Netty and jQuery. I have it working
I'm trying to implement long polling technique using NodeJS. I have this basic code
I'm trying to implement an http long polling server in Node.js, and have no
I'm trying to implement long polling in a php chat script, however the long
I'm trying to implement a comet style, long polling connection using an XMLHttpResponse object.
I am using Spring 3.2 Milestone 1 to implement a service with long polling
For days I have been experimenting with long polling/comet implementation for my site. I
I just learned about comet pattern(Long polling) and very curious to implement a simple
I have implement the exact same concept for my project- http://blog.perplexedlabs.com/2009/05/04/php-jquery-ajax-javascript-long-polling/ My question is,
How to implement a gmail-like this is taking too long warning message using jQuery

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.