F
F
fLipE2015-05-19 18:47:43
JavaScript
fLipE, 2015-05-19 18:47:43

How to read only the last message from a long-polling channel?

When implementing the simplest messaging through long-polling using nginx, a problem arose: all messages that were sent to this channel, and not just one, come to XMLHttpRequest.responseText . I don't think that's how it should be.
Receiving messages (js):

var xhr = new XMLHttpRequest();
xhr.open('GET', '/sub/' + subID, true);
xhr.send();
xhr.onreadystatechange = xhr.onload = function () {
if (xhr.status == 200) {
       console.log(xhr.responseText);
}
}

Hence the question: is the problem in the configuration of the nginx module, or in something else?
Sending a message:
$channel_id = '1';
$msg = array('text' => 'a');

$ch = curl_init('http://127.0.0.1/pub?id='.$channel_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($msg));
if (curl_exec($ch)) 
  echo 'sended '.json_encode($msg);
else 
  echo 'fail';
curl_close($ch);

nginx:
location / {
            location ~ /sub/(.*) {
                # activate subscriber (streaming) mode for this location
                push_stream_subscriber;

                # positional channel path
                push_stream_channels_path                 $1;
                push_stream_longpolling_connection_ttl    30s;
                push_stream_last_received_message_time    $arg_time; 
                push_stream_last_received_message_tag     $arg_tag;
            }
            location /pub {
               # activate publisher (admin) mode for this location
               push_stream_publisher admin; 
                # query string based channel id
                push_stream_channels_path               $arg_id;
            }
            location /channels-stats {
                # activate channels statistics mode for this location
                push_stream_channels_statistics;

                # query string based channel id
                push_stream_channels_path               $arg_id;
            }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question