Search code examples
rethinkdbrethinkdb-javascript

How do I unsubscribe or change a changefeed in rethinkdb?


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?

Solution

  • 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();