G
G
GerasimGerasimov2020-03-10 12:45:43
Node.js
GerasimGerasimov, 2020-03-10 12:45:43

Websocket asynchronous client requests to one async server function how to organize?

I am writing a data collection system using WebSoket on Node.JS.

Clients access the server, the server, in turn, requests data from the hardware using an asynchronous function (that is, you have to wait until the hardware responds).

The data is requested by the client with the maximum possible frequency (about 300 times per second), as soon as the server has responded, a new request is immediately sent.

As long as there is only one client, everything is fine. When a second client is added, then, since they refer to the same asynchronous function, I begin to interfere with each other, sometimes hanging up the connection for several seconds.

How to properly design a server application in such a situation? It suggests creating an access queue to an asynchronous function with the expectation of a response to a request to this function from a specific socket. Surely someone has encountered similar behavior (for example, an asynchronous function - a request with a long call to the database) and somehow defeated the problem.

I changed this function to synchronous (produces some kind of real JSON) - then the whole server works without failures.

Sockets call the same async function, passing it different (after all, each socket has its own request) parameters. The function returns a Promice... but it doesn't automatically build a queue of calls to this function with different parameters, the result of which should be returned to the socket that called it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Bobkov, 2020-03-11
@mike_bma

You should separate the request from the client to the server and the request from the server to the hardware.
It will turn out like this - the server by a separate process maintains the relevance of the data from the "piece of iron", saving, for example, the data itself and the time it was received.
The client makes a request to the server and receives from it the latest up-to-date data from the object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question