Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

private_pub.js subscription callback and unsubcribe function. #60

Merged
merged 3 commits into from
Feb 22, 2013

Conversation

vic
Copy link
Contributor

@vic vic commented Aug 22, 2012

Hi,

I worked on solving issues #45, #37.

If options given to `sign` include a `subscription` function, it
will be called with the newly created subscription object. This
way users can add a Faye callback to know when a subscription is
ready or cancel a subscription in the future.
…bject.

Store subscription objects on an internal object and expose
three new methods: `subscription(channel)`, `unsubscribe(channel)` and
`unsubscribeAll()`
@brianweiner
Copy link

I tried this out and it works pretty good... is there a way to get PrivatePub to publish a "subscription" message on the channel that was subscribed to?

@vic
Copy link
Contributor Author

vic commented Oct 12, 2012

Hey,

Sure you can, I mean, on the client side when you're signing to a channel, you can pass a callback that will be called whenever the subscription is successful. (See https://github.com/vic/private_pub/blob/a0abfba2ca774303e00321f9565ec99fea7dc97c/spec/javascripts/private_pub_spec.js#L66 )

// on privatepub this data is sent by server to client
var opts = {
  server: "foo",
  channel: "/hello/world",
  ...
}

// and you can add a subscription callback
// it will take a Faye subscription object. (see http://faye.jcoglan.com/browser/subscribing.html)
opts.subscription = function(subscription) {
    subscription.callback(function() {
      // subscription was successful here.
      // send an initial message on that channel.
      PrivatePub.faye(function(faye){
         faye.send("/hello/world", {text: 'Hey I just connected!'});
      });
   });
}

PrivatePub.sign(options)

something like that. (havent tried it but hope that shows the path)

@vrinek
Copy link

vrinek commented Oct 13, 2012

+1

ryanb added a commit that referenced this pull request Feb 22, 2013
private_pub.js subscription callback and unsubcribe function.
@ryanb ryanb merged commit f25fa62 into ryanb:master Feb 22, 2013
@ryanb
Copy link
Owner

ryanb commented Feb 22, 2013

Thanks for your contribution!

@vic
Copy link
Contributor Author

vic commented Mar 12, 2014

@sternhenri PrivatePub.unsubscribe('/hello/world') would unsubscribe you from the /hello/world channel. You can always obtain the faye subscription object itself if you provide a channel name to the PrivatePub.subscription function.

@sternhenri
Copy link

@vic Hmm. I'm running 1.0.3 (latest version I think) and couldn't find the function when I tried to run it in the rails console... I'll look further :D. Thanks!

@vic
Copy link
Contributor Author

vic commented Mar 14, 2014

@sternhenri just to be clear, the examples I mentioned earlier are for javascript client side code, once the client is connected to a channel and wants to unsubscribe, so you wont find them at the rails console.

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

Successfully merging this pull request may close these issues.

5 participants