如何计算流星出版物的当前订阅数 [英] How to count the current number of subscriptions to a meteor publication

查看:94
本文介绍了如何计算流星出版物的当前订阅数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在流星服务器上有一个出版物,我想知道当前有多少客户订阅了该出版物.这样做的原因是,我想向出版物所有者"显示订阅计数.

I have a publication on the meteor server and I would like to know how many clients are currently subscribed to that publication. The reason for this is, that I would like to show subscription count to a "publication owner".

这是我尝试实现此目标的方法的简化代码(当然,不是执行console.log而是增加/减少共享文档)

Here is a simplified code-snipped of how I try to achieve this (of course instead of doing a console.log I would increment/decrement a shared document)

Meteor.publish('sessionForId', function (sessionId) {
  console.log('increment subscription count for ' + sessionId);

  this.onStop(function(){
    console.log('decrement subscription count for ' + sessionId);
  });

  return Sessions.find({_id: sessionId});
});

通常可以,但是存在一些问题:

In general this works, but there are some problems:

  • 如果我重新启动服务器,则订阅计数不同步(停止/杀死/重新启动服务器时不会触发onStop-Events)
  • 可能还有其他一些极端情况,可能会使订阅计数不同步

我也许可以通过在启动时将计数设置为0来解决其中一些问题,但是也许有更好的方法可以查询服务器以获取出版物的当前订阅量?

I could probably work around some of these by i.e. setting the count to 0 on startup, but maybe there is a better way to query the server for the current amount of subscriptions to a publication?

推荐答案

我在文档中没有发现任何比您的想法更好的方法.

I didn't see anything in the docs how to do it better than your idea.

下面显示了另一种可能的方法,该方法遍历这些订阅对象: https://github.com/meteor/meteor /blob/devel/packages/ddp-server/livedata_server.js#L241

There's another possible approach shown below which iterates over these subscription objects : https://github.com/meteor/meteor/blob/devel/packages/ddp-server/livedata_server.js#L241

您可以通过在服务器流星代码中添加以下代码来进行尝试.

You can try it by putting the below code in your Server meteor code.

 Meteor.setInterval(function(){
        var output = {};

        var connections = Meteor.server.stream_server.open_sockets;
        _.each(connections,function(connection){
          // named subscriptions
          var subs = connection._meteorSession._namedSubs;
          for(var sub in subs){
            var mySubName = subs[sub]._name;

            if(subs[sub]._params.length>0){
              mySubName += subs[sub]._params[0];  // assume one id parameter for now
            }

            if(!output[mySubName]){
              output[mySubName] = 1;
            }else{
              output[mySubName] += 1;
            }
          }
          // there are also these 'universal subscriptions'
          //not sure what these are, i count none in my tests
          var usubs = connection._meteorSession._universalSubs;


        });

        console.log(output);

      },2000);

这将每2秒向控制台输出每个唯一"发布的订阅数. 唯一"出版物看起来像"Sessions1234",其中"Sessions"是集合的名称,而"1234"是订户传递的ID.您可以根据需要构造此结构,我只是将每个唯一"的出版物名称都制成了字符串.

This will output to the console the number of subscriptions per 'unique' publication every 2 seconds. A 'unique' publication would look like "Sessions1234", where "Sessions" is the name of the collection and "1234" was the id passed in by the subscriber. You could structure this however you want, I just made each 'unique' publication name into a string.

FWIW这是流星垫.但是,流星垫每个应用程序会话运行一台单独的服务器,因此,如果打开更多选项卡,您将不会看到多个订阅者.但是您至少可以了解它是如何工作的: http://meteorpad.com/pad/tYr4SE73QJA8ciw6p/Count%20Subscribers

FWIW here's a meteor pad. But meteor pad runs a separate server per app session, so you're not going to see multiple subscribers if you open up more tabs. But you can at least get an idea of how it's working : http://meteorpad.com/pad/tYr4SE73QJA8ciw6p/Count%20Subscribers

我认为,如果您只想跟踪一份出版物,则您的想法很好.如果您有大量的收藏夹,并且想要对关联的客户端和事物进行一些分析,那么上述方法可能会很有用.由于它不是官方的流星API的一部分,因此很可能在流星版本更改上容易出错.该示例仅使用setInterval来说明输出,该输出仅在需要时运行.

I think if you just want to track that one publication your idea is pretty good. If you had tons of collections and wanted some analytics about connected clients and things, maybe the above approach is useful. since it's not part of the official meteor API probably prone to breaking on meteor version changes. The example uses setInterval just for illustrating the output easier, this should only be run when its needed if used.

这篇关于如何计算流星出版物的当前订阅数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