socketcluster icon indicating copy to clipboard operation
socketcluster copied to clipboard

Messages sent multiple times

Open alex198211 opened this issue 7 years ago • 2 comments

Hello,

I have this code:

var mychannel = socket.subscribe("updates-" + payload[1] + "-" + payload[4]);
mychannel.on('subscribe', function(){
    mychannel.publish(dbmsg.payload, function(err, ackData){
        mychannel.unsubscribe();
        console.log("adjust: sent " + dbmsg.payload);
    });
});

The payload is a string like "UPDATE;;995502044;;1.65;;2.65;;276" that I split.

Whenever that code is triggered it sends some of the previous messages, for example if I try to send this payload "UPDATE;;995502044;;0.00;;15.00;;276" it logs this:

adjust: sent UPDATE;;995502044;;1.65;;2.65;;276 adjust: sent UPDATE;;995502044;;2.65;;3.65;;276 adjust: sent UPDATE;;995502044;;3.65;;4.65;;276 adjust: sent UPDATE;;995502044;;4.65;;0.00;;276 adjust: sent UPDATE;;995502044;;0.00;;15.00;;276 <---- the payload I just sent. all above are older.

It's basically re-sending the whole message history for some reason.

Am I doing something wrong?

alex198211 avatar Sep 28 '18 01:09 alex198211

Should I do mychannel.off() after publishing? or will .unsubscribe do that? Can't seem to figure it out from the docs.

alex198211 avatar Sep 28 '18 10:09 alex198211

This is an old question, but I ran into this issue and solved it. So maybe others can benefit from my answer.

I would suggest using mychannel.isSubscribed() (https://socketcluster.io/#!/docs/api-scchannel-client). If you are in dev mode (with hot-reload) mychannel.on('subscribe'... is going to be run each time and you will end up with several calls.

if (!mychannel.isSubscribed()) {
  mychannel.on('subscribe', function(){
      // content
  });
}

hekigan avatar Jun 06 '19 06:06 hekigan