Real-time Notifications with javascript [closed]

1

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

  • A teacher requests a reservation;
  • Reservation data is stored in a json object (or in a request table);
  • At the same time, a notification is displayed on the operator's screen;
  • The operator can approve the reservation, which would give a INSERT in the bank with the reservation data.
  • I figured the json form would be easier, so it would not involve Java or SQL in the process. But my question: is it how to do it exactly? I do not know much about JavaScript yet, but I imagine it would be necessary to use ajax, correct? Could you help me with this?

        
    asked by anonymous 09.02.2017 / 18:59

    1 answer

    2

    You have two options mostly.

    One is to call polling via ajax, that is, in the operator's client a request is sent every x time interval to check if there is a new reservation. This is a simple way to verify the information but many unnecessary server requests are sent. Here's 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 websockets through the Socket.io API to maintain an open connection between the client and the server without having to make useless queries . This is an old article but you can help Connecting to Socket.io

    Even if you do not know a lot about javascript it is worth exploring the second option that is more current and advanced.

        
    10.02.2017 / 01:42