On my client (phone with a browser) I want to look at the statistics of the server, RAM and HDD and collect information from different logs.
I am using ajax poll.
On the client, every 5 seconds (setInterval) I call the PHP file:
Problems:
- Open a new connection every 5 seconds.
- Multiple AJAX Calls.
- Request headers (they are also data and consume bandwidth)
- Answer Headers (^)
- Use PHP to read files every 5 seconds. even if nothing has changed.
The final JSON data is less than 5 KB, but I send it every 5 seconds, and headers and a new connection appear every time, so basically every 5 seconds I have to send 5-10 KB to get 5 KB, which are 10-20 KB .
This is 60 sec / 5 sec = 12 new connections per minute and about 15 MB per hour of traffic if I leave the application open.
Suppose I have 100 users whom I allow to control / control my server, which will be about 1.5 GB of outgoing traffic in one hour.
Not to mention the fact that the PHP server reads several files 100 times every 5 seconds.
I need something on the server that reads the last lines of these logs every 5 seconds and possibly writes them to a file, then I want to send this data to the client only if it has changed.
SSE (events sent by server) with PHP
header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); while(true){ echo "id: ".time()."\ndata: ".ReadTheLogs()."\n\n"; ob_flush(); flush(); sleep(1); }
In this case, after establishing a connection with the first user, the connection remains open (PHP has not been created for this), so I save some space (request headers, response headers). This work on my server in most servers does not allow me to stay open for a long time.
Also with several users, I read the log several times (slowing down my old server) And I can not control the server ... I will need to use ajax to send the command ...
I need WebSockets !!!
node.js and websockets
using node.js, from what I understand, I can do all this without consuming a lot of resources and bandish. The connection remains open, so there are no unnecessary headers, I can receive and send data.it handles several users very well.
And here I need your help.
the node.js server should be in the background update and store log data every 5 seconds if the files are modified. OR should do the operating system with (iwatch, dnotify ...)
data should only be pressed when changing.
reading logs should occur only once after 5 seconds ... therefore, each user does not start.
this is the first example i found ..... and modified ..
var ws=require("nodejs-websocket"); var server=ws.createServer(function(conn){ var data=read(whereToStoreTheLogs); conn.sendText(data)
the above script will be a notification server, but I also need to find a solution to store somwhere information of these several logs and do it every time something changes in the background.
How would you do to support low-user as well as server resources.
How would you do?
EDIT Btw. Is there a way to stream use this data simultaneously in all clinics?
EDIT
About logs: I also want to be able to scale the time extension between updates ... I mean, if I read the ffmpeg logs, which are updated every second, if possible ... but when the conversion is not active. I need to get basic information about the car every 5 minutes, maybe ... and so on ...
OBJECTIVES: 1. a way to read and store log data (only if the clinics are connected ... [mysql, file, it is possible to save this information inside RAM (using node.js ??)]). 2. An effective way to transfer data to different clients (at the same time). 3. be able to send commands to the server .. (bi-directional) 4. use of web languages โโ(js, php ...), lunix commands (something that is easy to implement on multiple machines) .. if necessary, free software.
best approach:
read the logs based on current activity , into the system memory and stream simultaneously and constantly , with an already open connection to various clients with websites .
I do not know anything that could be faster.
UPDATE
node.js server is up and running using http://einaros.imtqy.com/ws/ webSocketServer as it seems to be the fastest. I wrote the following code with @HeadCode to properly handle the client situation and keep the process as low as possible. checking out various things inside the broadcast cycle. Now pushing and handling the client are at a good point.
var wss=new (require('ws').Server)({port:8080}), isBusy, logs, clients, i, checkLogs=function(){ if(wss.clients&&(clients=wss.clients.length)){ isBusy||(logs=readLogs()); if(logs){ i=0; while(i<clients){ wss.clients[i++].send(logs) } } } }; setInterval(checkLogs,2000);
But atm I use a really bad way to parse logs .. (nodejs-> httpRequest-> php) .. lol. After some googling, I found out that I can completely transfer linux software output directly to the nodejs application ... I did not check ... but maybe this would be the best way to do this. node.js also has an api file system where icould read logs. linux has its own api file system.
the readLogs () function (may be asynchronous) is still not satisfied.
- nodejs file system?
- linuxSoftware-> nodejs implementation
- linux api file system.
Keep in mind that I need to scan various folders for magazines, and then analyze somehow the output data, and this is every 2 seconds.
ps .: I adde isBusy for server variables if the logReading system is asynchronous.
EDIT
The answer is not complete.
Missing:
- An easy way to read, parse and store logs somewhere (linux api or nodejs api file system, so I store directly in system memory)
- An explanation of whether it is possible to transfer data directly to multiple users. apparently nodejs loops through clients and therefore (i think) sending data several times.
btw is possible / worth closing the node server if there are no clients and restarting new connections on the apache side. (for example: if I connect to the html apache file and the script starts the nodejs server again). will this further reduce memory leak? correctly?
EDIT
After some experimenting with websockets (some videos in the comments), I learned some new things. Raspberry PI has the ability to use some of the processor's DMA channels for high-frequency broadcasting, such as PWM ... I need to somehow understand how this works.
When using sensors and similar things, I have to store everything in RAM, is nodejs already doing this? (in the variable inside the script)
websocket remains the best choice as it is basically easily accessible from any device now, just using a browser.