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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:26:01+00:00 2026-05-27T21:26:01+00:00

here is my problem: I have a script (let’s call it comet.php) whic is

  • 0

here is my problem: I have a script (let’s call it comet.php) whic is requsted by an AJAX client script and wait for a change to happen like this:

while(no_changes){
    usleep(100000);
    //check for changes
}

I don’t like this too much, it’s not very scalable and it’s (imho) “bad practice”
I would like to improve this behaviour with a semaphore(?) or anyway concurrent programming
technique. Can you please give me some tips on how to handle this? (I know, it’s not a short answer, but a starting point would be enough.)

Edit: what about LibEvent?

  • 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-27T21:26:01+00:00Added an answer on May 27, 2026 at 9:26 pm

    You can solve this problem using ZeroMQ.

    ZeroMQ is a library that provides supercharged sockets for plugging things (threads, processes and even separate machines) together.

    I assume you’re trying to push data from the server to the client. Well, a good way to do that is using the EventSource API (polyfills available).

    client.js

    Connects to stream.php through EventSource.

    var stream = new EventSource('stream.php');
    
    stream.addEventListener('debug', function (event) {
        var data = JSON.parse(event.data);
        console.log([event.type, data]);
    });
    
    stream.addEventListener('message', function (event) {
        var data = JSON.parse(event.data);
        console.log([event.type, data]);
    });
    

    router.php

    This is a long-running process that listens for incoming messages and sends them out to anyone listening.

    <?php
    
    $context = new ZMQContext();
    
    $pull = $context->getSocket(ZMQ::SOCKET_PULL);
    $pull->bind("tcp://*:5555");
    
    $pub = $context->getSocket(ZMQ::SOCKET_PUB);
    $pub->bind("tcp://*:5556");
    
    while (true) {
        $msg = $pull->recv();
        echo "publishing received message $msg\n";
        $pub->send($msg);
    }
    

    stream.php

    Every user connecting to the site gets his own stream.php. This script is long-running and waits for any messages from the router. Once it gets a new message, it will output this message in EventSource format.

    <?php
    
    $context = new ZMQContext();
    
    $sock = $context->getSocket(ZMQ::SOCKET_SUB);
    $sock->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, "");
    $sock->connect("tcp://127.0.0.1:5556");
    
    set_time_limit(0);
    ini_set('memory_limit', '512M');
    
    header("Content-Type: text/event-stream");
    header("Cache-Control: no-cache");
    
    while (true) {
        $msg = $sock->recv();
        $event = json_decode($msg, true);
        if (isset($event['type'])) {
            echo "event: {$event['type']}\n";
        }
        $data = json_encode($event['data']);
        echo "data: $data\n\n";
        ob_flush();
        flush();
    }
    

    To send messages to all users, just send them to the router. The router will then distribute that message to all listening streams. Here’s an example:

    <?php
    
    $context = new ZMQContext();
    
    $sock = $context->getSocket(ZMQ::SOCKET_PUSH);
    $sock->connect("tcp://127.0.0.1:5555");
    
    $msg = json_encode(array('type' => 'debug', 'data' => array('foo', 'bar', 'baz')));
    $sock->send($msg);
    
    $msg = json_encode(array('data' => array('foo', 'bar', 'baz')));
    $sock->send($msg);
    

    This should prove that you do not need node.js to do realtime programming. PHP can handle it just fine.

    Apart from that, socket.io is a really nice way of doing this. And you could connect to socket.io to your PHP code via ZeroMQ easily.

    See also

    • ZeroMQ
    • ZeroMQ PHP Bindings
    • ZeroMQ is the Answer – Ian Barber (Video)
    • socket.io
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem here. My Zend_Forms do not render in view script. Via
Here's my problem: I have to call a web service with a secure header
Here is the problem I have: I have a lot (tens of thousands) of
Here's my problem - I have some code like this: <mx:Canvas width=300 height=300> <mx:Button
Here's my problem: I have a virtual method defined in a .h file that
Here is the problem: we have lots of Javascripts and lots of CSS files,
Here's my problem.I have 2 xmlfiles with identical structure, with the second xml containing
Here is the problem: We have all of our development under subversion, but our
Here's my problem: I have do create a menu/list of actions (which would be
Here's the problem: I have couple of pages which gets its content from a

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.