long-polling

468 views
Skip to first unread message

ramires

unread,
Apr 11, 2012, 10:13:50 AM4/11/12
to nginxpushstream
hi
i install dotdeb nginx-extras package and use 0.3.3 pushstream.js.
websocket and iframe works perfect. When i try to use long-polling
nginx gives 405 errors. if i use long-polling with tag and time nginx
gives 200 but there is no messages.
"GET /lp/test?_=1334152930760&tag=0&time=Wed%2C%2011%20Apr
%202012%2014%3A01%3A37%20GMT HTTP/1.1" 200

here is my curl results {"channel": "test", "published_messages": "4",
"stored_messages": "4", "subscribers": "0"}

Wandenberg Peixoto

unread,
Apr 11, 2012, 8:35:52 PM4/11/12
to nginxpu...@googlegroups.com
Hi Ramires,

could you send me your nginx configuration file and a step by step how can I reproduce your problem?
On my basic tests everything is working well, but may be a configuration problem or a specific use case which are resulting in an error.

Regards,
Wandenberg

ramires

unread,
Apr 12, 2012, 3:43:41 AM4/12/12
to nginxpushstream
i send a data with

curl -s -v -X POST 'http://89.107.225.193:8000/pub?id=test' -d 'hello'

{"channel": "test", "published_messages": "2", "stored_messages": "2",
"subscribers": "0"}


here is my nginx conf
=================================================

push_stream_shared_memory_size 500m;
push_stream_max_channel_id_length 200;
# max messages to store in memory
push_stream_max_messages_stored_per_channel 50;
# message ttl
push_stream_message_ttl 5m;
# ping frequency
push_stream_ping_message_interval 10s;
# connection ttl to enable recycle
push_stream_subscriber_connection_ttl 15m;
# connection ttl for long polling
push_stream_longpolling_connection_ttl 30s;
# broadcast
push_stream_broadcast_channel_prefix "broad_";

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

# subscriber may create channels on demand or only authorized
# (publisher) may do it?
push_stream_authorized_channels_only off;
push_stream_broadcast_channel_max_qtd 3;



server {
listen 8000 default_server;
#listen 9443 ssl;
#ssl_certificate /usr/local/nginx/ssl/server.crt;
#ssl_certificate_key /usr/local/nginx/ssl/server.key;
server_name 192.168.200.193;

location /channels-stats {
# activate channels statistics mode for this location
push_stream_channels_statistics;

# query string based channel id
set $push_stream_channel_id $arg_id;
}

location /pub {
# allow workstation
allow 192.168.200.200;
# drop rest of the world
deny all;


# activate publisher mode for this location, with admin
support
push_stream_publisher admin;

# query string based channel id
set $push_stream_channel_id $arg_id;

# store messages in memory
push_stream_store_messages on;

# Message size limit
# client_max_body_size MUST be equal to
client_body_buffer_size or
# you will be sorry.
client_max_body_size 32k;
client_body_buffer_size 32k;
}

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

# positional channel path
set $push_stream_channels_path $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(-2).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~');</script>";
push_stream_message_template
"<script>p(~id~,'~channel~','~text~');</script>";
# footer to be sent when finishing subscriber connection
push_stream_footer_template "</body></
html>";
# content-type
push_stream_content_type "text/html;
charset=utf-8";
}

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

# activate event source support for this location
push_stream_eventsource_support on;

# positional channel path
set $push_stream_channels_path $1;
# message template
push_stream_message_template "{\"id\":~id~,
\"channel\":\"~channel~\",\"text\":\"~text~\"}";

# ping frequency
push_stream_ping_message_interval 10s;

}

location ~ /lp/(.*) {
# activate long-polling mode for this location
push_stream_subscriber long-polling;

# positional channel path
set $push_stream_channels_path $1;

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

push_stream_last_received_message_tag $arg_tag;
push_stream_last_received_message_time $arg_time;

# connection timeout
push_stream_longpolling_connection_ttl 30s;


}
location ~ /ws/(.*) {


# activate websocket mode for this location
push_stream_websocket;

# positional channel path
set $push_stream_channels_path $1;
# message template
push_stream_message_template "{\"id\":~id~,
\"channel\":\"~channel~\",\"text\":\"~text~\"}";

# store messages in memory
push_stream_store_messages on;

push_stream_websocket_allow_publish on;

# ping frequency
push_stream_ping_message_interval 10s;

}
}




