Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another usage example #14

Open
snshn opened this issue Oct 25, 2013 · 0 comments
Open

Another usage example #14

snshn opened this issue Oct 25, 2013 · 0 comments

Comments

@snshn
Copy link

snshn commented Oct 25, 2013

I'm counting the number of users via publish/subscribe method, sending only the total amount of online users.

common.js:

Server = new Meteor.Collection('server'); // Global temporary values

client.js:

Meteor.subscribe('server');

...
    counter: function()
    {
        if( Meteor.status().status == 'connecting' )
            return 'connecting...';
        else if( Meteor.status().status != 'connected' ) // offline or connecting
            return 'offline';

        var total_users = Server.findOne('online') ? Server.findOne('online').val : 1;
        return total_users+' user'+((total_users == 1) ? '' : 's')+' online';
    },
...

server.js:

Meteor.publish('server', function(){
    return Server.find();
});

Meteor.startup(function(){
    Meteor.presences.find().observeChanges({
        added: function(){
            Server.upsert({_id: 'online'}, {_id: 'online', val: Meteor.presences.find().fetch().length || 1});
        },
        removed: function(){
            Server.upsert({_id: 'online'}, {_id: 'online', val: Meteor.presences.find().fetch().length || 1});
        }
    });

});

I had to create a separate collection to make things easier, but there may be a way which would not involve publish/subscribe. I will need that collection to publish more server values later, so probably will stick to that.

I wish there was a way to subscribe to the .length of the Meteor.presences collection, would've made things much more simple.
Great plugin, thanks @tmeasday Ü

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant