I am setting up a blog between our clients and us so they can have a space to talk about the different commercial transactions (each commercial transaction has a reference number and has it's own information).
Right now I was able to setup Ratchet with ZeroMQ and Autobahn to achieve this. Whenever a user updates the blog, the rest of the users who are subscribed to them get the information pushed into their browsers.
However, I have the subscription method linked to a button in the main page. (The whole idea is that they will click on the reference on the left menu bar, and the blog entries will appear on the right side of the screen).
What this is causing is that if the user clicks on the same reference multiple times, or if the user goes to other reference and then comes back to the first reference, whenever there's an update on the blog, the user receives multiple updates (one per click -> each click triggers a subscription).
Any idea how to prevent this from happening?
Here's how the jquery that creates the session looks like:
$('.reference-container').on('click', function(){
idReferencia = $(this).attr('id');
$('#modifyReference').val(idReferencia);
console.log('Creating connection with server on topic: ' + idReferencia + '..');
var conn = new ab.Session('wss://plt.prolog-mex.com/wss2/',
function() {
conn.subscribe(idReferencia, function(topic, data) {
$('#messageBoard').prepend('<p class="message-nonown">'+data.article+'</p>')
console.log('New article published to category "' + topic + '" : ' + data.title);
});
},
function() {
console.warn('WebSocket connection closed');
},
{'skipSubprotocolCheck': true}
);
console.log
});
You are creating both a new WAMP session and a subscription on that session on each button click.
What you should do is check on click whether both already exist, and just do nothing if this is the case.