7

Hey i'm using pubnub Services to add chat functionality to my Titanium App but i'm wondering if there is a way to get the number of unread messages.
there is no info about this on api references

What i tried to save the numbers of messages in history then reloading new history and calculate the difference but it's so stupid and complex solution any body know how to achieve this ? Thanks

Stephen Blum
  • 5,522
  • 2
  • 24
  • 44
Antwan
  • 3,679
  • 7
  • 38
  • 61

1 Answers1

8

Track Read/Unread Messages on PubNub

Years back we promised we'd make it super easy method for tracking Unread Message Counts in your app. Now it is finally possible! Using PubNub Functions, you can add permanent state objects and values into your multi-device applications. You'll use our atomic methods available in PubNub Functions Key/Value Storage engine. PubNub Functions Storage Engine ( kvstore ) is replicated to every data center, making it fast and reliable to store/retrieve data.

You'll only need to create one function to increment, decrement and retrieve the current count of your unread messages.

Count Unread Messages

Our Function will count messages sent to a channel and save the value using an atomic increment and decrement methods using a key named after the channel and user ID. This is a practical design pattern called "conventional namespacing". It means that you'll use hints and pieces of information found in the message to create a name that is constructible based on the information in the message. We will be using the channel name in this example for our conventional name-spacing.

This first function will increment a value to track the unread messages in a user's inbox.

Increment and Decrement the Unread Message Counter

Channel: room.*

Event: On-Before Publish

// Access to Distributed Database
const db = require('kvstore');
export default (request) => { 
    // Conventionally build the Key ID based on the request parameters and channel name.
    let counterId = request.channels[0] + '/' + request.params.uuid;
    
    // Increment or Decrement the unread message counter
    let method = request.message.read ? -1 : 1;
    
    // Increment/Decrement and read the unread message counter
    return db.incrCounter( counterId,  method ).then(()=>{
        return db.getCounter(counterId).then((counter) => {
            request.message.unread = counter || 0;
            return request.ok();
        });
    });
}

Now any JSON Message published to this channel hierarchy room.* will track unread messages by incrementing a counter. The counter can be decremented by including a read flag. This read method may also be used as a read-receipt tracking system if desired. You can learn more about read-receipts in the article by Adam Bavosa: Read Receipts Pattern for Realtime Chat Apps.

Using the Counter

Publish a message to room.john-smith like this:

{ "message" : "Hello John!" }

John will receive your message and a unread message counter is added to the message. The message will have been augmented with a unread variable. Now the message looks like this:

{ "message" : "Hello John!", "unread" : 1 }

John can respond with a read-receipt reply by publishing:

{ "read" : true }

The message will update the read receipt with a unread counter.

{ "read" : true, "unread" : 0 }

That's it! You did it. Patterns will change based on group and one-to-one messaging apps.

Community
  • 1
  • 1
Stephen Blum
  • 5,522
  • 2
  • 24
  • 44
  • Thanks for your response but i didn't understand you correctly ,do you mean tracking the history time token and check if there is new messages and i think that will help be if there are new messages but i couldn't get there number ,and i didn't get your second solution sorry :( – Antwan May 30 '15 at 17:44
  • The only way to get the number is by doing a Full-fetch. Also we are adding a future service which will provide this for you automatically. However it will be a few months before it is available. – Stephen Blum Jun 02 '15 at 03:53
  • How can I do the full-fetch in android? any resources? – amateur programmer Nov 14 '15 at 00:53
  • Full-fetch Android Example: https://www.pubnub.com/docs/android-java/api-reference#history_example_4 - you can fetch all messages by using the Paging Example. – Stephen Blum Nov 16 '15 at 23:58
  • Hi you have a good question. We have built it and it will be available in Q2! – Stephen Blum Jan 19 '16 at 17:29
  • Just added this to the list! We are building it now. – Stephen Blum May 19 '16 at 00:12
  • whats the state here? – unreal Jul 25 '16 at 07:53
  • we have built a BLOCK for this! – Stephen Blum Jul 25 '16 at 16:18
  • @pubnub could you please paste a link to this BLOCK? I cannot find it :S – Alex R. R. Sep 16 '16 at 16:38
  • Goooooood request. We haven't posted it yet. – Stephen Blum Sep 22 '16 at 00:45
  • @PubNub NOW are you providing any proper way for read / unread message? or the mechanism above you mentioned is still. actually we are waiting for basic features of chat app from 2016 Feb., OUR major requirements are for App badge count and show unread message count in user chat list. – Chetan Prajapati Jan 17 '17 at 11:07
  • We are almost done. only things left are maintain App badge count and show message read / unread status. Waiting for your team update..... – Chetan Prajapati Jan 17 '17 at 11:09
  • What do you need from PubNub? We do not provide detailed features for chat in our SDKs because our SDKs are not use case specific. We many many many chat use case customers that have implemented these features. Have you engaged with a PubNub Solution Architect? It's a free service for customers. Please submit your request to [PubNub Support](https://support.pubnub.com) – Craig Conover Jan 17 '17 at 16:06
  • UPDATE! We've finally made this feature available to you. The latest answer update shows you one way how to achieve an unread message counter using PubNub. See our updated answer above. – Stephen Blum Mar 27 '18 at 21:54