Articles Tagged: Javascript
In the previous article we made a very simple app that sent updates to clients in real-time, and in this article, we’ll build on that example and make a slightly more complicated app; a private messaging app.
In the first example messages were broadcast to all client, but this time we’ll authenticate users with Spring Security and target them specifically by using STOMP’s user-specific methods. We’ll also use a message queue (namely, Apache ActiveMQ, which must be installed on your computer) to temporarily store messages before sending them out. And as for the transport between the queue and our message dispatchers, we’ll use Apache Camel.
We live in the age of digitized social interaction. As demonstrated by the countless gameplay video streams of games over 20 years old, we feel the need to socialize with even the most static digital content. Now, everything is collaborative, en masse and in real-time, so web apps need to be able to handle this concurrent and user-scoped data in an elegant fashion in order to survive.
The traditional approach to handing out clients data in real-time (or quasi-real-time) was to make clients poll the server at regular intervals. This could be achieved by a simple polling (say, every 5 seconds), or long-polling (same as the other, but with longer-lasting sockets and late responses), but now there are new kids in town. The new approach to this situation is to let the server push the data using a publish/subscribe mechanism, rather than the client pulling them in, and technologies like Comet, WebSocket, Server-Sent Events were created for this exact purpose.
This article will demonstrate a simple guestbook app that uses the WebSocket method, and will feature Spring Boot and STOMP on the server side, and SockJS and stomp.js on the client side.
Every now and then you might need to shuffle some elements on your page, like in an online banking page you might have to display a virtual keypad which could be shuffled on demand, or on a tile game you’re most likely to have to shuffle the board. This problem usually boggles the mind of many people, but it has a very simple solution.
Let’s start with the basics. Javascript arrays have a method called sort(), which returns the sorted version of an array (note that it doesn’t actually modify the array itself), and receives a callback function as a parameter (called a predicate function), which in turn receives two parameters, referring to tuples of values from the array. The predicate is expected to compare those two values, and return a numeric value that tells the method which of the two values is supposed to rank first. Given that the two values are represented by a and b, the predicate would return a negative value when a comes before b, a positive value when b comes before a, and 0 if a and b match. Let’s look at an example.
Given an array of integers such as [ 3, 2, 8, 1, 5, 1 ], and given that we mean to sort it by ascending order, our predicate can be as follows: