Dibyajyoti Mondal
Dibyajyoti Mondal

Reputation: 86

How to get the number of individual unread messages of PubNub?

I'm using pubnub Services to add chat functionality to my App but i'm wondering if there is a way to get the individual number of unread messages. i'm using this link -> https://www.pubnub.com/docs/swift/data-streams-publish-and-subscribe

Upvotes: 2

Views: 770

Answers (1)

Adam
Adam

Reputation: 641

This can be accomplished using PubNub Functions. Functions are your own scripts that run automatically in the cloud when a message is published on one or many PubNub Channels.

It has a key-value store with increment, decrement, and retrieve functionality. This can be used very intuitively with an unread message pattern on PubNub.

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();
        });
    });
}

Following this official guide you can integrate this new functionality into an existing app.

Upvotes: 3

Related Questions