M
M
mega epic2019-04-17 13:29:14
JavaScript
mega epic, 2019-04-17 13:29:14

How to handle multiple SSEs on an event on a server?

Hello. I am writing a web application within the framework of AIS in an enterprise. nodejs+mysql server. I can not deal with the following task (due to lack of experience, apparently). I'll try to be brief:
Task: The head of production on the form in the web application should change a certain figure online (for example, the number of chairs produced). Those. the worker made a chair and pressed the "add chair" button on his form, and at that moment the number on the manager's form changed without any page updates (for example, from 4pcs to 5pcs).
How I started to do ( Further text may injure you, the essence of the question is at the bottom ): I
began to google ways to get data from the server online. I found that Server-Sent Events is suitable for me.
With the client, the algorithm is more or less clear: I create new EventSource(). On receipt of a new message, I hang up a handler, which takes out the number of chairs from the json that came from the server and inserts it onto the page.
But the problem is with the server: there is a function on the node server

case('addChair')://фрагмент из кода, обрабатывающего запросы с клиента
    console.log('Добавление стула');
    //вставка стула в бд(некоторые подсобные части кода опущены) 
     var values = {//информация о стуле с базы данных}
            Database("INSERT INTO ?? SET ?", values , function(req){
         //..........
           res.end(//отвечаю на клиент что стул добавлен);
     });
  }
break;

(called by ajax from a client post request) - it adds a new chair to the database. Everything is clear there.
Now I am writing a function for "online" sending json to the client with the number of chairs:
case '/getChairSumm':
 //...........
    console.log('запрос кол-ва стульев');
 //отправляем заголовок для открытия SSE на клиенте
    res.writeHead(200, {
      'Content-Type': 'text/event-stream; charset=utf-8',
      'Cache-Control': 'no-cache'
    });
//???тут хаос и поток сознания
  }
break;

I write to the header of the event stream, the client listens to the response. I can immediately answer with a line with the number of chairs (by making a request to the database and sending json with the result to the client, where my handler will insert the updated number of chairs on the page.
But!!! I need the chairs on the page to be updated according to the end of the function for adding a chair (addChair above in the text). That is, the addChair function needs to call the method (?) that requests the number of chairs and sends json to the client. The problem is that there can be several clients.
I tried to create objects in which requests were stored. wrote a function that was called after adding a chair (addChair), it in turn called methods of objects (these methods sent responses) with requests.()
And actually the question is: how to implement such an algorithm for issuing data on an event on the server? I'm completely confused. The question is kind of stupid, the answer - "how how ... with your hands" is obvious.
I would like to receive your vision of solving the problem in general terms (verbal algorithm). It is possible on a paid basis.)) Thank you for reading and answering.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question