4

Is there a way to unsubscribe or change an existing changefeed observer in rethinkdb? Setting the return value of the changes() function to null doesn't seem to do anything, is there a unsubscribe() function?

What I'd ideally like to do is change one of the index filter parameters (favorites) after the changefeed is created (since changefeeds on joins don't work and I have to change the feed if the underlying favorites collection changes).

Here is the sample code in javascript

  var observer = r.table("users")
                 .getAll(r.args(favorites), {index:"name"})
                 .changes().then(function(results) {


            results.each(function(err,row) {
              if (err) console.error(err);
              var prefix = row.new_val ? 'added' : 'deleted';
              var msg =  row.new_val ?  row.new_val :  row.old_val;
              console.log(prefix + ': ' +  msg.name);
          });

      });

 observer = null; //what do I do there to have it stop observing or unsubscribe... or change the subscription to something else.. say adding a filter or changing a filter?
MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460

2 Answers2

4

Don't know what library are you using for JS. With rethinkdb + rethinkdb-pool you can use this syntaxes:

r.table("users").getAll(r.args(favorites), {index:"name"})
                .changes().run(connection, function(err, cursor) {


     cursor.each(function(err,row) {
         if (err) console.error(err);
         var prefix = row.new_val ? 'added' : 'deleted';
         var msg =  row.new_val ?  row.new_val :  row.old_val;
         console.log(prefix + ': ' +  msg.name);
     });
}

So after that you can just close cursor to stop receiving changes:

cursor.close();

Or you can close connection, and it will automatically close all cursors associated with a connection:

connection.close();
0

I'm not familiar with the JS driver in particular, but generally you should receive a cursor at some point, and calling close on that cursor will close the changefeed. (I'm not sure in the API above whether observer or results is the cursor, but if you print their types you should be able to see.)

mlucy
  • 5,249
  • 1
  • 17
  • 21