A
A
Aleksey Filippov2014-01-31 11:38:30
PHP
Aleksey Filippov, 2014-01-31 11:38:30

How to send push notification (Nginx, PHP)?

The user sends 20 images, each of them needs to be processed. In principle, synchronous execution for this task is also acceptable. But it's just very interesting how you can still implement the mechanism asynchronously with sending push notifications to the user .
If I understand correctly, then the next task is to create a sub-process and open a channel ( nginx push module ) so that the browser can listen for messages on this channel. upon completion of all operations in the sub-process, a message about the result must be sent to the channel.
how to create/open a sub-process in PHP/Windows and close it, dispose? How to properly set up an nginx channel?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vit, 2014-01-31
@fornit1917

Google for Comet, Long Polling.
Although in your case, even Short Polling will probably work, without any Comet there. That is, you upload pictures and that's it. On the server, they are processed separately in the background (you can use the queue manager for this, or you simply add tasks to the database and clean them up with a scheduled script). When the photo processing is over, report it to the database.
At this time, the browser will periodically ask the server with Ajax whether the processing has ended

A
Aleksey Filippov, 2014-01-31
@Huzzah

@fornit1917
- I need to redirect the user to the edit page after uploading and processing images. That is, I need to know that all the pictures from the upload panel were either processed successfully or processed unsuccessfully (the point is not to catch exceptions).
- it is clear about a DB.
- not clear about the background mode. I understand what a queue handler is, but I have not tried to use it. read about redis , activemq , zeromq , rabbitmq . I need a queue handler that can be used under Windows (Server 2008) + a simple example of its use.
- at the moment the idea is this:

$app->post('/photos-upload', function() use ($app, $dbConn) { 
    // проверили все что нам нужно
    $app->performAsyncPost('http://localhost:8090/mx/api/photos-process', array(
        // отправляем всякие данные
        'args' => 'data'
    ));
}
$app->post('/photos-process', function() use ($app, $dbConn) { 
    // отправляем что-то вроде curl http://localhost/pub?id=ch1 -d "Pending"
    // делаем нужные нам операции
    // отправляем что-то вроде curl http://localhost/pub?id=ch1 -d "Done"
}

public function performAsyncPost($url, array $params) {
    foreach($params as $key => &$val) {
        if(is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);  
    }
            
    $post_string = implode('&', $post_params);
    $parts = parse_url($url);

    $fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 30);

    $out = "POST ".$parts['path']." HTTP/1.1\r\n";
    $out.= "Host: ".$parts['host']."\r\n";
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out.= "Content-Length: ".strlen($post_string)."\r\n";
    $out.= "Connection: Close\r\n\r\n";
    if(isset($post_string)) $out.= $post_string;
    fwrite($fp, $out);
    fclose($fp);
}

poll the channel-file on the client side

T
TomaZ Vazovsky, 2014-01-31
@Keksinautin

If I understand correctly, the task is to asynchronously upload 20 pictures to the server (which should be processed there after uploading) and, of course, find out how it all went.
Firstly, I have big doubts about 20 simultaneous connections to the server. Browsers limit the number of simultaneous connections to a single server. Maybe 20 at the same time will not work, you need to google these values ​​​​for each browser.
Secondly, I would use the following scheme for this:
1. Nginx HttpUploadModule uploads the file and only after that it proxies the request further to the server that will process the file. In our case PHP.
2. And the HttpUploadProgressModule will help us track the file upload process.
3. Accordingly, when we have collected answers about all 20 successful downloads on the front-end, we can already do what we want.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question