0

My problem is related to disconnect event of Faye. I can easily subscribe to a channel from java script and i can also handle the /meta/subscribe and /meta/connect. But I want some thing like a button in my html.erb file and using that i want to disconnect or unsubscribe from the channel. I want to know how do i fire up a java script for disconnecting from the channel like we do while subscribing to a channel as given below:

$(function() {
  var faye = new Faye.Client('http://localhost:9292/faye');
    faye.subscribe("/messages/new", function(data) {
     eval(data);
    });
});

please provide me a controller code or java script code. Thanks

Braham Shakti
  • 1,408
  • 4
  • 22
  • 39

1 Answers1

2

The subscribe() method returns a Subscription object, which you can cancel if you want to remove that listener from the channel.

So what you can do is to store the object in another variable named subscription then store the subscription object returned by the subscribe method inside it.

var subscription;
$(function() {
  var faye = new Faye.Client('http://localhost:9292/faye');
  subscription = faye.subscribe("/messages/new", function(data) {
    eval(data);
  });
});

$('#button').click(function() {
  subscription.cancel();
});

So in this case when you click the button that has id button it will cancel the subscription. I would suggest checking if you are already subscribed before cancelling.

Source for subscription/cancel: http://faye.jcoglan.com/browser/subscribing.html