{{#if currentUser}} is not logged in -> insert to database

Hello!

I’m trying to create a list that displays currently logged in users. But im struggling on the “when a user signs out / exists the page” part.

Can i insert code somehow like this?

{{#if currentUser}}
    UsersList.insert({
        user: Meteor.user().username,
        timeJoined: Session.get('time')
    })
{{else}}
    UsersList.delete({
        //something something
    })
{{/if}}

EDIT:

Template.online.helpers({
    'isOnline': function(){
        if(Meteor.user().username){
            alert('signed in');
        } else {
            alert('not signed in');
        }
        if(!Meteor.user().username){
            alert('not signed in');
        }
    }   
});

“Signed in” pops up. “Not signed in” never happens though.

Any suggestion?

Have you tried using this package?

Keeps track of user connection data, such as IP addresses, user agents, and client-side activity, and tracks this information in Meteor.users as well as some other objects. This allows you to easily see users that are online, for applications such as rendering the users box below showing online users in green and idle users in orange.

Also bare in mind that you cannot execute raw js-code in your blaze templates.

I think you are looking for something like this:

Tracker.autorun(function () {
    if (Meteor.userId()) {
        alert('signed in');
    } else {
        alert('signed out');
    }
});

That seems promising, i simply want to print out a list of online users by username (accounts-password package)

However, i cant seem to get it to work. I dont think im doing it right. Can you explain?

That didnt seem to work at all, just giving an error. 'Too many “)” '. But i cant find any overflowing “)”

Sorry, I haven’t slept. I forgot the function (), edited it.

Ah cool, works now! The “meteor-user-status” package seems to be able to solve my problem more easily tough. Do you know how it works?

Yep, the package is much more robust. The Readme on github is pretty comprehensive and walks you through setting it up, so just have a read of it.

Yes i’ve read it and tried the examples but i cant seem to get it to work anyway. What am i supposed to do in the template? in the view?

So from the Readme, if you have:

Template.foo.usersOnline = function() {
  return Meteor.users.find({ "status.online": true });
};

You should be able to call usersOnline in your template like this:

<template name="foo">
    <ul>
        {{#each usersOnline}}
            <li>
                {{username}}
            </li>
        {{/each}}
    </ul>
</template>

Thanks but it doesnt work for me, nothing simply comes up. the foo list remains empty.

Should i put the Template.foo.usersOnline in the server? When i do, an error comes up regarding autopublish. Do i need to remove that?

Sorry for not understanding, im new to meteor

If you can upload your project to github, I’ll try to help you.

Might want to take a look at this: https://github.com/dburles/meteor-presence