2015-04-03 51 views
2

我正在使用包mizzao:用戶狀態爲我的用戶添加在線/活動狀態。使用這個,我可以運行查詢來獲取所有在線用戶。如何創建在線/活動用戶列表?

我面對的是,保持用戶的列表最新與登錄的用戶的問題。

我相信我會需要使用Accounts.onLogin更新列表,我認爲這是用在服務器端。那麼,我將如何保持用戶列表與客戶端連接的用戶保持最新?

回答

6

就像README上的mizzao點。

第一個進行發佈。

Meteor.publish("userStatus", function() { 
    return Meteor.users.find({ "status.online": true }); 
}); 

subscribe

Meteor.subscribe('userStatus') 

做幫手網上回報用戶。

的Javascript

Template.example.helpers({ 
    usersOnline:function(){ 
    return Meteor.users.find({ "status.online": true }) 
    }, 
    usersOnlineCount:function(){ 
    //event a count of users online too. 
    return Meteor.users.find({ "status.online": true }).count(); 
    } 
}) 

HTML

<template name="example"> 
There are currently {{usersOnlineCount}} users online. 
<h1>List of Users online </h1> 
    <ul> 
    {{#each usersOnline}} 
    <li> {{username}}</li> 
    {{/each}} 
    </ul> 
</template>