How to use Pusher with Django? - django

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 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.
Read more about channels at the API guide for channels.
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.
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);
});
Once that is clear, lets move to django.
The django-pusherable module just makes it easy to create channels by decorating your views.
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).
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.
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.
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.
How can I use it if I have 2 classes in my database like say,one for
question and one for options, after creating one question it should
appear in the feeds of its followers and along with options, Do I have
to push the options also? How to do this?
Pusher does not care what your database classes are, or what your database relationships are. You have to figure this out yourself.
Pusher's job is limited to making the "live update" happen on the browser without the user having to refresh the page.
Plus how to create relationships, i.e when an user follows another how
to subscribe to it and show related feeds?
I think you don't quite understand what is Pusher's role in all this.
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.
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:
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.
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:
Go to the view that is executed when a user posts new content.
Update that view as described above, inheriting from PusherUpdateMixin
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.
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.

Related

How to make notifications from one app to another app in django?

I am making one e-commerce website and i am just trying to make notifications from admin side to user side both are diffrent app in django...
My problems is how to make notifications in admin side (one app)when i click the button send the messages and display the notifications or message in userside(other app)
If I understood your question correctly, you want to notify all the users ( for example for a new offer) when they login to their accounts.
For this matter, you must create a model for the notification (and if it's user-specified there is need for a foreign key to the user in the notification model or maybe you want to show the notification to all users by date this varies duo to your application) and in the profile view just make a query on notifications and show them to the user.
but if you want a chat room like notification (show notification to online users only) just use Django channels.

create facebook like notification system in django

Say user a liked user b. I want to show this as a notification in the notification panel of user b when she logs in. She can also view her previous notifications (basically like facebook notification). This is not email notification or a success/warning type one time notification.
I want to know the right track to follow. Do I use django-notifications or django-messages-framework or push notifications? Where is the use of channels and web sockets in all of these?
My steps:
1) create a notification app
2) create a notification model (what fields do I put here if since django-notification already has actor, verb, action object and target fields?)
3) Do i have to create forms.py? How is the notification message getting passed?
4) What view do I put in views.py? (please give an example code)
If there is any example project that implemented the notification system please provide a link.
If you're using django-notifications you can just create an object with the actor as user_a, verb as 'like' or any other action performed, recipient would be user_b
So something like
notify.send(request.user, verb='liked', recipient=[target_user])
When user_b logs in you can fetch all the notification (or maybe just the unread ones) and display it
user.notifications.active()
user.notifications.unread()

How to add another option to the share functionality of Google Glass?

I am interested to add my service into the share functionality of the Google Glass, my flow is below:
1. Take photo / Record video
2. Share with -> My service
3. the photo or video should be uploaded to my site
Is this functionality possible? it is very similar to Facebook and G+ share options.
I will be happy to know how to do it, Thanks.
What you are looking for is what the Mirror API calls a Contact. Your Glassware can setup one or more Contacts, specifying what content type can be shared with you and/or if there are voice commands which will trigger the Contact.
You will also need to setup a Subscription which will be the public URL for an HTTPS enabled server that the Mirror API will use to send you the content that was shared with the Contact.
In general, the flow when a user first authorizes you to write to their time would be something like this:
Add a Subscription, so you can get callbacks.
Add one or more Contacts. In your example, you would want to register the Contacts to have acceptTypes of image/* and video/*, although you can also omit the acceptTypes to get everything (including text).
The callback you register with the subscription should be able to handle a JSON body, and should return HTTP code 200 as quickly as possible. A good procedure is to actually accept the body, place it on a job queue for processing later, and immediately return the 200 code. When processing the body, you may want to do something like
Confirm the userToken and verifyToken provided are valid.
Using the itemId, get the Timeline item, which will include attachment information about what was shared with you.
If the attachment is marked as isProcessingContent, then the content isn't ready for you and you should return the job to the queue and try again soon.
If isProcessingContent is false, you can use the attachment URL with the authentication token for the user to fetch the content itself.
There are a lot of details I've glossed over here. For a further overview of the flow, see https://developers.google.com/glass/develop/mirror/contacts

Randomly adding messages to the Django messages framework

In a Django project I have a space in the templates to show the messages coming from the Django messages framework. I would like to use that framework to show some messages that are not triggered by user actions in some views but instead are set by an app. Ideally my app will decide when it is best to send the messages but initially it will be random. An example to clarify a bit; I don't want to send the typical message related to an action like 'you have successfully updated your profile'. Instead I would like to recommend some content from times to times, something like 'you might be interested by spam'. The content ('spam' in this case) is provided by some objects in the database that another app gathers already.
I was wondering how I can set a message outside of the typical view. I was thinking about implementing a middleware that would be called on every request and setting the message in the process-request() class if this particular user has been marked to receive any.
Is it the even a good idea to begin with?

Facebook like notification updates using django signal or notification

How can i use django-notifications or django-signals to make something like facebook updates notification that shows in the user profile if any other user likes or posts comments on user's blog or posts?
For the activity feed, we use https://github.com/justquick/django-activity-stream Documentation: http://justquick.github.com/django-activity-stream/
For the js widget and live notifications, we use https://github.com/subsume/django-subscription yourlabs example, it depends on redis but you can easily add a model backend if you really want to. Redis is a good choices it's half a megabyte of dependency. Documentation: http://django-social.rtfd.org
There is no application that does meta-notifications ("notification groupping") properly but a lot of research has been done. Basically you need another app, with a MetaNotification model, and something (management command, signal ...) that will visit notifications and create MetaNotification instances. Then you should display MetaNotification lists rather than Activity or notification list.
Finnaly, if you want configurable email notifications then you can use django-notifications: https://github.com/jtauber/django-notification or this app which looks nicer: http://www.tomaz.me/django-notifications/
I'm not aware of any app that does it all. It is going to be some work for you.
"It's a long way to the top if you wanna rock'n'roll" or as I like to say "patience and perseverance" :)
https://pypi.python.org/pypi/feedly allows you to build newsfeed and notification systems using Cassandra and/or Redis. Examples of what you can build are applications like the Facebook newsfeed, your Twitter stream or your Pinterest following page.