How to use Pusher with Django?

I am trying to create an application using pusher and django. I looked through some links, such as https://github.com/pusher/django-pusherable , but it had no example and therefore was hard to understand! Can anyone help here? And also, what are the channels here and how to create the next system with channels (activity flows)? Thanks!

+5
source share
1 answer

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

There are several channels in this template. Each channel is similar to the frequency of a radio station. The publisher places messages in the channel, and all subscribers (listeners) who listen to this channel will receive the message.

The publisher does not know how many people are listening to a particular channel, it simply sends a message. Subscribers can listen to the channels of interest to them.

In practical terms, a channel usually contains an event type; therefore, subscribers can decide what to do with the data depending on the type of event. Sometimes it is also called a message class.

For example, inventory updates can be a channel. The publisher (your backend script) will push the message to this channel whenever a stock change occurs; all and all clients listening to this channel will receive this message.

Learn more about channels in the channel API guide.

Pusher takes care of channel management and provides tools for recording listeners.

In your example, each user will have their own activity feed channel. Followers (they can be users) can subscribe to the listening channel of the user they are interested in.

Your system simply publishes updates for all channels.

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

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

From the side of the consumer (client):

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

Once this becomes clear, go to django.

The django-pusherable simply simplifies the creation of channels by decorating your views.

Each decorated image will automatically have a channel created for the object that is accessed in the view. Each object gets its own channel called 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 applies to the backend (pushing messages).

On the front part (client, reading messages) there are several template tags. These tags simply import the necessary javascript and help you subscribe to the right events.

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

Now suppose you want to know when a book with ID 1 is updated and the page automatically refreshes, in your templates you should write the following. obj - object for the 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 can call this view using a specific book:

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

Now open this view in a new browser tab.

In another browser tab or in the django shell - update the book with identifier 1, and you will notice that the javascript console will automatically register your changes.


How can I use it if I have 2 classes in my database, for example, say, one for a question and one for options, after creating one question it should appear in the feed of its followers and along with the options, do I also have click on options? How to do it?

Pusher doesn't care what your database classes are or what your relationship with the database is. You must understand this yourself.

Pusher's task is limited to the fact that "real-time updating" occurs in the browser without the need to refresh the page.

Plus, how to create relationships, that is, when a user follows a different way, how to subscribe to it and show related channels?

I think you do not quite understand what is the role of Pusher in all of this.

Pusher does not care about your database and does not know about your relationships in the database, which object relates to who and who is following whom.

All that pusher does is make one page in the browser automatically refresh without the need for updating.

The logic to “follow” another user must already be created in your application. That is, you should have a view that allows the user to follow someone else. As soon as they follow someone, a record will be created / updated in the database. This action will cause Pusher to post a message for this database object. Now the one who listens to this channel will receive this message, and then will be able to do whatever he wants with it.

Here is the order of events / developments:

  • First create the application as usual. It should have all the features you expect. If it is a social network, people should be able to follow others and update their profile page to see any updates from their followers.

  • The system should already “know” what the update is and what content is stored for each object. Thus, if you create “users” and “followers”, forms, screens, logic, database tables, etc. should already be formed, in order to make sure that the content can be added, updated by the correct users.

  • As soon as everything is right and works as you like, now you bring Pusher; and then you decide which "event" you want to automatically update in the browser.

Assume that the event "whenever a user adds new content to the site, all his followers must be notified." Therefore, you should do the following:

  • Go to the view that is executed when the user submits new content.
  • Refresh this view as described above, inheriting from PusherUpdateMixin
  • Go to the template that appears to users, where all of their followers are shown. In this template code, add the tags described above to enable the javascript api.

  • Further, in the same template, you will have a code listing all users of this user, in this logical code you can add a div that will be updated “automatically” whenever the user downloads the update.

+7
source

All Articles