本文介绍了如何在 Django 中使用 Pusher?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 pusher 和 django 构建一个应用程序.我浏览了一些链接,例如 https://github.com/pusher/django-pusherable,但它缺少一个例子,因此很难理解!有人可以在这里帮忙吗?还有这里的频道是什么,因此如何创建一个带有提要(活动流)的关注系统?谢谢!

I am trying to build an app using pusher and django. I went through few of the links like https://github.com/pusher/django-pusherable, but it lacked an example and thus was difficult to understand! Can anyone please help in here?And also what are channels in here and thus how to create a follow-following system with feeds(activity streams)?Thanks!

推荐答案

Pusher 允许您轻松实现 发布/订阅模式用于消息传递(也简称为发布/订阅).

Pusher allows you to easily implement a publish/subscribe pattern for messaging (also called pub/sub for short).

在这种模式中,有许多渠道.每个频道就像一个电台的频率.发布者将消息放在一个频道上,任何正在收听该频道的订阅者(听众)都会收到该消息.

In this pattern, there are a number of channels. Each channel is like a radio station's frequency. A publisher puts messages on a channel, and any subscribers (listeners) that are listening to that channel will receive the message.

发布者不知道有多少人在收听特定频道,它只是发送消息.收听他们感兴趣的频道取决于订阅者.

The publisher does not know how many people are listening to a particular channel, it just sends the message. It is up to the subscribers to listen to the channels that they are interested in.

实际上,一个频道通常包含一个事件类型;因此订阅者可以根据事件类型决定如何处理数据.这有时也称为消息类.

Practically speaking, a channel is usually contains an event type; so subscribers can decide what to do with the data depending on the event type. This is sometimes also called a message class.

例如,股票更新可以是一个频道.每当库存发生变化时,发布者(您的后端脚本)都会向此频道推送消息;任何和所有在此频道上收听的客户端都会收到该消息.

For example, stock updates can be a channel. The publisher (your backend script) will push a message to this channel whenever there is a change in stock; any and all clients listening on this channel will get that message.

渠道 API 指南中阅读有关渠道的更多信息.

Read more about channels at the API guide for channels.

Pusher 负责管理频道并为您提供编写侦听器的工具.

Pusher takes care of managing the channels and giving you the tools to write listeners.

在您的示例中,每个用户都有自己的活动流频道.关注者(这些可以是用户)可以订阅收听他们感兴趣的用户的频道.

In your example each user would have their own activity stream channel. Followers (these can be users) can subscribe to listen on the channel of the user they are interested in.

您的系统只是发布所有频道的更新.

Your system simply publishes updates for all channels.

在代码中,这会像这样工作(来自 pusher docs 的示例)- 来自发布者(后端)方面:

In code, this would work like this (example from the pusher docs) - from the publisher (backend) side:

from pusher import Pusher
pusher.trigger(u'test-channel', u'my-event', {u'message': u'hello world'})

从消费者(客户端)方面:

From the consumer (client) side:

var channel = pusher.subscribe('test-channel');
channel.bind('my-event', function(data) {
  alert('An event was triggered with message: ' + data.message);
});

一旦清楚,让我们转向 django.

Once that is clear, lets move to django.

django-pusherable 模块只是通过装饰您的视图来轻松创建频道.

The django-pusherable module just makes it easy to create channels by decorating your views.

每个被修饰的视图都会自动为视图中正在访问的对象创建一个通道.每个对象都有自己的频道,名为 modelclass_pk,因此如果您的模型名为 Book,而您刚刚创建了第一本书,则该频道将称为 Book_1.

Each view that is decorated will automatically have a channel created for the object being accessed in the view. Each object gets its own channel, named modelclass_pk, so if your model is called Book, and you just created your first book, the channel will be called Book_1.

from pusherable.mixins import PusherDetailMixin, PusherUpdateMixin

class BookDetail(PusherDetailMixin, DetailView):
    model = Book

class BookUpdate(PusherUpdateMixin, UpdateView):
    model = Book

这会处理后端(推送消息).

This takes care of the backend (pushing messages).

在前端(客户端,阅读消息),为您提供了一些模板标签.这些标签只是导入必要的 javascript 并帮助您订阅正确的事件.

On the front end (client, reading messages), there are a few template tags provided for you. These tags just import the necessary javascript and help subscribe you to the correct events.

每个模型有两个默认事件,更新和查看.

There are two default events for each model, update and view.

现在,假设您想知道 id 为 1 的书何时更新,并自动更新页面,您可以在模板中编写以下内容.obj 是 book 的对象:

Now, suppose you want to know whenever the book with id 1 is updated, and automatically update the page, in your templates you would write the following. obj is the the object for book:

{% load pusherable_tags %}
{% pusherable_script %}
{% pusherable_subscribe 'update' obj %}