here is my code
==============================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html;
charset=utf-8" />
<title>Long Polling Example</title>
</head>
<body>
<p>Messages:</p>
<div id="messages" style="width:800px;height:
300px;overflow:scroll;"></div>

<script src="/js/pushstream.js" type="text/javascript"
language="javascript" charset="utf-8"></script>
<script type="text/javascript" language="javascript"
charset="utf-8">
// <![CDATA[
function messageReceived(text, id, channel) {
document.getElementById('messages').innerHTML += id + ': ' +
text + '<br>';
};

var pushstream = new PushStream({
host: 89.107.225.193,
port: 8000,
modes: "longpolling",
});
pushstream.onmessage = messageReceived;
pushstream.addChannel('test');
pushstream.connect();
// ]]>
</script>
</body>
</html>


Wandenberg Peixoto

unread,
Apr 12, 2012, 8:50:07 PM4/12/12
to nginxpu...@googlegroups.com
Hi Ramires,

I tested your configuration and seems you have mixed some examples.
When you set the directives bellow on your longpolling location (/lp) means you will pass this values by query string instead of normal headers.

push_stream_last_received_message_tag       $arg_tag;
push_stream_last_received_message_time      $arg_time;

So you have to configure the PushStream object to send the values using arguments setting the
"longPollingByHeaders: false" configuration. If you want to send the values using other argument names
you can you the configurations "longPollingTagArgument: 'tag' ", and "longPollingTimeArgument: 'time' "

This example is described here on "Not using headers control" topic.

Regards,
Wandenberg

ramires

unread,
Apr 13, 2012, 3:51:29 AM4/13/12
to nginxpu...@googlegroups.com
hi wanden

 websocket, stream, iframe works.Also longpolling versin of jsonp woks but it shows more then one sub and shows duplicate entries.
 
if i use only longpolling i get same error in nginx logs.
========
95.9.238.225 - - [13/Apr/2012:10:44:06 +0300] "OPTIONS /lp/test?_=1334302559419 HTTP/1.1" 405 0 "-" "Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1

 i chande nginx conf like this.

==============================================
    push_stream_shared_memory_size               500m;
    push_stream_max_channel_id_length           200;
    # max messages to store in memory
    push_stream_max_messages_stored_per_channel  50;
    # message ttl
    push_stream_message_ttl                      5m;
    # ping frequency
    push_stream_ping_message_interval           10s;
    # connection ttl to enable recycle
    push_stream_subscriber_connection_ttl        15m;
    # connection ttl for long polling
    push_stream_longpolling_connection_ttl        30s;
    # broadcast
    push_stream_broadcast_channel_prefix        "broad_";

    # subscriber may create channels on demand or only authorized
    # (publisher) may do it?
    push_stream_authorized_channels_only        off;
    push_stream_broadcast_channel_max_qtd       3;



server {
        listen           8000 default_server;
            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(-2).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~');</script>";
            push_stream_message_template                "<script>p(~id~,'~channel~','~text~');</script>";

            # footer to be sent when finishing subscriber connection
            push_stream_footer_template                 "</body></html>";
            # content-type
            push_stream_content_type                    "text/html; charset=utf-8";
        }

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

        # activate event source support for this location
        push_stream_eventsource_support on;

        # positional channel path
        set $push_stream_channels_path              $1;
        # message template
        push_stream_message_template                "{\"id\":~id~,\"channel\":\"~channel~\",\"text\":\"~text~\"}";

        # ping frequency
        push_stream_ping_message_interval           10s;

        }

        location ~ /lp/(.*) {
   
     # activate long-polling mode for this location
        push_stream_subscriber      long-polling;

        # positional channel path
        set $push_stream_channels_path    $1;

        # message template
        push_stream_message_template                "{\"id\":~id~,\"channel\":\"~channel~\",\"text\":\"~text~\"}";


        # connection timeout
        push_stream_longpolling_connection_ttl        30s;

    }
        location ~ /ws/(.*) {


    # activate websocket mode for this location
        push_stream_websocket;

        # positional channel path
        set $push_stream_channels_path              $1;
        # message template
        push_stream_message_template                "{\"id\":~id~,\"channel\":\"~channel~\",\"text\":\"~text~\"}";

        # store messages in memory
        push_stream_store_messages                  on;

        push_stream_websocket_allow_publish         on;

        # ping frequency
        push_stream_ping_message_interval           10s;

        }
    }


my test file is same

==============================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>Long Polling Example</title>
</head>
<body>
    <p>Messages:</p>
    <div id="messages" style="width:800px;height:300px;overflow:scroll;"></div>

    <script src="/js/pushstream.js" type="text/javascript" language="javascript" charset="utf-8"></script>
    <script type="text/javascript" language="javascript" charset="utf-8">
    // <![CDATA[
    function messageReceived(text, id, channel) {
      document.getElementById('messages').innerHTML += id + ': ' + text + '<br>';
    };

    var pushstream = new PushStream({
      host: "89.107.225.193",
      port: "8000",
      modes: "longpolling"

Wandenberg Peixoto

unread,
Apr 13, 2012, 9:09:34 AM4/13/12
to nginxpu...@googlegroups.com
Hi Ramires,

please do another test for me, use other browser or an earlier version of Firefox, to see if it works.
Seems a problem with your browser doing a request with OPTIONS method.
This is not done by pushstream.js, and is not supported by pushstream module, this is the reason you received a 405.

Regards,
Wandenberg

ramires

unread,
Apr 15, 2012, 9:07:24 AM4/15/12
to nginxpu...@googlegroups.com
hi wanden
 
 i tested longpolling explorer 6,7,8,9 it worked all. Also i tested on firefox 11, chrome 18 and safari they all failed. i got options and 405 error.

Wandenberg Peixoto

unread,
Apr 15, 2012, 6:27:35 PM4/15/12
to nginxpu...@googlegroups.com
Hi Ramires,

It is very strange. I tested in firefox 11, an chrome 18, without problems, any OPTIONS request was made.
Are you using any browser extension which could cause this problem?
Have you tested the chat example http://www.nginxpushstream.com/chat_longpolling.html.

Regards,
Wandenberg

ramires

unread,
Apr 16, 2012, 2:51:54 AM4/16/12
to nginxpu...@googlegroups.com
hi wanden

 Your chat script worked on my firefox 11 an chrome 18. But same script didn`t work on our server. I think we have a strange configuration problem on nginx. We use dotdeb version nginx. Our nginx conf same as above. Can you sand me your nginx conf ?

ii  nginx-common                     1.0.14-1~dotdeb.1            small, but very powerful and efficient web server (common files)
ii  nginx-extras                     1.0.14-1~dotdeb.1            nginx web server with full set of core modules and extras

Marek Janda

unread,
Apr 16, 2012, 5:43:56 AM4/16/12
to nginxpu...@googlegroups.com

OPTIONS means it's considered cross domain request and expects Access-Control-Allow-Origin header (or some other solution like relaxing document.domain etc.).

--

S pozdravem

Marek Janda

 

 

ramires

unread,
Apr 17, 2012, 3:28:31 AM4/17/12
to nginxpu...@googlegroups.com
hi marek
 
  I put all nginx confs in same domain but nothings change. i put "longPollingByHeaders: false" option and time,tag in script. After I get this but there is no messages with firefox and chrome.

95.9.238.225 - - [17/Apr/2012:09:36:23 +0300] "GET /lp/test?_=1334644092172&tag=0&time=Tue%2C%2017%20Apr%202012%2006%3A27%3A43%20GMT HTTP/1.1" 200 397 "http://" "Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1 srktctbv01"
95.9.238.225 - - [17/Apr/2012:09:36:26 +0300] "GET /lp/test?_=1334644095237&tag=0&time=Tue%2C%2017%20Apr%202012%2006%3A27%3A43%20GMT HTTP/1.1" 200 397 "http://" "Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1 srktctbv01"
 
   
   I also notice that longpolling with explorer crash after some time. After few minutes it pulled 10 diffrent subs and increased.

Wandenberg Peixoto

unread,
Apr 17, 2012, 10:22:20 AM4/17/12
to nginxpu...@googlegroups.com
Hi Ramires,

You are getting very a strange behavior in you server.
Can you give me access to your server to help you debug the problem?
I can't solve a bug which I can't reproduce, sorry :(

Regards,
Wandenberg

Wandenberg Peixoto

unread,
Apr 26, 2012, 10:18:21 PM4/26/12
to nginxpu...@googlegroups.com
Marek point me to right direction.

Some browsers are considering a request to same server but using a different port as a cross domain request.
And to allow this they does a "preflight" request with OPTIONS method as explained here 
I implemented on longpolling_with_array_support branch the correct response.
Please test last updates, and if everything is ok I will merge the code into master branch.

Regards,
Wandenberg
Reply all
Reply to author
Forward
0 new messages