In our application, staff use their phones to log activities within a business. They end up using 0.5GB-2GB data per month on average.
I'm trying to build functionality into our app that logs data usage so that we can send it back to the business in the form of an expense claim.
In the example code below, how can I determine how much bandwidth/data was used by the device sending the message over a WebSocket
?
var ws = new WebSocket('ws://host/path');
ws.onopen = () => {
ws.send('something');
};
In our application, staff use their phones to log activities within a business. They end up using 0.5GB-2GB data per month on average.
I'm trying to build functionality into our app that logs data usage so that we can send it back to the business in the form of an expense claim.
In the example code below, how can I determine how much bandwidth/data was used by the device sending the message over a WebSocket
?
var ws = new WebSocket('ws://host./path');
ws.onopen = () => {
ws.send('something');
};
Share
Improve this question
asked Nov 22, 2018 at 17:23
jskidd3jskidd3
4,78315 gold badges66 silver badges131 bronze badges
2 Answers
Reset to default 7 +25Assuming that you can identify a client session by unique IP (just the session, they do not always need this IP), I would remend leveraging lower level tools that are more suited for your application, specifically NetFlow collectors.
NetFlow measures TCP 'conversation', via recording IP src, dst and throughput over a time slice. You can enable this in a Linux kernel or directly in some networking equipment. You will then need a program to collect and store the data.
Assuming you have NetFlow collection enabled and can identify sessions by IP, you can do the following:
- Record the time, user id and their IP address at the beginning of a session
- Using this data you can then query your NetFlow logs and get the throughput
The reason I suggest this instead of some sort of userspace solution that might count bytes received (which you could probably do fairly easily) is because there is a lot of data being abstracted by libraries and the kernel. The kernel handles the TCP stack (including re-sending missed packets), libraries handle the TLS handshakes/encryption and also the WebSocket Handshake. All this data is counted toward the user's used data. How users use the app will effect how much of this overhead data is sent (constantly opening/closing it vs leaving it open).
Depends on what precision you need. Simplest way would be to "subclass" existing sockets by something like this:
var inboundTraffic = 0;
var outboundTraffic = 0;
function NewWebSocket(addr) {
var ws = new WebSocket(addr);
var wsSend = ws.send;
ws.send = function(data) {
outboundTraffic += data.length;
return wsSend.call(ws,data);
}
ws.addEventListener("message", function (event) {
inboundTraffic += event.data.length;
});
return ws;
}
Simple and costs pretty much nothing.