How to parse Backtrack response to get old messages?

80 views
Skip to first unread message

krishnaxv

unread,
Dec 29, 2015, 6:42:47 AM12/29/15
to nginxpushstream
Hey Wandenberg,

I am trying to get old messages for a channel. But, I am getting following HTML response. My target data is in p() function in the <script> tag. It will be great if you can please help me out to retrieve this data? Can I use pushstream.onmessage callback function?

<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="Thu, 1 Jan 1970 00:00:00 GMT">
<script type="text/javascript">
window
.onError = null;
try{ document.domain = (window.location.hostname.match(/^(\d{1,3}\.){3}\d{1,3}$/)) ? window.location.hostname : window.location.hostname.split('.').slice(-1 * Math.max(window.location.hostname.split('.').length - 1, (window.location.hostname.match(/(\w{4,}\.\w{2}|\.\w{3,})$/) ? 2 : 3))).join('.');}catch(e){}
parent
.PushStream.register(this);
</script>
</head>
<body><script>p(9,'RNuCFWJh1a','Try 7!','', 'Tue, 29 Dec 2015 11:24:57 GMT', '1');</script><script>p(10,'RNuCFWJh1a','Try 8!','', 'Tue, 29 Dec 2015 11:29:18 GMT', '1');</script>

I am using following code to subscribe to a channel. I am sending an additional AJAX request on page load to get the old messages. Is it the right way to retrieve old messages or it can be done through web sockets only.

<script charset="utf-8">
 
function messageReceived(text, id, channel) {
    console
.log(text)
    document
.querySelector('#messages').innerHTML += id + ': ' + text + '<br>';
 
}


 
var HOST = '192.168.1.188';
 
var PORT = 9080;
 
var pushstream = new PushStream({
   
// host: window.location.hostname,
   
// port: window.location.port,
    host
: HOST,
    port
: PORT,
    modes
: 'websocket',
    messagesPublishedAfter
: 5,
    messagesControlByArgument
: true
 
});
  pushstream
.onmessage = messageReceived;
  pushstream
.addChannel('RNuCFWJh1a');
  pushstream
.connect();


 
/* Backtrack Request */
 
var request = new XMLHttpRequest();
  request
.onreadystatechange= function () {
   
if (request.readyState == 3) {
      console
.log(request.responseText);
      request
.abort();
   
}
 
}
  request
.open('GET', 'http://' + HOST + ':' + PORT + '/sub/RNuCFWJh1a', true);
  request
.setRequestHeader('If-Modified-Since', 'Tue, 29 Dec 2015 10:40:45 GMT');
  request
.setRequestHeader('If-None-Match', '1');
  request
.send();
</script>

Please guide me to understand what am I doing wrong.

Thanks,
krishnaxv

Wandenberg Peixoto

unread,
Dec 29, 2015, 6:56:30 AM12/29/15
to nginxpu...@googlegroups.com
Hi Krishnaxv,

based only on your description, since you didn't send your configuration, I guess that your /sub location is configured to handle the default stream not long polling. 
Take a look on the examples at README.
When you proper configure your location to receive long polling requests it will answer with the json, again assuming is that format you have configured.

There is any reason to not use the PushStrem client on both cases (websocket and long polling)?
It was designed to help an easy setup of the module isolating your javascript code from the mode used to connect to the server.
Of course, you can write your own client code, just will be a little bit harder to handle the connections mode in a cross browser environment.

Regards,
Wandenberg

--
You received this message because you are subscribed to the Google Groups "nginxpushstream" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nginxpushstre...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

krishnaxv

unread,
Dec 29, 2015, 7:43:05 AM12/29/15
to nginxpushstream
Hey Wanden,

I am using the PushStream client with WebSockets only. The only thing I am doing additional is, sending an AJAX request separately to get the old messages as I am not able to retrieve it via the details mentioned on GitHub WebSocket - Getting old messages. Rest of the messages are retrieved by the default PushStream client only as mentioned in my code via WebSocket connection.

