Chrome Desktop Notification for Django

188 views
Skip to first unread message

Max Nathaniel Ho

unread,
Nov 12, 2014, 1:12:43 AM11/12/14
to django...@googlegroups.com

I found this excellent example on Chrome Desktop Notification - Chrome desktop notification example?

I am able to create the Desktop Notification by clicking a button. 

However, that is pretty front-end. 

I would like to find out how to link an event in the web app. For example,  the event could be someone posting a job for a freelance work. And user who is logged into the web app but not actively browsing on the web page will be notified via the Chrome Desktop Notification. 

Are there any examples of how I can create a Event Handler to listen to data in the backend, and prompt the user via the Desktop Notification?

Many thanks!

Collin Anderson

unread,
Nov 12, 2014, 9:55:23 PM11/12/14
to django...@googlegroups.com
Hello,

From a front-end standpoint, I personally like EventSource for this, though you could also use websockets or polling (checking every few minutes/seconds).


Collin

Max Nathaniel Ho

unread,
Nov 14, 2014, 1:25:07 AM11/14/14
to django...@googlegroups.com
HI Collin,

Thanks for pointing that out. Apart from the documentation, do you have any examples of how to implement it?

Thank you.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/64rg0i9ChyY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c162fe08-81fa-4a25-aa55-30bd2821ce2b%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Regards,
Max Nathaniel Ho.

Collin Anderson

unread,
Nov 17, 2014, 8:58:45 PM11/17/14
to django...@googlegroups.com
Hello,

The frontend part is simple:

var myEventSource = new EventSource('/path/to/eventsource/');
myEventSource
.onmessage = function(e) {
  console
.log(e.data)
}

The backend part is where things get complicated (untested code, as always :)
def eventsource(request):
    last_id
= request.META.get('HTTP_LAST_EVENT_ID')
    id
, data = get_new_data()  # somehow wait for some new data, and timeout after 10-50 seconds.
   
if not data:
     
return HttpResponse('', content_type='text/event-stream')
   
assert '\n\n' not in data, 'two line breaks in a row signifies an end of message'
   
return HttpResponse('id: %s\ndata: %s' % (id, data), content_type='text/event-stream')

Collin
Reply all
Reply to author
Forward
0 new messages