Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add singleton subscriptions #101

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add singleton subscriptions #101

wants to merge 2 commits into from

Conversation

Sephi-Chan
Copy link

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.

@dvdplm
Copy link

dvdplm commented Nov 9, 2011

+1 on including this.

@duncanbeevers
Copy link
Contributor

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
Copy link
Contributor

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.

@Sephi-Chan
Copy link
Author

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.

@goncalossilva
Copy link

+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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants