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

Don't get message in other browsers tabs. #162

Closed xFalkoN closed 9 years ago

xFalkoN commented 9 years ago

Hi, first of all let me thank you for this great module. I have problem with getting messages in different tabs across browsers. I open 3 tabs in Google Chrome(38.0.2125.111 (64-bit)) and 3 tabs in Mozila Fire Fox (33.0 Mozila for ubuntu). There are 4 long-polling subscruber channels for each tabs. When i push one message in each channel i get next situation:

In first tab of each browsers i get messages of all channels , second tab only 1-2 channels get message and other tabs still pending.

My conf:

push_stream_message_ttl   10s;
push_stream_shared_memory_size 300M; 

fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;

server {
    listen 80;
    server_name steam.example.dev;

    location /channels-stats {
        push_stream_channels_statistics;
        push_stream_channels_path               $arg_id;
    }

    location /pub {
        push_stream_publisher admin;
        push_stream_channels_path               $arg_id;
    }

    location ~ /sub/(.*) {
        push_stream_subscriber;
        push_stream_channels_path               $1;
    }

    location ~ /lp/(.*) {
        push_stream_allowed_origins *;
        push_stream_subscriber      long-polling;
        add_header 'Access-Control-Expose-Headers' 'Etag';
        push_stream_channels_path         $1;
        push_stream_message_template          
"{\"id\":\"~id~\",\"channel\":\"~channel~\",\"text\":~text~,\"time\":\"~time~\",\"eventId\":\"~eventid~\", \"tag\":\"~tag~\"}";
        push_stream_last_received_message_tag       $arg_tag;
        push_stream_last_received_message_time      $arg_time;
        push_stream_longpolling_connection_ttl        30s;
    }
}

I use your pustream.js lib for client

function refresh() { var pushstream = []; for(var i = 0; i <ChanelKeys.length; i++){ pushstream[i] = new PushStream({ host: PushServerHost, timeout: 300000, modes: "longpolling" }); pushstream[i].onmessage = updateWidget; pushstream[i].addChannel(ChanelKeys[i]); pushstream[i].connect(); } } ChanelKeys = array of chanels ids.

Thanks in advance for your help.

wandenberg commented 9 years ago

Hi @xFalkoN

the problem is related with the number of opened connections to the same host, each browser has a different limit.

The right way of subscribe to multiple channels is connecting to all of them on the same request. Since you are using the pushstream.js this can be done with the following code.

function refresh()
{
  var pushstream = new PushStream({
    host: PushServerHost,
    timeout: 300000, 
    modes: "longpolling",
    onmessage: updateWidget
  });

  for(var i = 0; i <ChanelKeys.length; i++){
    pushstream.addChannel(ChanelKeys[i]); 
  }

  pushstream.connect();
}

When the connect method is called it will compose a url like /lp/ch1/ch2/ch3 ... and will deal with the received messages and reconnection for you.

xFalkoN commented 9 years ago

Thank you for the quick and complete answer.