本文介绍了带存储的Pubnub聊天应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个与Pubnub一起开发的聊天应用程序,我想确保所有发送的聊天消息都存储在数据库中,并且还希望在聊天中发送消息.

I'm looking to develop a chat application with Pubnub where I want to make sure all the chat messages that are send is been stored in the database and also want to send messages in chat.

我发现我可以使用pubnub解析器提供存储选项,但是我不确定如何设置这两种方式,以便将聊天中发送的消息和图像存储在数据库中.

I found out that I can use the Parse with pubnub to provide storage options, But I'm not sure how to setup those two in a way where the messages and images send in the chat are been stored in the database.

有人用pubnub和parse做过此事吗?可以使用pubnub代替解析来使用其他简单的选项吗?

Anyone have done this before with pubnub and parse? Are there any other easy options available to use with pubnub instead of using parse?

推荐答案

Sutha,

除非您要谈论的最终用户数量有限,否则您寻求的解决方案不是简单的解决方案.所以我不会说没有简单"的解决方案,但是有解决方案.

What you are seeking is not a trivial solution unless you are talking about a limited number of end users. So I wouldn't say there are no "easy" solutions, but there are solutions.

原因是您的服务器需要侦听(订阅)每个活动的聊天通道,并将正在发送的消息存储到数据库中.想象一下,您的应用程序可以扩展到一百万个用户(甚至不需要增加用户数,但是这个数字应该可以帮助您认识到,当几个服务器实例以非重叠方式或重叠方式监听频道时,如何灵活地进行扩展)但使用服务器队列实现并删除重复数据.)

The reason is your server would need to listen (subscribe) to every chat channel that is active and store the messages being sent into your database. Imagine your app scaling to 1 million users (doesn't even need to get that big, but that number should help you realize how this can get tricky to scale where several server instances are listening to channels in a non-overlapping manner or with overlap but using a server queue implementation and de-duping messages).

也就是说,是的,有一些PubNub客户已经实现了这样的解决方案-顺便说一下,解析并不是实现这一目标的关键.

That said, yes, there are PubNub customers that have implemented such a solution - Parse not being the key to making this happen, by the way.

您可以通过以下三个基本选项来实现此目的:

You have three basic options for implementing this:

  1. 实施一种解决方案,该方案将允许您的服务器的许多实例在所有通道变为活动状态时订阅所有通道,并在消息进入时存储消息.要实现此目的,有很多详细信息,因此如果您没有达到这个目标,那么这不太可能是您想要去的地方.

  1. Implement a solution that will allow many instances of your server to subscribe to all of the channels as they become active and store the messages as they come in. There are a lot of details to making this happen so if you are not up to this then this is not likely where you want to go.

有一种方法可以监视通过PubNub Presence Webhooks变为活动或不活动的所有频道(启用在线状态.您将使用它来保留服务器用于提取历史记录的所有通道的列表(启用您的键上的Storage& Play .

There is a way to monitor all channels that become active or inactive with PubNub Presence webhooks (enable Presence on your keys). You would use this to keep a list of all channels that your server would use to pull history (enable Storage & Playback on your keys) from in an on-demand (not completely realtime) fashion.

对于每个活动或不活动的频道,您的服务器将通过REST调用(以及您在服务器上实现的端点-在这种情况下为Parse服务器)接收这些事件:

For every channel that goes active or inactive, your server will receive these events via the REST call (and endpoint that you implement on your server - your Parse server in this case):

  • 活动频道:在您的Parse数据库中记录开始聊天"时间令牌
  • 频道无效:在您的Parse数据库中记录结束聊天"时间令牌
  • 无效事件是该过程的开始,该过程使用您为该通道记录的开始/结束时间令牌从PubNub获取通道的历史记录:pubnub.history({channel: channelName, start:startTT, end:endTT})
  • 您将需要对此历史记录进行重复,直到您收到< 100条消息(100条是一次可以检索的最大消息数)
  • 检索这些消息时,会将它们保存到Parse数据库中
  • channel active: record "start chat" timetoken in your Parse db
  • channel inactive: record "end chat" timetoken in your Parse db
  • the inactive event is the kickoff for a process that uses start/end timetokens that you recorded for that channel to get history from for channel from PubNub: pubnub.history({channel: channelName, start:startTT, end:endTT})
  • you will need to iterate on this history call until you receive < 100 messages (100 is the max number of messages you can retrieve at a time)
  • as you retrieve these messages you will save them to your Parse db

已添加新的状态Webhooks:我们现在有了所有出席事件的网络鸣叫:加入,离开,超时,状态更改.

  1. 最后,您可以在每次pubnub.publish调用成功后将每条消息保存到Parse db.我不是Parse专家,几乎不了解其所有功能,但我相信他们具有某种排序或存储本地功能,然后同步到cloud db选项(例如,作为产品时的StackMob),但是即使没有,您也可以将msg保存到Parse直接云数据库.

在您的JavaScript客户端(在浏览器上),代码看起来像这样(不完整,可能的错误,找出原因或要求PubNub支持以获取详细信息).

The code would look something like this (not complete, likely errors, figure it out or ask PubNub support for details) in your JavaScript client (on the browser).

var pubnub = PUBNUB({
    publish_key   : your_pub_key,
    subscribe_key : your_sub_key
});

var msg = ... // get the message form your UI text box or whatever

pubnub.publish({
    // this is some variable you set up when you enter a chat room
    channel: chat_channel, 
    message: msg
    callback: function(event){
        // DISCLAIMER: code pulled from [Parse example][4] 
        // but there are some object creation details
        // left out here and msg object is not
        // fully fleshed out in this sample code

        var ChatMessage = Parse.Object.extend("ChatMessage");
        var chatMsg = new ChatMessage();
        chatMsg.set("message", msg);
        chatMsg.set("user", uuid);
        chatMsg.set("channel", chat_channel);
        chatMsg.set("timetoken", event[2]);
        // this ChatMessage object can be 
        // whatever you want it to be
        chatMsg.save();
    }
    error: function (error) {
        // Handle error here, like retry until success, for example
        console.log(JSON.stringify(error));
    }

});

您甚至可以根据时间间隔,发布数量或总数据大小来存储整个发布集(在对话的两端),但是要小心,因为任何一个用户都可以退出聊天和浏览器,而不会发出通知,并且您将无法保存.因此,如果有点吵,每次发布​​保存可能是最佳做法.

You might even just store the entire set of publishes (on both ends of the conversation) based on time interval, number of publishes or size of total data but be careful because either user could exit the chat and the browser without notice and you will fail to save. So the per publish save is probably best practice if a bit noisy.

我希望您能找到其中一种技巧,作为正确方向上的入门方法.遗漏了一些细节,所以我希望您会遇到后续问题.

I hope you find one of these techniques as a means to get started in the right direction. There are details left out so I expect you will have follow up questions.

其他一些链接可能会有所帮助:

Just some other links that might be helpful:

  • http://blog.parse.com/learn/building-a-killer-webrtc-video-chat-app-using-pubnub-parse/
  • http://www.pubnub.com/blog/realtime-collaboration-sync-parse-api-pubnub/
  • https://www.pubnub.com/knowledge-base/discussion/293/how-do-i-publish-a-message-from-parse

我们也有一个 PubNub Parse SDK . :)

And we have a PubNub Parse SDK, too. :)

这篇关于带存储的Pubnub聊天应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 19:31