Refresh a page when something new appears in Django - django

I'm creating a website for a local establishment that uses Ifood. Ifood provides an API that allows you to check information, both regarding establishments and incoming orders.
The code is ready, but I can only show a new order update when manually refreshing the page. Which is a bit shippable. I thought about putting an automatic update with JavaScript at the given time, but it would be an ugly workaround.
In this case, I'm getting JSON data, but in a more general context.
How could I refresh the page as soon as new data appears?

An automatic update with Javascript might not be as dirty as you think. Even smartphone push notifications are really just occasionally polling under the hood.
If you really want to push from your server, WebSockets or server-side events are what you want. Unfortunately, it seems this kind of setup is not natively supported by Django.
Another option could be to use a paid service like Pusher.com. Such services usually let you listen for an even in JS, then call an API endpoint to trigger it from your server. This will work well on Django or any other server setup.

Related

How to use django signals to refresh a specific page

I am creating an ecommerce webapp and I want to update my admin page when user place order. After research i found out about Django signals, but it seems pretty confusing. The idea behind this is that, when a user places an order I want the admin page to be refreshed and show the latest updates. I tried ajax but javascript can only work with the current open page. Can anyone help me with usibg django signals this way?
I think you misunderstand a little, if your idea is that the admin page viewed by the user in a browser is to be refreshed. The page viewed by a site visitor is retrieved on demand of the browser. The user could refresh a page, or a page script may auto-refresh on a timer (this is not a very good solution, but it is easy).
Django can't make the browser update the page.
Signals can cause django to do something in the backend, but they can't solve the problem that the browser is in charge.
There is one technology designed to allow the server to push content to the browser: websockets.
If you use websockets (Django's fairly recent built-in support is called Channels (https://channels.readthedocs.io/en/stable/)), then you can push content to the browser, where a receiving script on the page will do something with it. This is a very powerful technique, but there is a learning curve of some hours if you are starting from scratch.
Recently I came across this front-end library which tries to make this easy as
far as the browser goes: https://htmx.org/docs/
But you still have to deal with running a websocket server and learning how to send messages to a websocket. You will however feel like a superhero at the end of it, so there's that.

Is there a way to send push notifications with Django

Hello I wonder is there a way to send push notifications with Django to a user.
I have a website that accepts/refuses vacation demands.
When a user sends a vacation demand my Django app sends email to the CEO to notify him that there was a new vacation request.
When the CEO accepts the demand it sends email to the user that the demand was accepted.
But since the CEO receives plenty of emails a day and he barely sees my emails i would like to make a browser notification whenever he opens the browser to see notification from my website that a demand is waiting to be approved/refused.
Is there a library that can do that for me,
I've tried django-webpush but I couldn't managed it to work even though I
followed all the steps.
Yes you can, since your have the information that your user accessed your server at least once. checkout this lib
https://github.com/jazzband/django-push-notifications
EDIT Gonna put more information about it
If you expect receive one response from your backend to your backend you can write some watcher to receive new data, or create one plugin, or use sockets or even make your frontend send one call to backend with some interval time to check if there is any new messages...
Lets split up a bit
1 - Watcher
Using watchers you can just watch your backend to any changes... build it from scratch i thing i a bit "hard", you can use some modern frontend framework that already have it like Angular, React, Vue... and capture new incomes messages from your backend and create Notification instance in your browser and your it to your user (i guess they will have to keep the page openned to do it... im not 100% sure)
2 - Plugins
You can build one plugin to add to your browser and receive the data from your server... since you already in browser is more easier to use browser functions
3 - Sockets
The common way to make 2 ways comunication from frontend to backend, most used with chats and things arround that, just create one channel of communications between this 2 sides and you will be able to send and receive messages from frontend or backend
4 - Dirty Way
If you not get the time to implement it like supposed to do with quality you can go the dirty way, just setup one ajax in your page to check your backend to new messages every 5 minutes? or more or less... and if find any new data (of course you will have to handle it on your backend like any other suggestions above) and then you create one new notification in your browser and show to your user...
Im sure there is bunch of libs that already do most of things to you, so just search a bit and test until you find anything that fits your need

Subscription handling complexity

I create a google glass app with custom menu using mirror api. But when I click the menu it shows a synchronization icon over timeline item and the timeline becomes first position of my app.
But I cannot get the menu's click event from my notification servlet. And the Redirect Uri
are:
http://localhost:8080/oauth2callback
http://localhost:8080
https://mirrornotifications.appspot.com/forward?url=http://localhost:8080/notify
How can I solve it?
I think you're mixing up two different concepts here.
The Redirect URLs are used as part of the OAuth dance and are only important as part of a user for your Glassware authenticating themselves to your service.
You will, however, also need to subscribe to notifications in order to receive information about the menu commands. This isn't done through the API console but, instead, you will need to issue a subscriptions.insert command as documented at https://developers.google.com/glass/v1/reference/subscriptions/insert. Your Glassware only needs to do this once, however, although it may wish to update subscriptions.
Timeline subscriptions will not work with localhost — the callback URLs must be to a server that is publicly visible to the Mirror API servers that are pushing the notifications and must also support SSL.
So to test subscriptions in development, you have a couple options:
Deploy to a staging server
Try one of a handful of localhost proxies that are available
Use curl as described in this post to manually push the notification payload to your callback URL
I've found during my own development that #3 was the easiest, but as your application gets more complex you may want to look into the other options.

Django refresh page if change data by other user

I have a test django app.
In one page the test show the same question to all users.
I'd like that when a user answers correctly, send a signal to other active user's browser to refresh to the next question.
I have been learning about signals in django I learning work with them but I don't now how send the "refresh signal" to client browser.
I think that it can do with a javascript code that check if a certain value (actual question) change and if change reload the page but I don't know this language and the information that I find was confused.
Can anybody help me?
Many Thanks.
There is no existing way to send a event from server to browser. But you can get your web page polling your server periodically (say every 5 seconds).
The code in javascript/jquery could be like the following
setInterval(function(){
$.post("your_ajax_handler_url/is_answerd", userId, function(xhr){
if(xhr.responseText == "answered"){
location.reload(true);
}
}
}, 5000);
That is not at all what signals in Django are for. Signals in django are server side hooks that allow you perform tasks on the server when a certain even happens.
To 'send a refresh' to the browser, you need to use a server-push approach such as Comet. Alternatively you can get your clients to periodically poll the server to look for update.
Here's some links:
How to implement Server push / long polling / comet using PHP and Javascript
How do I implement basic "Long Polling"?
What you need are coment(http://en.wikipedia.org/wiki/Comet_%28programming%29) and tornado(http://www.tornadoweb.org/)

What Web Applications Do You Know Using Webhooks

Description of how a webhook works from http://webhooks.pbwiki.com/ -
How do they work?
By letting the user specify a URL for various events, the application will POST data to those URLs when the events occur...Among other things, you can:
create notifications to you or anybody via email, IRC, Jabber, ...
put the data in another app (real-time data synchronization)
process the data and repost it using the app's API
validate the data and potentially prevent it from being used by the app
Who is using web hooks?
DevjaVu, BitBucket, GitHub, Shopify, Versionshelf, PayPal (IPN), Jott (Links), IMified, PBwiki, Facebook (Platform, sort of), Mailhook.org, SMTP2Web, Astrotrain, Notifixious, Assembla, ZenDesk, Google Code
Do you know of any good uses of webhooks?
AlertGrid is the webhook consumer. You can configure it to accept http calls from ANY source and raise alert (email, sms, phone) to a specified person or group of people (works worldwide!) whenever the parameters in the http callback meet your criteria or when the http call was expected but it didn't occur (kind of 'heartbeat' monitoring). There is a visual editor for you to easily create rules.
Apart from notifying people by sms or email it can also notify existing applications by sending the http requests to their APIs.
It can also visualise data received in http callbacks and show the history.
Unfortunately, the wiki is not the most up to date list of known implementations. I have my own list that I'll put on the wiki when I get around to reorganizing it. Some not mentioned in the current list:
Dropbox
Gnip
Google Code (Project Hosting)
Checkout by Amazon (both for notifications and as actual callbacks with return data)
Hubilicious
Beanstalk
Google Checkout
MailChimp
SurveyGizmo
Hey!Watch
MySpace (for app developers)
I know shopify is using webhooks quite successfully now. By extension so is fetchapp uses them as well. You either are sending an xml file, or receiving one and doing your own processing logic on it.
Oh and shopify's wiki in the link has a whole write up about how to implement it in your app.
OfficeAutopilot has an interesting version of webhooks.. they use their rule interface to trigger API posts. Can trigger in response to any system event.. email opens, clicks, page visits, purchases, etc, etc.
Kiln 1.2 uses webhooks much like GitHub, BitBucket, etc.
(Disclaimer: I'm a Kiln/FogBugz dev.)
Say for example you want to get data from any API( eg. twitter, facebook etc.,). Instead of you polling the data for every few minutes/seconds, it POSTS the data to the specified URL, whenever it is available.
By using this, you will avoid unnecessary polling like say you poll and data is not there yet.
StorageRoom is a JSON-based CMS that supports webhooks, so that you can notify other services or kick of some manual processing on your own servers.
(Please note: I created the service myself)
If you want to connect one service that supports webhooks to another service's API, you can check out IronWorker's webhook support. Here's a blog post that walks through connecting github webhooks to HipChat:
http://blog.iron.io/2012/04/one-webhook-to-rule-them-all-one-url.html
There are some other examples here too, one that takes a chargify callback and posts to Campfire.