Push Notifications

251 views
Skip to first unread message

Jaimee S

unread,
Nov 2, 2016, 10:32:54 PM11/2/16
to web2py-users
Hello,

My website allows users to send messages to one another; however, the only way to see their new messages is to refresh the page. Is there any way to notify the user via any method?

Thanks for your time

Dave S

unread,
Nov 2, 2016, 11:52:05 PM11/2/16
to web2py-users


There are 2 mechanism in HTML5, I believe, but I'm only going to point you at one for the moment:  Web Workers.
<URL: http://www.htmlgoodies.com/html5/javascript/working-with-web-workers-in-html5-powered-web-pages.html>
<URL:http://www.htmlgoodies.com/HTML5/client/introduction-to-html5-web-workers-the-javascript-multi-threading-approach.html>
<URL:http://www.htmlgoodies.com/html5/other/html5-tech-shared-web-workers-help-spread-the-news.html>

With this mechanism, you'd spawn a web worker  to do jquery/ajax to check when it was time to replace the content.  A couple of the examples calculate Pi, and paste the results into the main page.

There is also web sockets in HTML5.
<URL:http://www.htmlgoodies.com/html5/tutorials/making-html5-websockets-work.html>
Gluon/contrib has websocket_messaging.py.

I thought I remembered an HTML5 server push mechanisms for notifications, but if it's there I'm not looking in the right place.

/dps

Jaimee S

unread,
Nov 3, 2016, 1:30:23 AM11/3/16
to web2py-users
Thanks for the speedy reply! I've looked over the links you sent. the first two didn't work for me on my browser. I'm on a a mac and used the default browser. 

Will having a web socket open 24/7 slow down my website? My knowledge of web sockets is limited at the moment

Anthony

unread,
Nov 3, 2016, 2:37:52 AM11/3/16
to web2py-users
On Wednesday, November 2, 2016 at 7:52:05 PM UTC-4, Dave S wrote:
There are 2 mechanism in HTML5, I believe, but I'm only going to point you at one for the moment:  Web Workers.
<URL: http://www.htmlgoodies.com/html5/javascript/working-with-web-workers-in-html5-powered-web-pages.html>
<URL:http://www.htmlgoodies.com/HTML5/client/introduction-to-html5-web-workers-the-javascript-multi-threading-approach.html>
<URL:http://www.htmlgoodies.com/html5/other/html5-tech-shared-web-workers-help-spread-the-news.html>

With this mechanism, you'd spawn a web worker  to do jquery/ajax to check when it was time to replace the content.  A couple of the examples calculate Pi, and paste the results into the main page.

This approach is "short polling" (i.e., polling the server with quick requests at some interval to check for updates). Note, there is no particular reason this must be done with a web worker -- you can simply do it from the main web page, as it has been done since long before web workers existed. The idea is simply to keep making Ajax requests at regular intervals (e.g., every second). This approach might be fine, though depending on how many users are connected and how frequently they are checking, the server could get overwhelmed with requests. To reduce the load, you can decrease the request frequency, but then you increase the average latency between when a message is sent and when the recipient receives it.
 
There is also web sockets in HTML5.
<URL:http://www.htmlgoodies.com/html5/tutorials/making-html5-websockets-work.html>
Gluon/contrib has websocket_messaging.py.

To handle the shortcomings of "short polling", you can instead use websockets (or long-polling, which maintains a long-held HTTP connection with the server until a new message is received). However, you need a web server and application that can handle many long-held open connections. To address this need, web2py includes websocket_messaging.py, as noted above -- it makes use of the Tornado web server to handle the websocket connections. You can also use various realtime messaging services (e.g., Fanout), or something like Pushpin (an open source proxy server used by Fanout), which is probably a bit more robust and full-featured than websocket_messaging.py.

Anthony

Jaimee S

unread,
Nov 3, 2016, 7:48:29 PM11/3/16
to web2py-users
Thanks for such a thorough response, Anthony!

Long polling seems to be the way to go for my website. I've been reading up on the tornado web socket  all day. 

I'm nearing 100 users on my website and i believe that speed will be a problem eventually. I'm deployed to python anywhere like you suggested; however, i am using sqlite still. As of now, everything is working and i've received no complaints on performance. Will there be a problem with long polling while using sqlite?

Dave S

unread,
Nov 3, 2016, 7:51:16 PM11/3/16
to web...@googlegroups.com


On Wednesday, November 2, 2016 at 7:37:52 PM UTC-7, Anthony wrote:
On Wednesday, November 2, 2016 at 7:52:05 PM UTC-4, Dave S wrote:
There are 2 mechanism in HTML5, I believe, but I'm only going to point you at one for the moment:  Web Workers.
<URL: http://www.htmlgoodies.com/html5/javascript/working-with-web-workers-in-html5-powered-web-pages.html>
<URL:http://www.htmlgoodies.com/HTML5/client/introduction-to-html5-web-workers-the-javascript-multi-threading-approach.html>
<URL:http://www.htmlgoodies.com/html5/other/html5-tech-shared-web-workers-help-spread-the-news.html>

