<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: Show a real time progress bar in a Html User Dialog?</title><link>https://answers.axonivy.com/questions/16/show-a-real-time-progress-bar-in-a-html-user-dialog</link><description>&lt;p&gt;I sometimes have server side actions that last for quite some time and I want the user  kept informed about the progress in real time. How can I do that easily?&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/16/show-a-real-time-progress-bar-in-a-html-user-dialog" rel="self"></atom:link><language>en</language><lastBuildDate>Fri, 22 May 2015 22:12:36 -0400</lastBuildDate><item><title>Comment by Ammoula essaid on SupportIvyTeam's answer</title><link>https://answers.axonivy.com/questions/16/show-a-real-time-progress-bar-in-a-html-user-dialog#1328</link><description>&lt;p&gt;hi , please how can i use this exemple with primefaces 4.0&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ammoula essaid</dc:creator><pubDate>Fri, 22 May 2015 22:12:36 -0400</pubDate><guid>https://answers.axonivy.com/questions/16/show-a-real-time-progress-bar-in-a-html-user-dialog#1328</guid></item><item><title>Answer by SupportIvyTeam</title><link>https://answers.axonivy.com/questions/16/show-a-real-time-progress-bar-in-a-html-user-dialog/17</link><description>&lt;p&gt;You can achieve this with a combination of &lt;a href="http://www.primefaces.org/showcase/push/index.jsf"&gt;PrimePush&lt;/a&gt; and &lt;a href="http://www.primefaces.org/showcase/ui/progressBar.jsf"&gt;p:progressBar&lt;/a&gt;. You can use &lt;a href="http://www.primefaces.org/showcase/ui/blockUI.jsf"&gt;p:blockUI&lt;/a&gt; in addition to block the UI. See this &lt;a href="http://answers.xpertivy.ch/questions/10/can-i-use-primepush-in-xpert-ivy"&gt;entry in the Q&amp;amp;A&lt;/a&gt; for how to install PrimePush.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;h:head&amp;gt;
        &amp;lt;script&amp;gt;
                function handleProgress(progressJson) {
                        var progressData = eval("(" + progressJson + ")");
                        progressBar.setValue(progressData.progress);
                        $("#msg").text(progressData.msg);
                }
        &amp;lt;/script&amp;gt;
&amp;lt;/h:head&amp;gt;
&amp;lt;h:body&amp;gt;
        &amp;lt;h:form&amp;gt;
                &amp;lt;p:panel id="panel" header="Realtime ProgressBar"&amp;gt;
                &amp;lt;p:commandButton id="btn" value="Start" actionListener="#{logic.startLongRunningProcess}"&amp;gt;&amp;lt;/p:commandButton&amp;gt;                   
                &amp;lt;/p:panel&amp;gt;
                &amp;lt;p:blockUI block="panel" trigger="btn"&amp;gt;
                        &amp;lt;p:progressBar widgetVar="progressBar" style="width:300px" labelTemplate="{value}%"&amp;gt;&amp;lt;/p:progressBar&amp;gt;
                        &amp;lt;span id="msg"&amp;gt;&amp;lt;/span&amp;gt;
                &amp;lt;/p:blockUI&amp;gt;
        &amp;lt;/h:form&amp;gt;
        &amp;lt;p:socket channel="#{data.channel}" onMessage="handleProgress"&amp;gt;&amp;lt;/p:socket&amp;gt;
&amp;lt;/h:body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On lines 15 to 18 we define the progress bar and a label within a &lt;code&gt;p:blockUI&lt;/code&gt; element. It is important to define the &lt;code&gt;widgetVar&lt;/code&gt; variable because we will access it in the JavaScript function &lt;code&gt;handleProgress&lt;/code&gt; to set the current value for the progress bar.&lt;/p&gt;
&lt;p&gt;The aforementioned JavaScript function &lt;code&gt;handleProgress&lt;/code&gt; receives a JSON string as input. It then transforms this JSON into a object (line 4) with the attributes &lt;code&gt;progress&lt;/code&gt; and &lt;code&gt;msg&lt;/code&gt;. We will use &lt;code&gt;progress&lt;/code&gt; for the progress in percentage and &lt;code&gt;msg&lt;/code&gt; for the label to display.&lt;/p&gt;
&lt;p&gt;So, big question? From where we will call &lt;code&gt;handleProgress&lt;/code&gt;? Well, the answer is on line 20. With &lt;code&gt;p:socket&lt;/code&gt; a connection from the server to the client is opened with which the server can send messages directly to a client. And as soon as a message is send, then the JavaScript function defined in the &lt;code&gt;onMessage&lt;/code&gt; attribute will be called.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;channel&lt;/code&gt; attribute of &lt;code&gt;p:socket&lt;/code&gt; defines the topic on which the connection is listening. In our case we use a process data member &lt;code&gt;channel&lt;/code&gt;. For a progress bar, you must ensure that every dialog has its own channel. So, e.g. in the startup of the dialog you can set something like 
&lt;br&gt;&lt;br&gt;
&lt;code&gt;in.channel = "/realtime/" + java.util.UUID.randomUUID().toString();&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now, to send an update to the progress bar use the PrimePush API, e.g. 
&lt;br&gt;&lt;br&gt;
&lt;code&gt;PushContextFactory.getDefault().getPushContext().push(in.channel, "{ 'progress' : 10, 'msg' : 'Extract data'}");&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="real time progress bar" src="/upfiles/realtimeprogressbar.png"&gt;&lt;/p&gt;
&lt;p&gt;Please try as well the &lt;a href="http://xpert-ivy-hacker-source-code.googlecode.com/svn/trunk/PrimePush"&gt;example project&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;br&gt;
&lt;br&gt;
Note: This question and answer was &lt;a href="http://blog.xpertivyhacker.ch/2012/11/xpertivy-code-rezept-realtime-progressbar.html"&gt;originally posted&lt;/a&gt; by Heinrich Spreiter on his &lt;a href="http://blog.xpertivyhacker.ch/"&gt;Xpert.ivy Hacker blog&lt;/a&gt;. Henry, many thanks to you for your enthusiastic work. &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">SupportIvyTeam</dc:creator><pubDate>Mon, 07 Oct 2013 17:21:12 -0400</pubDate><guid>https://answers.axonivy.com/questions/16/show-a-real-time-progress-bar-in-a-html-user-dialog/17</guid></item></channel></rss>