1

I know that i can subscribe for listen the changes on a table doing this

r.table('users').changes().run(conn, function(err, cursor) {
  cursor.each(console.log);
});

But, how i can stop to listen this changes? How i can unsubscribe for this changes?

1 Answers1

1

You can just call cursor.close(), (or you can do conn.close())

From the docs for cursor.close:

r. Closing a cursor cancels the corresponding query and frees the memory associated with the open request.

Additionally, closing the connection itself will automatically close all cursors associated with a connection. In other words, if you're closing connections properly, you usually won't need to explicitly close your cursors.

From the connection.close docs:

Closing a connection normally waits until all outstanding requests have finished and then frees any open resources associated with the connection. By passing false to the noreply_wait optional argument, the connection will be closed immediately, possibly aborting any outstanding noreply writes.

deontologician
  • 2,764
  • 1
  • 21
  • 33
  • 1
    I'm not sure what other information would be helpful here. The question asked how to unsubscribe from a changefeed, with example code. I explained how to do that using his variable names, implicitly referring to his snippet. – deontologician Jun 12 '15 at 00:12
  • Fair enough, I've added links to the docs and copied in relevant snippets – deontologician Jun 12 '15 at 00:53
  • Thanks for the answers. I asked this because i'm trying to create an alert system in my application, so when a user create an alert, i will subscribe him to a changefeed, but the user can remove this alert too, so i need a way to unsubscribe this user of this feed. And the create and remove methods are endpoints of my API. Have some idea of how i can do that? – Marco Paulo Patricio Jun 12 '15 at 17:55
  • Store your cursor somewhere in your app state, and close it when the user calls the remove() method. – deontologician Jun 12 '15 at 20:08
  • So, i can do that, `var myCursor; r.table('users').changes().run(conn, function(err, cursor) { myCursor = cursor; }); ` And then just call: myCursor.close(); This works? – Marco Paulo Patricio Jun 12 '15 at 20:43
  • Exactly, you just need to store your cursor somewhere – deontologician Jun 14 '15 at 06:24
  • But this is not a good solution, because if my application is down, i will lost the variable values. And when my application is back, how i can get the values of all cursors again? – Marco Paulo Patricio Jun 14 '15 at 22:09
  • In rethinkdb 2.2, we're adding the ability to have persistent changefeeds that will survive a lost connection and keep all of your events. Right now there's no way around this if the application goes down – deontologician Jun 20 '15 at 03:04
  • I should mention, however, that usually this isn't a huge problem. You can just refetch the current value from the server, and start a new changefeed. Persisting across restarts only matters if you cannot afford to miss *any* intermediate events, or if refetching results from scratch would be too expensive (say you have thousands of results) – deontologician Jun 20 '15 at 03:06