Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure bandwidth used by web socket in React Native app

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.com/path');

ws.onopen = () => {
  ws.send('something');
};
like image 464
jskidd3 Avatar asked Nov 22 '18 17:11

jskidd3


People also ask

When using WebSocket send () How do you know?

The only way to know the client received the webSocket message for sure is to have the client send your own custom message back to the server to indicate you received it and for you to wait for that message on the server.

How do you test a WebSocket in react?

WebSocket = class extends WebSocket { constructor(url) { super("wss://test"); global. sendMsg = null } addEventListener(event, cb) { if (event === "open") { cb(); } else if(event === "message") { global. sendMsg = cb; } } }; test("testing message",() => { render(<LiveChat />); global.

Does react native support WebSockets?

React Native also supports WebSockets, a protocol which provides full-duplex communication channels over a single TCP connection.

How do I use a WebSocket in react native?

WebSockets work on their own protocol, ws:// . In React Native, we can create a connection using the following code: var ws = new WebSocket('ws://host.com/path'); Here the link corresponds to the socket service running on the backend.


1 Answers

Assuming that you can identify a client session by unique IP (just the session, they do not always need this IP), I would recommend 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:

  1. Record the time, user id and their IP address at the beginning of a session
  2. 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).

like image 168
Liam Kelly Avatar answered Oct 10 '22 03:10

Liam Kelly