1# Traffic Management
2
3## Introduction
4
5The traffic management module allows you to query real-time or historical data traffic by the specified network interface card (NIC) or user ID (UID).
6
7Its functions include:
8
9- Obtaining real-time traffic data by NIC or UID
10- Obtaining historical traffic data by NIC or UID
11- Subscribing to traffic change events by NIC or UID
12
13> **NOTE**
14> To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the promise mode. For details about the APIs, see [API Reference](../reference/apis-network-kit/js-apis-net-statistics.md).
15
16The following describes the development procedure specific to each application scenario.
17
18## Available APIs
19
20For the complete list of APIs and example code, see [Traffic Management](../reference/apis-network-kit/js-apis-net-statistics.md).
21
22| API                                                      | Description                                                        |
23| ------------------------------------------------------------ | ------------------------------------------------------------ |
24| getIfaceRxBytes(nic: string, callback: AsyncCallback\<number>): void; | Obtains the real-time downlink data traffic of the specified NIC.                               |
25| getIfaceTxBytes(nic: string, callback: AsyncCallback\<number>): void; | Obtains the real-time uplink data traffic of the specified NIC.                               |
26| getCellularRxBytes(callback: AsyncCallback\<number>): void;  | Obtains the real-time downlink data traffic of the cellular network.                                  |
27| getCellularTxBytes(callback: AsyncCallback\<number>): void;  | Obtains the real-time uplink data traffic of the cellular network.                                  |
28| getAllRxBytes(callback: AsyncCallback\<number>): void;       | Obtains the real-time downlink data traffic of the all NICs.                               |
29| getAllTxBytes(callback: AsyncCallback\<number>): void;       | Obtains the real-time uplink data traffic of the all NICs.                               |
30| getUidRxBytes(uid: number, callback: AsyncCallback\<number>): void; | Obtains the real-time downlink data traffic of the specified application.                               |
31| getUidTxBytes(uid: number, callback: AsyncCallback\<number>): void; | Obtains the real-time uplink data traffic of the specified application.                               |
32| <!--DelRow-->getTrafficStatsByIface(ifaceInfo: IfaceInfo, callback: AsyncCallback\<NetStatsInfo>): void; | Obtains the historical data traffic of the specified NIC. This is a system API. For details, see [API Reference](../reference/apis-network-kit/js-apis-net-statistics-sys.md).|
33| <!--DelRow-->getTrafficStatsByUid(uidInfo: UidInfo, callback: AsyncCallback\<NetStatsInfo>): void; | Obtains the historical data traffic of the specified application. This is a system API. For details, see [API Reference](../reference/apis-network-kit/js-apis-net-statistics-sys.md).|
34| getSockfdRxBytes(sockfd: number, callback: AsyncCallback\<number>): void; | Obtains the real-time downlink data traffic of the specified socket.                             |
35| getSockfdTxBytes(sockfd: number, callback: AsyncCallback\<number>): void; | Obtains the real-time uplink data traffic of the specified socket.                             |
36| <!--DelRow-->on(type: 'netStatsChange', callback: Callback\<{ iface: string, uid?: number }>): void; | Subscribes to traffic change events. This is a system API. For details, see [API Reference](../reference/apis-network-kit/js-apis-net-statistics-sys.md).|
37| <!--DelRow-->off(type: 'netStatsChange', callback?: Callback\<{ iface: string, uid?: number }>): void; | Unsubscribes from traffic change events. This is a system API. For details, see [API Reference](../reference/apis-network-kit/js-apis-net-statistics-sys.md).|
38| <!--DelRow-->getTrafficStatsByNetwork(networkInfo: NetworkInfo): Promise\<UidNetStatsInfo\>; | Obtains the traffic statistics of all applications on the specified network within the specified period. This is a system API. For details, see [API Reference](../reference/apis-network-kit/js-apis-net-statistics-sys.md).|
39| <!--DelRow-->getTrafficStatsByUidNetwork(uid: number, networkInfo: NetworkInfo): Promise\<NetStatsInfoSequence\>; | Obtains the traffic statistics of the specified application on the specified network within the specified period. This is a system API. For details, see [API Reference](../reference/apis-network-kit/js-apis-net-statistics-sys.md).|
40
41## Obtaining Real-Time Traffic Data by NIC or UID
42
431. Obtain the real-time data traffic of the specified NIC.
442. Obtain the real-time data traffic of the cellular network.
453. Obtain the real-time data traffic of all NICs.
464. Obtain the real-time data traffic of the specified application.
475. Obtains the real-time data traffic of the specified socket.
48
49```ts
50// Import the statistics namespace from @kit.NetworkKit.
51import { statistics, socket } from '@kit.NetworkKit';
52import { BusinessError } from '@kit.BasicServicesKit';
53
54// Obtain the real-time downlink data traffic of the specified NIC.
55statistics.getIfaceRxBytes("wlan0").then((stats: number) => {
56  console.log(JSON.stringify(stats));
57});
58
59// Obtain the real-time uplink data traffic of the specified NIC.
60statistics.getIfaceTxBytes("wlan0").then((stats: number) => {
61  console.log(JSON.stringify(stats));
62});
63
64// Obtain the real-time downlink data traffic of the cellular network.
65statistics.getCellularRxBytes().then((stats: number) => {
66  console.log(JSON.stringify(stats));
67});
68
69// Obtain the real-time uplink data traffic of the cellular network.
70statistics.getCellularTxBytes().then((stats: number) => {
71  console.log(JSON.stringify(stats));
72});
73
74// Obtain the real-time downlink data traffic of the all NICs.
75statistics.getAllRxBytes().then((stats: number) => {
76  console.log(JSON.stringify(stats));
77});
78
79// Obtain the real-time uplink data traffic of the all NICs.
80statistics.getAllTxBytes().then((stats: number) => {
81  console.log(JSON.stringify(stats));
82});
83
84// Obtain the real-time downlink data traffic of the specified application.
85let uid = 20010038;
86statistics.getUidRxBytes(uid).then((stats: number) => {
87  console.log(JSON.stringify(stats));
88});
89
90// Obtain the real-time uplink data traffic of the specified application.
91let uids = 20010038;
92statistics.getUidTxBytes(uids).then((stats: number) => {
93  console.log(JSON.stringify(stats));
94});
95
96// Obtain the real-time downlink data traffic of the specified socket.
97let tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
98tcp.getSocketFd().then((sockfd: number) => {
99  statistics.getSockfdRxBytes(sockfd).then((stats: number) => {
100    console.log(JSON.stringify(stats));
101  }).catch((err: BusinessError) => {
102    console.error(JSON.stringify(err));
103  });
104});
105
106// Obtain the real-time uplink data traffic of the specified socket.
107tcp.getSocketFd().then((sockfd: number) => {
108  statistics.getSockfdTxBytes(sockfd).then((stats: number) => {
109    console.log(JSON.stringify(stats));
110  }).catch((err: BusinessError) => {
111    console.error(JSON.stringify(err));
112  });
113});
114```
115
116<!--Del-->
117## Obtaining Historical Traffic Data by NIC or UID
118
1191. Obtain the historical data traffic of the specified NIC.
1202. Obtain the historical data traffic of the specified application.
121
122```ts
123import { statistics } from '@kit.NetworkKit';
124import { BusinessError } from '@kit.BasicServicesKit';
125
126class IfaceInfo {
127  iface: string = "wlan0"
128  startTime: number = 1685948465
129  endTime: number = 16859485670
130}
131// Obtain the historical data traffic of the specified NIC.
132statistics.getTrafficStatsByIface(new IfaceInfo()).then((statsInfo: statistics.NetStatsInfo) => {
133  console.log(
134    "getTrafficStatsByIface bytes of received = " +
135    JSON.stringify(statsInfo.rxBytes)
136  );
137  console.log(
138    "getTrafficStatsByIface bytes of sent = " +
139    JSON.stringify(statsInfo.txBytes)
140  );
141  console.log(
142    "getTrafficStatsByIface packets of received = " +
143    JSON.stringify(statsInfo.rxPackets)
144  );
145  console.log(
146    "getTrafficStatsByIface packets of sent = " +
147    JSON.stringify(statsInfo.txPackets)
148  );
149});
150
151class UidInfo {
152  uid: number = 20010037
153  ifaceInfo: IfaceInfo = new IfaceInfo()
154}
155
156let uidInfo = new UidInfo()
157
158// Obtain the historical data traffic of the specified application.
159statistics.getTrafficStatsByUid(uidInfo).then((statsInfo: statistics.NetStatsInfo) => {
160  console.log("getTrafficStatsByUid bytes of received = " + JSON.stringify(statsInfo.rxBytes));
161  console.log("getTrafficStatsByUid bytes of sent = " + JSON.stringify(statsInfo.txBytes));
162  console.log("getTrafficStatsByUid packets of received = " + JSON.stringify(statsInfo.rxPackets));
163  console.log("getTrafficStatsByUid packets of sent = " + JSON.stringify(statsInfo.txPackets));
164})
165```
166
167## Subscribing to Traffic Change Events
168
1691. Subscribe to traffic change events.
1702. Unsubscribe from traffic change events.
171
172```ts
173import { statistics } from '@kit.NetworkKit';
174
175class Data {
176  iface: string = ""
177  uid?: number = 0
178}
179
180let callback = (data: Data) => {
181  console.log('on netStatsChange, data:' + JSON.stringify(data));
182};
183// Subscribe to traffic change events.
184statistics.on('netStatsChange', callback);
185
186// Unsubscribe from traffic change events. You can pass the callback of the **on** function if you want to unsubscribe from a certain type of event. If you do not pass the callback, you will unsubscribe from all events.
187statistics.off('netStatsChange', callback);
188statistics.off('netStatsChange');
189```
190<!--DelEnd-->
191