<script>
    function pusherable_notify(event, data) {
        console.log(data.user + "has begun to " + event + " " + data.model);
    }
</script>

在您的后端,您将使用特定书籍调用此视图:

In your backend, you would call this view with a specific book:

def book_update(request):
    obj = get_object_or_404(Book, pk=1)
    return render(request, 'update.html', {'obj': obj})

现在在新的浏览器标签中打开该视图.

Now open that view in a new browser tab.

在另一个浏览器选项卡中,或在 django shell 中 - 用 ID 1 更新本书,您会注意到 javascript 控制台会自动记录您的更改.

In another browser tab, or in the django shell - update the book with id 1, and you'll notice the javascript console will automatically log your changes.

如果我的数据库中有 2 个类,例如,一个用于问题和一个选项,在创建一个问题后,它应该出现在其关注者的提要中以及选项中,我有吗也推选项?如何做到这一点?

Pusher 不关心你的数据库类是什么,或者你的数据库关系是什么.你必须自己解决这个问题.

Pusher does not care what your database classes are, or what your database relationships are. You have to figure this out yourself.

Pusher 的工作仅限于在浏览器上进行实时更新",而无需用户刷新页面.

Pusher's job is limited to making the "live update" happen on the browser without the user having to refresh the page.

加上如何创建关系,即当用户关注另一个用户时订阅它并显示相关的提要?

我认为您不太了解 Pusher 在这一切中的作用.

I think you don't quite understand what is Pusher's role in all this.

Pusher 不关心您的数据库,也不知道您在数据库中的关系、什么对象与什么有关以及谁在关注谁.

Pusher doesn't care about your database and it has no knowledge about your relationships in the database, what object relates to what and who is following whom.

推送器所做的只是让浏览器上的一个页面自动更新,而无需用户刷新.

All pusher does is makes so that one page on the browser will automatically update without the user having to refresh.

关注"另一个用户的逻辑应该已经在您的应用程序中创建.也就是说,您必须拥有一个允许用户关注其他人的视图.一旦他们关注某人,就会在数据库中创建/更新记录.此操作将触发 Pusher 为该数据库对象发布消息.现在,在该频道上收听的任何人都会收到该消息,然后可以对其进行任何他们想做的事情.

The logic to "follow" another user should already be created in your application. That is, you must have a view that allows a user to follow someone else. Once they follow someone, a record will be created/updated in the database. This action will trigger Pusher to publish a message for that database object. Now, whoever is listening on that channel will receive that message, and then can do whatever they want with it.

这是事件/发展的顺序:

Here is the order of events/development:

  1. 首先,照常创建您的应用程序.它应该具有您期望的所有功能.如果这是一个社交网络,人们应该能够关注其他人并刷新他们的个人资料页面以查看他们关注者的任何更新.

  1. First, create your application as normal. It should have all the features that you expect. If this is a social network, people should be able to follow others and refresh their profile page to see any updates from their followers.

系统应该已经知道"什么是更新以及为每个实体存储了什么内容.因此,如果您正在创建用户"和关注者",那么应该已经有了表单、屏幕、逻辑、数据库表等,以确保内容可以由正确的用户添加、更新.

The system should already "know" what is an update and what content is stored for each entity. So, if you are creating "users" and "followers", there should already be the forms, screens, logic, database tables, etc. to make sure that content can be added, updated, by the correct users.

一旦你把所有这些都正确地准备好并按照你喜欢的方式工作,现在你就可以引入 Pusher;然后您决定要在浏览器中自动更新哪个事件".

Once you have all that in place correctly and working as you like, now you bring in Pusher; and then you decide which "event" do you want to have automatically updated in the browser.

假设事件是每当用户向网站添加新内容时,都应通知其所有关注者".因此,您将执行以下操作:

Suppose the event is "whenever a user adds new content to the site, all their followers should be notified". So you would then do the following:

  1. 转到在用户发布新内容时执行的视图.
  2. 如上所述更新视图,继承自 PusherUpdateMixin
  3. 转到为用户显示的模板,其中显示了他们的所有关注者.在此模板代码中,添加上述标签以包含 pusher javascript api.

  1. Go to the view that is executed when a user posts new content.
  2. Update that view as described above, inheriting from PusherUpdateMixin
  3. Go to the template that is shown for users where all their followers are shown. In this template code, add the tags described above to include the pusher javascript api.

接下来,在同一个模板中,您将拥有列出该用户关注的所有用户的代码,在该逻辑代码中,您可以添加一个 div,该 div 将在该用户发布帖子时自动"更新更新.

Next, in the same template, you will have code that lists all the users this user is following, in that logic code, you can then add a div which will be updated "automatically" whenever that user posts an update.

这篇关于如何在 Django 中使用 Pusher?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 01:27
查看更多