Unread count in meteor chat application for each room

i am working on a chat app which have various channels/rooms, want to display the unread messages for each room/channel, iam able to display the unread messages count on the active room of all the participants but not on all the rooms. The helper function is not executed although the new messages are there in the inactive rooms, any idea? thanks

        var currentuserid=Meteor.userId();
        console.log("channelid and userid"+this._id + "  " + currentuserid);
        var res=channelViewLog.findOne({channelId:this._id,userId:currentuserid})
            var varlastviewed=new Date(res.lastViwed);
            //console.log("last viewed timestamp"+varlastviewed + "  " + this._id +  " " + new Date());
            var res1=Messages.find({channel:this._id,createdAt: {$gte:varlastviewed},user:{ $ne: currentuserid}}).count();
            return res1;

Meteor.publish('PubChannelViewLog', function () {
    var currentuserid=this.userId;
    return channelViewLog.find({userId:currentuserid});

Just my quick two cents:

In an app I’m currently working on, we needed to display the same thing – unread counts by room. I started with your approach but ultimately switched once we realized we were going to be throttling the pub/sub, meaning our counts may not be correct on a find/count.

I ended up with a collection of message counts by userid/roomid that reset on entry and incremented on message insertions, rather than querying by last visit/message time.

To your code:

var varlastviewed=new Date(res.lastViwed);

should be

var varlastviewed=new Date(res.lastViewed);

Are you hitting any errors, or just no display?


thanks @vigorwebsolutions, no errors but does not display the unread count, however the room that are currently active and selected by the users display the unread count, this is exactly the opposite i need. it does not makes sense when the user is in a room and it shows unread count, when ever there are new messages in that room. let me try your approach. thanks a lot.

@vigorwebsolutions, i have done as per your suggestion, its simply works. not sure whether its an elegant way of doing, but it works for now. thanks a ton.

No worries, glad it helped!

can u send ur code i want to see ur code as i am trying to do the same

how u r getting lastviewed

Hey sorry, that was an old project and I don’t have access to the code any more. But the basic idea was:

  1. Create a roomMessageCount collection. There should be a document for every user on every channel. The fields look like:
  • roomId: String
  • recipientId: String
  • currentlyReading: Boolean
  • messageCount: Int
  1. When a user enters a room, set the currentlyReading property to true and reset the messageCount to 0
  2. When a new message is sent, increment the messageCount property for every doc that matches the roomId, that doesn’t have currentlyReading set to true
  3. You should be able to subscribe to that data by recipientId to get the messageCount for each room