Answer the question
In order to leave comments, you need to log in
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;
case '/getChairSumm':
//...........
console.log('запрос кол-ва стульев');
//отправляем заголовок для открытия SSE на клиенте
res.writeHead(200, {
'Content-Type': 'text/event-stream; charset=utf-8',
'Cache-Control': 'no-cache'
});
//???тут хаос и поток сознания
}
break;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question