With this mechanism, you'd spawn a web worker  to do jquery/ajax to check when it was time to replace the content.  A couple of the examples calculate Pi, and paste the results into the main page.

This approach is "short polling" (i.e., polling the server with quick requests at some interval to check for updates). Note, there is no particular reason this must be done with a web worker -- you can simply do it from the main web page, as it has been done since long before web workers existed.

The idea I was applying was keeping the main page quiet, although Google News has used refresh interval to update the entire main page.
Web workers give you a thicker curtain to draw over the checkers, than does a LOAD/jquery/ajax in a timer loop on the main page.
 
The idea is simply to keep making Ajax requests at regular intervals (e.g., every second). This approach might be fine, though depending on how many users are connected and how frequently they are checking, the server could get overwhelmed with requests. To reduce the load, you can decrease the request frequency, but then you increase the average latency between when a message is sent and when the recipient receives it.
 
There is also web sockets in HTML5.
<URL:http://www.htmlgoodies.com/html5/tutorials/making-html5-websockets-work.html>
Gluon/contrib has websocket_messaging.py.

To handle the shortcomings of "short polling", you can instead use websockets (or long-polling, which maintains a long-held HTTP connection with the server until a new message is received). However, you need a web server and application that can handle many long-held open connections. To address this need, web2py includes websocket_messaging.py, as noted above -- it makes use of the Tornado web server to handle the websocket connections. You can also use various realtime messaging services (e.g., Fanout), or something like Pushpin (an open source proxy server used by Fanout), which is probably a bit more robust and full-featured than websocket_messaging.py.

Anthony

Thanks for the additional pointers.  For myself, I haven't yet gotten to an immediate need of this, but I'm starting to browse the Pushpin docs.
 

/dps
 

Anthony

unread,
Nov 4, 2016, 2:04:59 AM11/4/16
to web2py-users
On Thursday, November 3, 2016 at 3:48:29 PM UTC-4, David wrote:
Thanks for such a thorough response, Anthony!

Long polling seems to be the way to go for my website. I've been reading up on the tornado web socket  all day. 

I'm nearing 100 users on my website and i believe that speed will be a problem eventually. I'm deployed to python anywhere like you suggested; however, i am using sqlite still. As of now, everything is working and i've received no complaints on performance. Will there be a problem with long polling while using sqlite?

Use of long-polling/websockets will not affect the database. You just have to worry about how many reads and writes you are doing. You might get away with SQLite for quite a while -- see https://sqlite.org/whentouse.html.

Anthony

Anthony

unread,
Nov 4, 2016, 2:17:00 AM11/4/16
to web2py-users
On Thursday, November 3, 2016 at 3:51:16 PM UTC-4, Dave S wrote:

With this mechanism, you'd spawn a web worker  to do jquery/ajax to check when it was time to replace the content.  A couple of the examples calculate Pi, and paste the results into the main page.

This approach is "short polling" (i.e., polling the server with quick requests at some interval to check for updates). Note, there is no particular reason this must be done with a web worker -- you can simply do it from the main web page, as it has been done since long before web workers existed.

The idea I was applying was keeping the main page quiet, although Google News has used refresh interval to update the entire main page.
Web workers give you a thicker curtain to draw over the checkers, than does a LOAD/jquery/ajax in a timer loop on the main page.

Understood. Just wanted to make it clear that the short-polling approach does not require the added complication of web workers.

Anthony

David

unread,
Nov 4, 2016, 2:20:41 AM11/4/16
to web2py-users
I read your link, Anthony. Based off of your link, a few other pages linked from your link, and a couple of Google searches I can see that sqlite is small time(unless you tweak it) and I'll definitely need to switch my db eventually.

Thanks for both of you guys time.

Michael Beller

unread,
Nov 8, 2016, 12:45:48 PM11/8/16
to web2py-users
I've been thinking about trying https://pusher.com/

Dave S

unread,
Nov 9, 2016, 6:14:46 AM11/9/16
to web2py-users


On Wednesday, November 2, 2016 at 7:37:52 PM UTC-7, Anthony wrote:
[...] You can also use various realtime messaging services (e.g., Fanout), or something like Pushpin (an open source proxy server used by Fanout), which is probably a bit more robust and full-featured than websocket_messaging.py.


Pushpin looks pretty easy to set up.  For the client side, they seem to recommend the Pollymer ajax library.

/dps

Dave S

unread,
Nov 9, 2016, 6:27:01 AM11/9/16
to web2py-users


On Tuesday, November 8, 2016 at 4:45:48 AM UTC-8, Michael Beller wrote:
I've been thinking about trying https://pusher.com/


They use servers running on an Amazon cluster?

/dps

 
Reply all
Reply to author
Forward
0 new messages