I just checked my configuration & actually we were setting push_stream_header_templatepush_stream_message_templatepush_stream_footer_templatedefault_type as mentioned below.

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


   
# positional channel path
    push_stream_channels_path                   $1
;
   
if ($arg_tests = "on") {
      push_stream_channels_path                
"test_$1";
   
}


   
# header to be sent when receiving new subscriber connection
    push_stream_header_template                
"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta http-equiv=\"Cache-Control\" content=\"no-store\">\r\n<meta http-equiv=\"Cache-Control\" content=\"no-cache\">\r\n<meta http-equiv=\"Pragma\" content=\"no-cache\">\r\n<meta http-equiv=\"Expires\" content=\"Thu, 1 Jan 1970 00:00:00 GMT\">\r\n<script type=\"text/javascript\">\r\nwindow.onError = null;\r\ntry{ document.domain = (window.location.hostname.match(/^(\d{1,3}\.){3}\d{1,3}$/)) ? window.location.hostname : window.location.hostname.split('.').slice(-1 * Math.max(window.location.hostname.split('.').length - 1, (window.location.hostname.match(/(\w{4,}\.\w{2}|\.\w{3,})$/) ? 2 : 3))).join('.');}catch(e){}\r\nparent.PushStream.register(this);\r\n</script>\r\n</head>\r\n<body>";


   
# message template
    push_stream_message_template                
"<script>p(~id~,'~channel~','~text~','~event-id~', '~time~', '~tag~');</script>";
   
# footer to be sent when finishing subscriber connection
    push_stream_footer_template                
"</body></html>";
   
# content-type
    default_type                                
"text/html; charset=utf-8";


   
if ($arg_qs = "on") {
      push_stream_last_received_message_time
"$arg_time";
      push_stream_last_received_message_tag  
"$arg_tag";
      push_stream_last_event_id              
"$arg_eventid";
   
}
}

I just removed push_stream_header_templatepush_stream_footer_templatedefault_type & changed push_stream_message_template to "{\"id\":~id~,\"channel\":\"~channel~\",\"text\":\"~text~\", \"tag\":\"~tag~\", \"time\":\"~time~\", \"eventid\":\"~event-id~\"}";

And, now I am getting the response as JSON.

Although it is working now, but it will be great if you can please let me know if I am doing anything wrong.

Thanks,
krishnaxv

krishnaxv

unread,
Dec 29, 2015, 7:45:01 AM12/29/15
to nginxpushstream
I forgot to mention my new configuration.

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


   
# positional channel path
    push_stream_channels_path                   $1
;
   
if ($arg_tests = "on") {
      push_stream_channels_path                
"test_$1";
   
}



   
# message template
    push_stream_message_template                
"{\"id\":~id~,\"channel\":\"~channel~\",\"text\":\"~text~\", \"tag\":\"~tag~\", \"time\":\"~time~\", \"eventid\":\"~event-id~\"}";



   
if ($arg_qs = "on") {
      push_stream_last_received_message_time
"$arg_time";
      push_stream_last_received_message_tag  
"$arg_tag";
      push_stream_last_event_id              
"$arg_eventid";
   
}
}

Thanks,
krishnaxv

Wandenberg Peixoto

unread,
Dec 29, 2015, 8:08:38 AM12/29/15
to nginxpu...@googlegroups.com
Why you are not being able to retrieve old messages like in GitHub WebSocket - Getting old messages? 
You are opening two connections to the server unnecessarily. This is not good.

If it is working for you it isn't wrong.
But surely is not on best fit.

Try to use only the websocket connection, since this seems to be your initial target.
Give a try on the example configuration and code, it is simple but probably do what you need.

Another tip, take care, it seems that you are using the file at misc/nginx.conf as base to your configuration, this file is used by me to develop and test and there are a lot of configuration unnecessary for production environment.

Regards,
Wandenberg

krishnaxv

unread,
Dec 29, 2015, 9:22:13 AM12/29/15
to nginxpushstream
Thanks!

I'll again give a try to the example configuration & code.

Thanks,
krishnaxv
Reply all
Reply to author
Forward
0 new messages