Answer the question
In order to leave comments, you need to log in
How do nginx-push-stream-module and php interact?
I'm trying to figure out the technology of long polling according to the article https://habr.com/ru/post/252349/ .
Nginx rebuilt with the module, here
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;
fastcgi_pass 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;
}
<?php
//$channel_id = 1;
$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);
//let channelId = 1; //id подписчика
let last_etag = 0; //переменная для заголовка 'Etag'
let last_time = null; //переменная для заголовка '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'); //присваиваем новое значение переменной last_etag
// из заголовка 'Etag' ответа
last_time = xhr.getResponseHeader('Last-Modified'); // присваиваем новое значение переменной last_time
// из заголовка 'Last-Modified' ответа
//здесь что-то делаем с полученным сообщением
console.log(data)
console.log(status)
//console.log(xhr)
setTimeout(new_message, 500); // переподключаемся сразу после получения ответа
}
})
}
new_message();
Answer the question
In order to leave comments, you need to log in
I recommend using the PushStream lib on the frontend JS https://github.com/wandenberg/nginx-push-stream-mo...
Sending a message to the frontend via the nginx-push-stream-module server
$message = array('message'=>'Привет!');
$idChannel = 1; //ID канала
$nginxPushStreamModuleHost = 'http://localhost/' //ВАШ СЕРВЕР С nginx-push-stream-module
//отправка сообщения
$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);
idChannel = 1; //ID канала
nginxPushStreamModuleHost = 'http://localhost/'; //ВАШ СЕРВЕР С nginx-push-stream-module
pushstream = new PushStream({
timeout: 20000,
modes: 'eventsource|stream',
host:nginxPushStreamModuleHost,
useSSL:false
});
pushstream.onmessage = function(json) {
//Сработает если с сервера что-то пришло пользователю
alert('О! что-то пришол! Сообщение: '+json.message)
}
pushstream.addChannel(idChannel);
pushstream.connect();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question