juggernaut icon indicating copy to clipboard operation
juggernaut copied to clipboard

Add singleton subscriptions

Open Sephi-Chan opened this issue 14 years ago • 5 comments

Sometime you need to have only one subscription to a channel, regardless the number of times you call subscribe. It may be useful when you subscribe in an AJAX callback.

$(".user a.follow").live("click", function(event){
  $.post(this.href, function(data, status, xhr){
    var channel = "users/" + data.user.id + "/feed";
    juggernaut.singleSubscribe(channel, function(feedEntry){
      console.log("Got new feed entry: " + feedEntry);
    }
  });
});

Even if the user click many times to follow an user, each new messages of this users's feed will be pushed to the client only once.

Sephi-Chan avatar Oct 13 '11 19:10 Sephi-Chan

+1 on including this.

dvdplm avatar Nov 09 '11 19:11 dvdplm

When does it make sense to have multiple subscriptions to a single channel? I think this should be the default behavior, not a special path.

duncanbeevers avatar Nov 09 '11 20:11 duncanbeevers

Or is the use case adding multiple subscriptions to a single channel, each with a different handler for the data?

I think the identity of the handler should be sufficient, and in this case you shouldn't be subscribing with anonymous handlers.

For example, I think it's reasonable for Juggernaut to detect and thwart duplicating messages to a handler added like this:

function followHandler(feedEntry) {
  console.log("Got new feed entry: " + feedEntry);
}

$(".user a.follow").live("click", function(event){
  $.post(this.href, function(data, status, xhr){
    var channel = "users/" + data.user.id + "/feed";
    juggernaut.subscribe(channel, gotFeedEntry);
  });
});

However, when passing an anonymous handler like in the initial example, it is perfectly reasonable to add each "new" handler.

duncanbeevers avatar Nov 09 '11 20:11 duncanbeevers

I can patch to use this behavior, @duncanbeevers. It's a good idea : subscribe only if it's a new callback. I would be glad to have @maccman's opinion on this.

Sephi-Chan avatar Nov 10 '11 08:11 Sephi-Chan

+1 on @duncanbeevers's comment that this should be the default behavior. If @maccman wants to keep the current subscription model, +1 on this PR.

goncalossilva avatar Mar 07 '12 19:03 goncalossilva