Ad

Send Real-time Data From Networked C Program To A Browser Client To Plot

I am faced with an architectural problem to which I cannot seem to find a suitable solution so far. I have a Linux box which is just a machine connected to the network with an Ethernet cable, running Linux. It is sampling values from an ADC(analog to digital converter) at about 3000 samples/sec. At each sample, it actually samples all 8 channels of the ADC. So, I have 3000*8 DOUBLE type values every second.

My problem is the following: stream this data in real time to a client running a web application and plot it there with some Javascript framework. The program on the Linux box has to be in C. I try to avoid interop like the one available with .NET Core to avoid the complexity.

What I have done so far?

I have implemented a web-server in C running as a child process along with the parent process which samples ADC values. (actually the parent does not need to sample, I did it as a demo before I incorporated it in this program). The server can serve a GET request to which it only know to respond with JSON-formatted values of all 8 channels like in the screenshot below. Here is the request I am making from my computer: screenshot of me calling the server on the box. Receiving JSON data back.

Note that I only want this to work on the local network.

Now, what I think is that I could have some JS framework which receives the data from this Linux box by calling 192.168.1.95:8080 continuously in an Ajax GET request, and plotting all 8 channels simultaneously. Is that viable? How bad is this design? Can this crash the browser? Note that, each request like the one in screenshot takes ~260 bytes as sending, and response (JSON) is ~130 bytes (about 50% of exchanged data is HTTP headers).

The way I feel is that if I continue like this, I will be wasting 99% of the data the Linux box samples only getting a very small amount of data in the client web application. Note that, I have not done the web app yet. So, I do not know how it actually performs. Although, I can tell you that the server on the Linux box is quite fast. Plus, it works locally only.

A much better solution would be if I could get a socket work in the browser. Then, this would be much faster (I think).

Can you suggest a better way to do this? I would also accept a solution which can be used outside of the local network as well. I am looking for a scalable solution. For the client app, I intend to use ASP.NET Core.

Thinks I have thought of: Web Sockets (using a library for C?)? Would this be faster?

Anything you can give me would be highly appreciated.

In terms of code, here is the code:

https://github.com/giorgi3092/ECG_HW_SW_DE1_SoC/blob/master/adc_example/web_server/web_server2.c

Ad

Answer

For your use case, WebSocket is definitely more scalable than doing HTTP polling.

If you want to improve overall system performance, you could use a more compact and efficient data serialization format, for example like MessagePack, it is available in wide array of programming languages (for your case, JavaScript and C).

In more extreme case where existing serialization formats didn't work too well, then you might want to directly encode double[] into uint8_t[] (C) at your server and push to browser, and then use TypedArray (JavaScript) in browser to access values encoded by the server.

On the other hand, I think C# (.NET Core) could work as good as C in this case.

Ad
source: stackoverflow.com
Ad