in reply to Re^3: Mojolicious refresh
in thread Mojolicious refresh
Avoid polling. Changed content is pushed to the client. This is AJAX based anyway.
Greetings,
-jo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Mojolicious refresh
by Your Mother (Archbishop) on Mar 19, 2020 at 06:54 UTC | |
I think it would be fantastic to see some demo code for that here. | [reply] |
by jo37 (Curate) on Mar 19, 2020 at 08:19 UTC | |
I was afraid somebody would ask :-) Some years ago I wrote such an application using AngularJS and Mojolicious::Lite. It's still running but I need to strip it down to a small demo. Maybe corona gives me the time to do that. Have some patience. Greetings, | [reply] |
by jo37 (Curate) on Mar 20, 2020 at 15:01 UTC | |
Here is an example using Mojolicious::Lite and AngularJS. The Mojolicious controller in this example watches for changes in the file data/content.pl. Only the parts therein that are modified are pushed to connected clients via a web socket. The index page show the usage of placeholders {{text}} and {{hash}} that will be replaced by the AngularJS module. The AngularJS module provides the variables text and hash in it's $rootContext for insertion into the HTML index page and has the client side web socket for receiving updates from the server. The config file data/content.pl defines a hash of values that are transmitted to the client. Changes of text will be visible on the client immediately. When the value of hash is modified and pushed to the client, this causes a modified url parameter in the <img> tag and thus forces a reload of the image. If hash is taken as the SHA sum of the given picture, a reload can be forced if and only if the picture has changed. Put any picture you like to public/image.png for this demo. Start the server and point your browser to http://localhost:8880. You may notice the displayed text changing from "text from root context" to the text provided in data/content.pl. Modify the content of data/content.pl and observe the change on the displayed page. demoserver.pl:
public/index.html:
public/demo.js:
data/config.pl:
EDIT: removed workaround for <script> tags in html file. Greetings, | [reply] [d/l] [select] |
|
Re^5: Mojolicious refresh
by LanX (Saint) on Mar 19, 2020 at 08:41 UTC | |
Sure, but again: what for would one want this? Speed?
Cheers Rolf | [reply] |
by jo37 (Curate) on Mar 19, 2020 at 09:09 UTC | |
In my case to avoid unnecessary traffic, especially for generated images. I can't tell how "smooth" your approach works. The push initiated update happens almost invisible. Greetings, | [reply] |
by LanX (Saint) on Mar 19, 2020 at 09:41 UTC | |
Probably, but logically it's a pull if you say it's initiated by the client.
Cheers Rolf | [reply] |
by haukex (Archbishop) on Mar 19, 2020 at 09:55 UTC | |
by LanX (Saint) on Mar 19, 2020 at 10:04 UTC | |
| |