Real Time Notifications with javascript

Asked

Viewed 1,015 times

2

I’m building a Java application that should show real-time notifications on the screen.

  1. A teacher requests a reservation;
  2. Reservation data is stored in a json object (or in a request table);
  3. At the same time, a notification appears on the operator screen;
  4. The operator may approve the reservation, which would give a INSERT in the database with the reservation data.

I figured the json shape would be easier, so it wouldn’t involve Java or SQL in the process. But my question: is how to do this exactly? I don’t know much about Javascript yet, but I imagine it would be necessary to use ajax, right? Could you help me with this?

  • 1

    java or javascript ?

  • As your question is very wide, and Real Time can vary greatly in complexity, I will suggest that you start your research with Node.js: https://nodejs.org/en/

  • @Douglas, the application itself is written in Java, but what I want to do doesn’t necessarily need Java, so I focused on Javascript and jQuery.

  • @Thiagosantos, thank you. I’m going to make a move on Ode.js.

  • I believe that, since we will not add anything to the community here, it is better to close that question. A last tip: https://atendesigngroup.com/blog/using-nodejs-create-real-time-web-applications is a good starting point!

  • See if it helps: http://gabrielfeitosa.com/angularjs-refresh-periodico/

Show 1 more comment

1 answer

3

It has two options mainly.

One is to make the so-called Polling via ajax, ie in the operator’s client is sent a request every x time interval to check if there is a new reservation. This is a simple way to check the information but a lot of unnecessary requests are sent to the server. See an example that makes a call every 5 seconds:

(function poll() {
setTimeout(function() {
    $.ajax({
        url: "/server/api/function",
        type: "GET",
        success: function(data) {
            console.log("polling");
        },
        dataType: "json",
        complete: poll,
        timeout: 2000
    })
}, 5000);

})();

The other solution is to use the websockets via the API Socket io. to keep an open connection between client and server without having to do useless queries. This is an old article but can help Connecting to Socket.io

Even if you don’t know much about javascript it’s worth exploring the second option which is more current and advanced.

  • Thank you, riotera and Thiago Santos, I’ll take a look at the links you sent. I’ll go into Socket.io, which both links speak about..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.