wandenberg / nginx-push-stream-module

A pure stream http push technology for your Nginx setup. Comet made easy and really scalable.
Other
2.22k stars 295 forks source link

Invalid JSON error #117

Closed pokal4u closed 10 years ago

pokal4u commented 10 years ago

Hi,

When we send or receive messages getting Invalid Json error

Please find attached file

error

My confg file:

pid logs/nginx.pid; error_log logs/nginx-main_error.log debug; master_process off; daemon off; worker_rlimit_core 500M; working_directory /tmp;

worker_processes 2; worker_rlimit_nofile 50000; events { worker_connections 1024; use epoll; }

http { access_log logs/nginx-http_access.log; tcp_nopush on; tcp_nodelay on; keepalive_timeout 30; send_timeout 10; client_body_timeout 10; client_header_timeout 60; sendfile on; client_header_buffer_size 1k; large_client_header_buffers 8 512k;#2 4k
ignore_invalid_headers on; 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   1000;  
push_stream_message_ttl                       3s;
# 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        5m;

push_stream_timeout_with_body                 off;

# wildcard
push_stream_wildcard_channel_prefix         "broad_";
push_stream_wildcard_channel_max_qtd        3;

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

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

#push_stream_allowed_origins                 "*";

server {
     listen           80 default_server;        
    server_name     localhost;

    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;
    }

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

        # query string based channel id
        push_stream_channels_path               $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
        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";
        }
    }

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

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

        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";
        }
    }

location ~ /lp/(.*) {

        # activate long-polling mode for this location

        push_stream_subscriber      long-polling;

        # positional channel path

         push_stream_channels_path    $1;
    # push_stream_longpolling_connection_ttl        30s;

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

        push_stream_last_received_message_time "$arg_time";
        push_stream_last_received_message_tag  "$arg_tag";
        push_stream_last_event_id              "$arg_eventid";

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

    location ~ /ws/(.*) {
        # activate websocket mode for this location
        push_stream_subscriber websocket;

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

        # store messages in memory
        push_stream_store_messages              on;

        push_stream_websocket_allow_publish     on;

        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";
        }
    }       
}

}

Please help me

Thanks Pokal4u

da4nik commented 10 years ago

As a see, you trying send a json. So you have json inside of json without escaping inner one. After unescaping string, "(quotes) breaks json received from push-stream-module so it can't parse it.

pokal4u commented 10 years ago

Hi Thanks for reply

Please give me one simple example

Thanks Pokal4u

comboy commented 10 years ago

If you are pushing json and not text, this is what works for me:

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

(simply remove quotes)