如何获取未读消息数 PubNub
How to get the number of unread messages PubNub
嘿,我正在使用 pubnub
服务向我的 Titanium 应用程序添加聊天功能,但我想知道是否有办法获取未读消息的数量。
api 参考文献
上没有关于此的信息
我试图保存历史消息的数量,然后重新加载新的历史记录并计算差异,但这是一个如此愚蠢和复杂的解决方案,任何人都知道如何实现这一点?
谢谢
在 PubNub 上跟踪 Read/Unread 消息
几年前,我们曾承诺,我们会在您的应用中提供一种超级简单的方法来跟踪未读消息计数。
现在终于可以了!
使用 PubNub Functions,
您可以将永久状态对象和值添加到您的多设备应用程序中。
您将使用 PubNub Functions Key/Value 存储引擎中可用的原子方法。
PubNub 函数存储引擎 ( kvstore ) 被复制到每个数据中心,使其对 store/retrieve 数据快速可靠。
您只需创建一个函数即可 increment
、decrement
和 retrieve
当前未读消息数。
计数未读消息
我们的函数将对发送到通道的消息进行计数,并使用以通道和用户 ID 命名的键使用原子递增和递减方法保存值。
这是一种称为“常规命名空间”的实用设计模式。
这意味着您将使用在消息中找到的提示和信息片段来创建一个可根据消息中的信息构造的名称。
我们将在此示例中使用频道名称作为常规名称间距。
第一个函数将增加一个值以跟踪用户收件箱中的未读邮件。
递增和递减未读消息计数器
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();
});
});
}
现在发布到此频道层次结构 room.*
的任何 JSON 消息都将通过递增计数器来跟踪未读消息。
可以通过包含读取标志来递减计数器。
如果需要,此阅读方法也可用作 阅读回执 跟踪系统。
您可以在 Adam Bavosa 的文章中了解有关已读回执的更多信息: Read Receipts Pattern for Realtime Chat Apps.
使用计数器
像这样向 room.john-smith
发布消息:
{ "message" : "Hello John!" }
John 将收到您的消息,并且消息中会添加一个未读消息计数器。
该消息将使用 unread
变量进行扩充。
现在消息看起来像这样:
{ "message" : "Hello John!", "unread" : 1 }
John 可以通过发布回复已读回执:
{ "read" : true }
该消息将使用 unread
计数器更新已读回执。
{ "read" : true, "unread" : 0 }
就是这样!你做到了。模式将根据群组和一对一消息传递应用程序而改变。
嘿,我正在使用 pubnub
服务向我的 Titanium 应用程序添加聊天功能,但我想知道是否有办法获取未读消息的数量。
api 参考文献
我试图保存历史消息的数量,然后重新加载新的历史记录并计算差异,但这是一个如此愚蠢和复杂的解决方案,任何人都知道如何实现这一点? 谢谢
在 PubNub 上跟踪 Read/Unread 消息
几年前,我们曾承诺,我们会在您的应用中提供一种超级简单的方法来跟踪未读消息计数。 现在终于可以了! 使用 PubNub Functions, 您可以将永久状态对象和值添加到您的多设备应用程序中。 您将使用 PubNub Functions Key/Value 存储引擎中可用的原子方法。 PubNub 函数存储引擎 ( kvstore ) 被复制到每个数据中心,使其对 store/retrieve 数据快速可靠。
您只需创建一个函数即可 increment
、decrement
和 retrieve
当前未读消息数。
计数未读消息
我们的函数将对发送到通道的消息进行计数,并使用以通道和用户 ID 命名的键使用原子递增和递减方法保存值。 这是一种称为“常规命名空间”的实用设计模式。 这意味着您将使用在消息中找到的提示和信息片段来创建一个可根据消息中的信息构造的名称。 我们将在此示例中使用频道名称作为常规名称间距。
第一个函数将增加一个值以跟踪用户收件箱中的未读邮件。
递增和递减未读消息计数器
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();
});
});
}
现在发布到此频道层次结构 room.*
的任何 JSON 消息都将通过递增计数器来跟踪未读消息。
可以通过包含读取标志来递减计数器。
如果需要,此阅读方法也可用作 阅读回执 跟踪系统。
您可以在 Adam Bavosa 的文章中了解有关已读回执的更多信息: Read Receipts Pattern for Realtime Chat Apps.
使用计数器
像这样向 room.john-smith
发布消息:
{ "message" : "Hello John!" }
John 将收到您的消息,并且消息中会添加一个未读消息计数器。
该消息将使用 unread
变量进行扩充。
现在消息看起来像这样:
{ "message" : "Hello John!", "unread" : 1 }
John 可以通过发布回复已读回执:
{ "read" : true }
该消息将使用 unread
计数器更新已读回执。
{ "read" : true, "unread" : 0 }
就是这样!你做到了。模式将根据群组和一对一消息传递应用程序而改变。