【问题标题】:How to manage multiple user changefeeds with RethinkDB in node.js app如何在 node.js 应用程序中使用 RethinkDB 管理多个用户更改提要
【发布时间】:2017-02-08 20:38:39
【问题描述】:

我需要一些关于如何在我的 node.js 应用程序中为多个用户使用 rethinkDb 的 change-feed 的建议。

基本上我希望当用户登录我们的网络应用程序时,我想为该特定用户设置一个更改提要,以监控对用户组织过滤的特定表的更改。

我在想,当用户连接时,他们也将通过 socketio 连接,我可以在连接时为用户分配一个 change-feed。然后在断开连接时关闭该用户光标。

可能是这样的:

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.
    })
}

请原谅代码中的任何错误,但这是我处理最终目标的最初想法。

我只是想知道您对如何正确处理我之前所说的最终目标的任何建议。

提前感谢大家的时间和建议。非常感谢。

【问题讨论】:

  • 您测试该代码时发生了什么?

标签: javascript node.js rethinkdb


【解决方案1】:

我也在寻找有关此的一些提示。我看到了另一种方法 - 使用您的数据订阅表格以获取更改提要,然后让您的代码使用一些新数据更新特定用户。

在为每个用户创建一个新的 changefeed 的情况下,恐怕最终会同时出现很多 changefeed。不知道有多少可以同时运行。 如果用户通过简单地关闭浏览器注销会发生什么?我相信 changefeed 将继续运行。数据库将充满未关闭的更改提要,并且永远不会收到用户不再对接收更改感兴趣的反馈。

【讨论】:

    【解决方案2】:

    您可以使用它在 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(),
                        };
                    })
    

    【讨论】:

      【解决方案3】:

      如果所有用户都订阅了同一个 changefeed,并且您不需要将 includeInitial 设置为 true,则您可以为服务器只创建一个 changefeed(而不是每个连接到服务器一个)并推送 changefeed到 socket.io 房间,然后在连接时将用户添加到房间。这样做的好处是可以节省 rethink 集群上的 cpu 资源

      服务器端:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-01
        • 2015-01-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多