G
G
Gleb Lukashonok2021-10-20 12:32:07
Node.js
Gleb Lukashonok, 2021-10-20 12:32:07

How to find out about new events?

Hello.
There is one program with an old square interface, the task is to remake the interface (at least part of it).
I will use React / Node js / Firebird (db).
Essence of a question: it is necessary to create "listener" of a database. And somehow send it to the front.
New events are generated in the database (from the controller), how do I find out what's new there? How then to immediately transfer it to the front? Tipo look at me here the array has replenished - finish drawing.
In general, there are no thoughts about creating a listener at all. And about automatic sending to the front - is it necessary to use sockets? Or is there something better?
Please advise, thanks!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
syxme, 2021-10-21
@syxme

It is possible to use classical long polling
Principle such:
In the table where at you are updated, data is added there has to be a field something like updateTime (last update time) If record is created then updateTime = createTime.
To implement long polling, it is necessary that the server does not send a response until timeout has passed (5-15 seconds on average) or new data has appeared.
Implementation example on nodejs

function subscribe(req,res,params,callback){
  var timeStamp;
  if (params.timestamp){  // Если передали timestamp c которого начинать выборку данных
    timeStamp = new Date(parseInt(params.timestamp));
  }else{
    timeStamp = new Date();
  }
  var isConnected = true;
  req.on('close', function() { // Если пользователь закрыл соединение
    isConnected = false;
  });
  console.log("timeStamp",timeStamp);
  (function startEvents(cb,time){

    if (!isConnected){
      console.log("Connection close_end pool");
      return;
    }
    model.db.find({}).sort({updateTime :-1}).exec(function (err,items){ // SELECT * FROM datatable ORDER BY updateTime DESC
      if (items.length>0){
        cb(null,{
          items:items,
          timestamp:new Date(items[0].updateTime).getTime()  // Возвращаем время для следующего соединения
        });
      }else {
        if (time > 10) {
          cb(null, {
            items:[],
            timestamp:new Date().getTime()  // Возвращаем время для следующего соединения 
          });
        } else {
          setTimeout(function (){
            startEvents(rQuery, group, cb, time + 1);
          },1000);
        }
      }
    });

  })(callback,0)
}

The first request will be something like this: localhost:8080/controller/subscribe
{items:[],timestamp:1634803048151} //  Если ничего не изменилось 
{items:[1,2,3,4,5],timestamp:1634803048151} //  Если что-то изменилось

The next request will be
localhost:8080/controller/subscribe?timestamp=1634...
And so on ad infinitum

N
Nam Zanilla, 2021-10-21
@sadsdasdasdsadsadsadsadsa

If it is possible to use sockets - use it, long polling is already so to speak when there are no other options, but this is clearly not better than sockets

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question