How to interact with nginx-push-stream-module and php?

Try to deal with the long polling technology article https://habr.com/ru/post/252349/.
Recompiled my Nginx with the module, here
listing nginx.conf
user www-data;
worker_processes 1;
events {
 worker_connections 1024;
}


http {
 include mime.types;
 default_type application/octet-stream;
 sendfile on;
 keepalive_timeout 65;

 server {
 listen 80;
 charset utf-8;
 root /var/www;
 index index.php;


 location ~ \.php$ { 
 include fastcgi_params; 
 location unix:/var/run/php5-fpm.sock; 
 fastcgi_index index.php; 
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
 } 

 location /pub {
 push_stream_publisher admin;
 set $push_stream_channel_id $arg_id;
 push_stream_store_messages on;
 allow 127.0.0.1;
}

 location ~ /sub/(.*) {
 push_stream_subscriber long polling;
 push_stream_last_received_message_tag $arg_tag;
 push_stream_last_received_message_time $arg_time;
 push_stream_longpolling_connection_ttl 25s;
 push_stream_channels_path $1;
}
}
 push_stream_shared_memory_size 32M;
 push_stream_channels_path $1;
}


Next, curl om messages are sent:
listing
<?php
 //$channel_id = 1;
 $message = 'Hello!';
 //sending a message
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, 'localhost/pub/1');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($message));
curl_exec($ch);
print_r(curl_getinfo($ch));
 curl_close($ch);

The recipient:
listing
//let ChannelID data = 1; //id of the subscriber
 let last_etag = 0; //variable for the 'Etag'header
 let last_time = null; //variable for the header 'Last-Modified'

 function new_message() {
$.ajax({
 url: '/sub/1',
 type: "GET",
 dataType: 'json',
 beforeSend: function (xhr) {
 xhr.setRequestHeader('Etag', last_etag);
 xhr.setRequestHeader('Last-Modified', last_time);
},
 success: function (data, status, xhr) {
 last_etag = xhr.getResponseHeader('Etag'); //assign a new value of the variable last_etag
 // from the 'Etag' header of the response
 last_time = xhr.getResponseHeader('Last-Modified'); // assign new value of the variable last_time
 // header 'Last-Modified' response

 //do something with the received message
console.log(data)
console.log(status)
//console.log(xhr)

 setTimeout(new_message, 500); // reconnecting immediately after receipt of the response
}
})
}

 new_message();


Long polling apparently works because the output data and status to the console occurs every 25 seconds (in accordance with Directive push_stream_longpolling_connection_ttl)
But it is not clear how it should work. A message left post to the server, but where? it's necessary to keep. The subscriber checks for messages, but to inform it and who will generate json on the server with a message and status?? Can you help?
April 4th 20 at 13:02
1 answer
April 4th 20 at 13:04
Solution
I recommend to use JS on the frontend libo PushStream https://github.com/wandenberg/nginx-push-stream-mo...

Send a message to the frontend via nginx-push-stream-module server
$message = array('message'=>'Hello!');
 $idChannel = 1; //ID of the channel
 $nginxPushStreamModuleHost = 'http://localhost/' //YOUR SERVER WITH nginx-push-stream-module
 //sending a message
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $nginxPushStreamModuleHost.'pub/'.$idChannel);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($message));
curl_exec($ch);
print_r(curl_getinfo($ch));
 curl_close($ch);


Listened nginx-push-stream-module server for the frontend
idChannel = 1; //ID of the channel
nginxPushStreamModuleHost = 'http://localhost/'; //YOUR SERVER WITH nginx-push-stream-module

pushstream = new PushStream({
 timeout: 20000,
 modes: 'eventsource|stream',
host:nginxPushStreamModuleHost,

 useSSL:false 

});

pushstream.onmessage = function(json) {
//Works if server something it's the user
alert('About! something came! Message: '+json.message)
}

pushstream.addChannel(idChannel);
pushstream.connect();
The principle is simple:
There is a backend, kotoryy sends messages to nginx-push-stream-module
you have a server with nginx-push-stream-module
there is a frontend listener server nginx-push-stream-module

From the frontend to nginx-push-stream-module sends a message to a particular channel (e.g. user ID)
If there is a client subscribed to this channel (or signed on his ID) and he now sits on the front, he will get the message. - Haylie.McDermott commented on April 4th 20 at 13:07

Find more questions by tags AJAXPHPNginxLong pollingPush-technology