ryanb / private_pub

Handle pub/sub messaging through private channels in Rails using Faye.
MIT License
863 stars 226 forks source link

private_pub.js subscription callback and unsubcribe function. #60

Closed vic closed 11 years ago

vic commented 12 years ago

Hi,

I worked on solving issues #45, #37.

brianweiner commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

+1

ryanb commented 11 years ago

Thanks for your contribution!

vic commented 10 years ago

@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 commented 10 years ago

@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 commented 10 years ago

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