slact / nchan.js

NPM package for the Javasript client for Nchan
Other
91 stars 25 forks source link

Repeated messages dropped #10

Open ivanovv opened 6 years ago

ivanovv commented 6 years ago

OK, not sure if it nchan or nchan.js problem, but here it goes:

We are trying to migrate from Faye, and while our chat mostly works fine with nchan, there are several issues. I will describe one as it seems like something you would be able to reproduce

The problem: when chat users are in the same browser process, just different tabs, repeated messages are ignored sometimes. After I was reported of this, I tried to reproduce and changed out chat backend code to send the message 50 times instead of one.

If I use Chrome + Safari pair, everything works just fine - every time I send a message, I receive it in every browser 50 times. If I use several Chrome tabs in incognito mode, everything works fine, too.

if I use two tabs in one Chrome window, this is what happens - the "broken" tab will receive the message sent just once, while the good tab will receive it 50 times.

What it a "broken" tab? The backend injects several channel URLs for the frontend, like this

<script type="text/javascript">var nchanUrls = (nchanUrls || []).concat(['https://dev.company.com//sub/channel/2']);</script>
<script type="text/javascript">var nchanUrls = (nchanUrls || []).concat(['https://dev.company.com//sub/channel/2/private/2']);</script>

and then this simple script listens to messages

Pubsub = {
  init: function () {
    self = this;
    self.online = false;
    for (var i = 0; i < nchanUrls.length; i++ ) {
      var nchanUrl = nchanUrls[i];
      var sub = new NchanSubscriber(nchanUrl, { subscriber: ['websocket', 'longpoll'], shared: true });
      sub.on('message', Pubsub.nchanMessage);
      sub.on('connect', function(evt) {
        self.online = true;
      });
      sub.on('disconnect', function(evt){
        self.online = false;
      });
      sub.start();
    }
  },

  nchanMessage: function(message, message_metadata) {
    // message is a string
    // message_metadata is a hash that may contain 'id' and 'content-type'
    message = JSON.parse(message);
    eval(message.data.eval);
  }
}

So I call a "good" tab a tab that opens exactly the same number of WS connections as the number of URLs passed from the backend.

A "broken" tab will have less connections open than the number of URLs

Some screenshots: this is a good tab: screen shot 2017-12-14 at 17 30 33

this is a "broken" tab (it has only one connection open, but 2 URLs were passed from the backend): screen shot 2017-12-14 at 17 31 22

Another observation - usually, it will always be the same - one tab is broken, another one is good. Not that it randomly both good, both broken, one good and another one broken.

If I enable the reconnect feature, some times they will change sides (broken becomes good, and vice versa).

Same behaviour in both browser I've tested, Safari (9.1.3 (9537.86.7.8)) and Chrome (59.0.3071.115).