简体   繁体   English

如何使用node.js应用程序中的RethinkDB管理多个用户更改进程

[英]How to manage multiple user changefeeds with RethinkDB in node.js app

I need some advice on how to handle using rethinkDb's change-feeds for multiple users in my node.js app. 我需要一些关于如何处理我的node.js应用程序中为多个用户使用rethinkDb的更改源的建议。

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. 基本上我想要当用户登录我们的Web应用程序时,我想为该特定用户设置更改源,以监视由用户组织筛选的特定表的更改。

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. 我在想,当用户连接时,他们也会通过socketio进行连接,我可以在连接时为用户分配更改。 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. 非常感谢。

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. 数据库将充满非封闭的更改源,并且永远不会得到用户不再对接收更改感兴趣的反馈。

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 您可以使用它在rethinkdb中查询以检测任何更改,并记住它只适用于rethinkdb上下文希望这会提供一些帮助

  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(),
                    };
                })

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. 如果所有用户都订阅了相同的更改源,并且您不需要将includeInitial设置为true,则只能为服务器创建一个更改源(与每个服务器连接相反)并将更改源推送到套接字.io房间然后将用户添加到连接房间。 This has the advantage of conserving cpu resources on your rethink cluster 这样做的好处是可以在重新考虑群集上节省cpu资源

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');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM