wandenberg / nginx-push-stream-module

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

Long poll messages lost #268

Closed M1ha-Shvn closed 6 years ago

M1ha-Shvn commented 6 years ago

Hey. I face a problem in our project with long-poll. When multiple messages come in a very short period of time, some of them are lost (not received by client). 1) Any suggestions, why messages are lost? 2) I see ~ id ~ field in every received message, and as I see (but not found in the docs) it is sequential for every channel and different for different channels. Can I control that all messages are received, using this id (and expecting next message with id+1). P. s. we use web-socket, when it is possible, and no problem there.

wandenberg commented 6 years ago

@M1hacka how are you doing the connection to get the messages in long polling mode? Every message has two information to make them "unique" and sequential, the time and tag. Especially in long polling mode, it is required to the client send to the server which was the last message received informing the time and tag of it. Without this, you will see this "lost message" effect. Try to check the examples that are in the redame. They explain a little bit about how it works.

M1ha-Shvn commented 6 years ago

Here is the config:

push_stream_shared_memory_size 2048M;
push_stream_channel_inactivity_time 15s;
push_stream_ping_message_text "ping";
push_stream_message_ttl 180s;

# Other non-pushstream options here

server {
        listen 80;
        listen 443 ssl;

        location ~ ^/longpoll/(.*)$ {
                set $carrot_channels $1;
                set $carrot_cache_key "${carrot_channels}__${arg_auth_token}";
                auth_request /auth;
                #auth_request_set $my-cache-status $upstream_cache_status;
                #add_header X-Cache-Status my-cache-status;
                push_stream_subscriber long-polling;

                push_stream_channels_path $1;
                push_stream_last_received_message_time $arg_time;
                push_stream_last_received_message_tag  $arg_tag;

                push_stream_message_template "{\"id\": ~id~, \"channel\": \"~channel~\", \"message\": ~text~, \"tag\": \"~tag~\", \"time\": \"~time~\"}\n";

                push_stream_longpolling_connection_ttl 25s;
                add_header Access-Control-Allow-Origin * always;
                add_header Access-Control-Expose-Headers "ETag, Last-Modified" always;
        }

       # Other locations here: websocket, shortpoll etc
}
M1ha-Shvn commented 6 years ago

Is arg_time timestamp in seconds? That may be the problem, if many messages come in the same second?

wandenberg commented 6 years ago

The arg_time precision is in seconds, but the value must be represented in the same format you are receiving in your messages. No, is there any problem if more than one message comes in the same second, this is why the tag is used for. The first message on the second will have tag 1, the second 2, ... And this counter is reset when the second changes. Just ensure that you are sending the values to the server, the same you received in the last message without changing it and you will be good. As I said, check the examples and the code in pushstream.js if you want to. It will give a better idea on how to use it and do your implementation. (Or use the pushstream client if want)

M1ha-Shvn commented 6 years ago

Thanks, found client side bag with tags and time.