Messages sent multiple times
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?
Should I do mychannel.off() after publishing? or will .unsubscribe do that? Can't seem to figure it out from the docs.
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
});
}