How to get network statics like Rx/Tx bytes(/packets) in node.js

I am developing a video streaming app in node.js. As it might consume a lot of network bandwidth, I want to find out how can I in node.js track the bandwidth usage of the application as well as a whole the system itself?

Example of desired output:

This App: 100 kb Rx 18 mb Tx

Total: 200 kb Rx 20 mb Tx

Embrace yourself, as it is not going to be any easy.

Only nodejs module which supposedly tracks network traffic I found is network-debugger. I have little faith you will find an answer to your question in that project though.

Network monitoring is usually highly OS specific:

  • Windows: Only option is the WinPCap
  • MacOS: nettop. Few linux network utilities ports might work too.
  • Linux: you are lucky man. Lately nethogs has been the most popular network top utility and is available in most distributors through their package managers. Please have a look at this comparison article to learn more about linux network tools.

Yes, you are right, it is not a nodejs library, but as you guessed there is not one. Partially this might be because sys admins always preferred console based one-thing tools to monitor their servers, as a result there is a very little demand to implement network monitoring library in node.


To achieve the desired:

  1. Choose one of the available network tools.
  2. Play around with it, see if it is what you want (they are different, so make sure to try few).
  3. If you are liking the information it gives, learn how to output it into a pipe-friendly format.
  4. Now you need your node.js server to launch the tool in that pipe mode and catch the output.
  5. Done.

There is an interesting article in great detail on how to code a system monitor in node.js using existing console based tools, might be of a great help. you might have to choose one of those console based tools, learn how to launch it in pipe-friendly mode (so that the output can be piped to a file or a stream right into your node.js server)


Alternatively, you could reinvent the wheel and make your own network monitoring tool in node.js, but I fear it will end up being half JavaScript, half OS specific code in C or Objective-C.

Also note that you need adminsitrative rights (sudo or admin user on windows) to monitor the network. Meaning you might have to run node.js with elevated rights. That is an extra risk to keep in mind.