3

I need some advice on how to handle using rethinkDb's change-feeds for multiple users in my node.js app.

Essentially I want when a user logs into our web app, I want to set up a change-feed for that particular user to monitor changes to a particular table filtered by the users organization.

I was thinking that when a user connects, they will also connect via socketio, and i could assign a change-feed for the user on connect. Then close that users cursor on disconnect.

Maybe something like:

io.on('connection', (socket)=> { 
    //Assume user information is in socket.user
    r.db('database').table('entries').filter({organization: socket.user.organization}).changes().run(conn, (err, cursor){
        cursor_holder[socket.user.id] = cursor //Maybe hold the cursors in memory while the user is connected?
        cursor.each((err, entry)=>{
            socket.emit('update', entry);
        })
    })

    socket.on('disconnect', ()=>{
        cursor_holder[socket.user.id].close() //I dont know what the exact close method is for the feed.
    })
}

Please excuse any errors in the code but this was the initial idea I had to handle my end-goal.

I'm just curious for any suggestions you may have how to properly handle the end-goal I stated earlier.

Thank you all in advance for your time and suggestions. Its greatly appreciated.

3 Answers3

0

I am also looking for some tips about this. I see another approach - subscribing a table with your data for changefeeds and then let your code update a specific user with some new data.

In the case of creating a new changefeed for every user, I am afraid ending up with a lot of simultaneous changefeeds. Not sure how many can run at the same time. What happens if a user logs out by simply closing a browsers? The changefeed will continue to run, I believe. The database will be full of non-closed changefeeds and will never get a feedback that the user is no longer interested in receiving changes.

Oleg
  • 187
  • 3
  • 5
0

You can use this for querying in rethinkdb for detecting any changes and keep in mind that it will only for rethinkdb context hope this will lend some help

  yield r.table('Us').filter(function (S) {
                    return S  //your rethinkdb query
                })
                .changes().merge(function () {
                    return {
                        total: r.table('Us').filter(function (x) {
                            return x    //your rethinkdb query
                        }).count(),
                    };
                })
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DEO
  • 316
  • 2
  • 4
  • 18
0

If all the users are subscribing to the same changefeed, and you do not require that includeInitial be set to true, you can create just one changefeed for the server (as oppossed to one per connection to the server) and push the changefeed to a socket.io room and then add the user to the room on connect. This has the advantage of conserving cpu resources on your rethink cluster

Server side:

var io = require('socket.io')(httpApp, {options});

r.table('cats')
.filter({location : 'kitchen'})
.changes({includeTypes : true})
.run(rethinkConnection, function(err, cursor) {
    if (err) {
        // error handling
    } else {
        cursor.each(function(err, row) {
            if (err) {
                // error handling
            } else {
                io.to('kitcatFeed').emit('kitcatChange', row);
            }
        });
    }
});

io.on('connection', function(socket) {
    socket.join('kitcatFeed');
});
nickanna_42
  • 169
  • 12