Free AI web copilot to create summaries, insights and extended knowledge, download it at here
3019
Abstract
on port <b>8080.</b></p><p id="107b">The application has a single class that handles web sockets, which you can find at <code>com.edu.retail.ws.DashboardWebSocket</code>. It implements the standard WebSocket lifecycle methods <code>onOpen()</code>, <code>onError()</code>,<b> </b>and<b> </b><code>onClose()</code><b>.</b></p>
<figure id="3a85">
<div>
<div>
<iframe class="gist-iframe" src="/gist/dunithd/e6d558aecf852762b4dccf015f179222.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="cda7">The annotation <code>@ServerEndpoint("/dashboard/{clientId}")</code> marks the class as a WebSocket endpoint so that Quarkus will route any client request to there. <code>{clientId}</code> indicates the unique ID used by dashboard clients. The endpoint uses that ID to identify individual dashboards.</p><p id="4f13">Notice the <code>sessions</code> hash map that stores sessions of all connected clients. A <b>Session</b> is how the WebSocket server communicates with the client. At <code>onOpen</code>, the client session is put into the <code>sessions</code> map. In the case of <code>onClose</code> or <code>onError</code>, the session is removed from the map.</p><p id="74fe"><code>onMessage</code> is where we receive messages from the client. Since this is a dashboard, we don’t necessarily want to listen to incoming messages. The critical method is <code>broadcast</code>, which writes data to all connected sessions.</p><p id="db38">We will come to that in a moment.</p><h1 id="58e7">The dashboard client</h1><p id="1bd5">When the dashboard loads, it makes a WebSocket connection to the Quarkus application and starts listening to incoming data. When new data arrives, it will update its UI to show the current number of sales orders received today.</p><p id="831e">For simplicity, I wrote the entire dashboard using plain HTML and Javascript. Bootstrap and jquery are the only external libraries I’ve used.</p><p id="17e1">You can find the dashboard implementation inside the <b>META-INF/resources </b>directory of the Quarkus application. Quarkus automatically serves static resources contained in the <b>META-INF/resources</b> directory.</p><p id="5c95">The following file, <b>META-INF/resources/js/dashboard.js</b> contains the logic to handle the WebSocket connection.</p>
<figure id="8ed5">
<div>
<div>
<iframe class="gist-iframe" src="/gist/dunithd/73788d0de8218755c94e5e9d21252ac3.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="1991">Clicking on the <b>Connect</b> button establishes a WebSocket connection with the server. Since both client and server are running on the same process, we can use localhost as the hostname. The co
Options
de generates a random string and uses it as the client ID.</p><p id="79e9"><code>socket.onmessage</code> binds to a callback function that updates the content of a div with received data.</p><div id="05ca"><pre>socket.onmessage =<span class="hljs-keyword">function</span>(<span class="hljs-params">m</span>) {
<span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-string">"Got message: "</span> + m.data);
$(<span class="hljs-string">"#totalOrders"</span>).<span class="hljs-built_in">text</span>(m.data);
};</pre></div><p id="2ca1">An important thing to note is that even though WebSockets is a bidirectional communication protocol, I haven’t considered sending data back to the server. Usually, real-time dashboards operate unidirectionally, that is, to receive data from the server and render them on the UI.</p><h1 id="72ff">Scheduled task</h1><p id="be63">So far, we have looked at the WebSocket server and client. Now we need to push some sales data to clients. To mimic a real-world scenario, I have created a scheduled task to write random values to all the sessions periodically.</p><p id="4371">Creating a scheduled task in Quarkus is very easy. You need to add the <code>scheduler</code> extension to the project’s POM file and create the method that needs to executed on the given period.</p><p id="1220">In this example, I’ve created a new method <code>increment()</code> in the same <code>DashboardWebSocket </code>class. It increments the value of <code>totalOrders</code> and broadcasts to all connected dashboards <b>every 5 seconds</b>.</p><div id="e290"><pre><span class="hljs-keyword">@Scheduled</span>(every=<span class="hljs-string">"5s"</span>)
void increment() {
if (sessions != null) {
totalOrders<span class="hljs-selector-class">.incrementAndGet</span>();
<span class="hljs-built_in">broadcast</span>(String.valueOf(totalOrders));
}
}</pre></div><p id="7a19">For more information on scheduling periodic tasks, Quarkus has an excellent <a href="https://quarkus.io/guides/scheduler">tutorial</a> you can follow.</p><h1 id="2963">Running the application</h1><p id="8e95">Now, let’s see our application in action. Using a terminal, navigate to the location where you’ve cloned the repository and issue the following commands.</p><p id="68a5"><code>cd quarkus-websockets-dashboard
./mvnw compile quarkus: dev</code></p><p id="122e">Then open your browser window to <a href="http://localhost:8080/">http://localhost:8080/</a>. Click on the <b>Connect</b> button and see the total number of sales orders changing every 5 seconds.</p><figure id="ee72"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*FEERUNn_qF5B85g17OkV8g.png"><figcaption></figcaption></figure><h1 id="60b7">References</h1><p id="527b"><a href="https://quarkus.io/guides/websockets">Quarkus — Using WebSockets</a></p><p id="e116"><a href="https://quarkus.io/guides/scheduler">Quarkus — Scheduling Periodic Tasks</a></p></article></body>