问题描述
寻找使用PubNub发送实时更新到用户的网页浏览器。
Looking in to using PubNub to send real-time updates to the user's web browser.
我查看了他们的网站和资料。看起来他们有几个不同的选择。
I looked over their website and materials. It looks like they have a few different options.
我们希望用它来发送实时更新到用户正在查看的网页。这些信息很简单,比如你刚刚收到一条消息。我们不是试图实施一个聊天程序或类似的东西。
We would like to use it for sending real time updates to a web page that a user is looking at. The information is simple stuff like "You just received a message". We are not trying ti implement a chat program or anything like that.
PubNub是一个很好的解决方案吗?如果是这样,应该使用哪个版本的服务?
Is PubNub a good solution for this? If so, which version of the service should be be using?
我们在Heroku服务器上运行Django。
We are running Django on a Heroku server.
非常感谢!
推荐答案
PubNub Facebook通知
是类似Facebook的
Window Box的一个示例,它通过PubNub通知用户自定义消息。
您可以通过移动电话或浏览器向用户发送更新。
这会向用户显示通知;任何通知你。
PubNub Facebook Notification
This is an example of a Facebook-likeWindow Box that notifies your user with a custom message via PubNub.You can send updates to your users on their Mobile Phone or Browser.This will show your user a notification; any notification you.
使用PubNub允许通过WebSockets,BOSH,Comet和其他机制的数据推送
在您的应用程序中使用,使您能够发送数据
任何时间通过 MASS BROADCAST 或
个人通知直接发送给您的用户。
Using PubNub allows Data Push via WebSockets, BOSH, Comet and other Mechanismsto be used in your application providing you the ability to send dataAT ANY TIME directly to your users via MASS BROADCAST orINDIVIDUAL NOTIFICATIONS.
下载源代码:
Download Source Code: https://github.com/pubnub/javascript/tree/master/examples/facebook-notification
从这里开始,轻松复制/粘贴代码。
开始非常简单,我们建议您在开始之前使用上面的示例链接启动
。
Begin here for easy copy/paste of code.It is very easy to get started and we recommend you startwith the example link above before you begin.
首先包含 FBootstrap 资源,以便提供通知窗口的
外观。
将这些样式添加到HTML文件中。
First include the FBootstrap resources in order to provide thelook and feel of the notification window.Add these Styles to your HTML file.
<link href=bootstrap.css rel=stylesheet>
<style type=text/css> body { padding-top: 60px; } </style>
数据连接代码
设置一个PubNub数据连接,然后
添加规则来处理收到的数据。
Data Connection Code
Next you need to setup a PubNub Data Connection and thenadd rules for what to do with the data once it is received.
<script src="https://pubnub.s3.amazonaws.com/pubnub-3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-modal/2.2.6/js/bootstrap-modal.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>(function(){
// PubNub (For Data Push to User)
var pubnub = PUBNUB.init({
subscribe_key : 'demo',
ssl : false
});
// Setup New Data Push Connectoin via PubNub
pubnub.subscribe({
restore : true,
channel : 'example-user-id-1234',
callback : show_notification
});
// Setup Alert Window
$('#new-alert').modal({ keyboard : true });
// Show the Notification Window
function show_notification(message) {
$('#new-alert').modal('show');
}
// Simulate Notification
$('#simulate-notification').bind( 'mousedown', function() {
pubnub.publish({
channel : 'example-user-id-1234',
message : 'alert'
});
return false;
} );
})();</script>
Python推送示例
希望将 python
代码
添加到 Django 或任何其他框架中。
您可以将此添加到您的应用中的消息发布
代码中。
这将通知您的用户。
这个具体的例子会导致通知在Facebook的通知页面出现
。
Python Push Example
Next you will want to add this python
codeto your Django or any other framework.You can add this to the message post
code in your app.This will post a notification to your user.This specific example will cause a notification to appearinside the Facebook Notification page.
pip install pubnub
Python来源
Python Source
## PubNub Setup
import pubnub from Pubnub
pubnub = Pubnub( 'demo', 'demo', None, False )
## Push Notice to 'example-user-id-1234'
info = pubnub.publish({
'channel' : 'example-user-id-1234',
'message' : { 'your-data' : 'any-data-here' }
})
print(info)
这篇关于希望使用PubNub将实时更新发送到用户的Web浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!