1e41f4b71Sopenharmony_ci# Traffic Management
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Introduction
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciThe traffic management module allows you to query real-time or historical data traffic by the specified network interface card (NIC) or user ID (UID).
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciIts functions include:
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci- Obtaining real-time traffic data by NIC or UID
10e41f4b71Sopenharmony_ci- Obtaining historical traffic data by NIC or UID
11e41f4b71Sopenharmony_ci- Subscribing to traffic change events by NIC or UID
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci> **NOTE**
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci> 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).
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciThe following describes the development procedure specific to each application scenario.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci## Available APIs
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ciFor the complete list of APIs and example code, see [Traffic Management](../reference/apis-network-kit/js-apis-net-statistics.md).
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci| API                                                                                                            | Description                                                                                                  |
24e41f4b71Sopenharmony_ci|-----------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------|
25e41f4b71Sopenharmony_ci| getIfaceRxBytes(nic: string, callback: AsyncCallback\<number>): void;                                           | Obtains the real-time downlink data traffic of the specified NIC.                                                                                      |
26e41f4b71Sopenharmony_ci| getIfaceTxBytes(nic: string, callback: AsyncCallback\<number>): void;                                           | Obtains the real-time uplink data traffic of the specified NIC.                                                                                      |
27e41f4b71Sopenharmony_ci| getCellularRxBytes(callback: AsyncCallback\<number>): void;                                                     | Obtains the real-time downlink data traffic of the cellular network.                                                                                       |
28e41f4b71Sopenharmony_ci| getCellularTxBytes(callback: AsyncCallback\<number>): void;                                                     | Obtains the real-time uplink data traffic of the cellular network.                                                                                       |
29e41f4b71Sopenharmony_ci| getAllRxBytes(callback: AsyncCallback\<number>): void;                                                          | Obtains the real-time downlink data traffic of the all NICs.                                                                                      |
30e41f4b71Sopenharmony_ci| getAllTxBytes(callback: AsyncCallback\<number>): void;                                                          | Obtains the real-time uplink data traffic of the all NICs.                                                                                      |
31e41f4b71Sopenharmony_ci| getUidRxBytes(uid: number, callback: AsyncCallback\<number>): void;                                             | Obtains the real-time downlink data traffic of the specified application.                                                                                      |
32e41f4b71Sopenharmony_ci| getUidTxBytes(uid: number, callback: AsyncCallback\<number>): void;                                             | Obtains the real-time uplink data traffic of the specified application.                                                                                      |
33e41f4b71Sopenharmony_ci| <!--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).             |
34e41f4b71Sopenharmony_ci| <!--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).             |
35e41f4b71Sopenharmony_ci| getSockfdRxBytes(sockfd: number, callback: AsyncCallback\<number>): void;                                       | Obtains the real-time downlink data traffic of the specified socket.                                                                                  |
36e41f4b71Sopenharmony_ci| getSockfdTxBytes(sockfd: number, callback: AsyncCallback\<number>): void;                                       | Obtains the real-time uplink data traffic of the specified socket.                                                                                  |
37e41f4b71Sopenharmony_ci| <!--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).               |
38e41f4b71Sopenharmony_ci| <!--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).             |
39e41f4b71Sopenharmony_ci| <!--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).|
40e41f4b71Sopenharmony_ci| <!--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). |
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci## Obtaining Real-Time Traffic Data by NIC or UID
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci1. Obtain the real-time data traffic of the specified NIC. 
45e41f4b71Sopenharmony_ci2. Obtain the real-time data traffic of the cellular network.
46e41f4b71Sopenharmony_ci3. Obtain the real-time data traffic of all NICs.
47e41f4b71Sopenharmony_ci4. Obtain the real-time data traffic of the specified application. 
48e41f4b71Sopenharmony_ci5. Obtains the real-time data traffic of the specified socket.
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci```ts
51e41f4b71Sopenharmony_ci// Import the statistics namespace from @kit.NetworkKit.
52e41f4b71Sopenharmony_ciimport { statistics, socket } from '@kit.NetworkKit';
53e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci// Obtain the real-time downlink data traffic of the specified NIC. 
56e41f4b71Sopenharmony_cistatistics.getIfaceRxBytes("wlan0").then((stats: number) => {
57e41f4b71Sopenharmony_ci  console.log(JSON.stringify(stats));
58e41f4b71Sopenharmony_ci});
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci// Obtain the real-time uplink data traffic of the specified NIC. 
61e41f4b71Sopenharmony_cistatistics.getIfaceTxBytes("wlan0").then((stats: number) => {
62e41f4b71Sopenharmony_ci  console.log(JSON.stringify(stats));
63e41f4b71Sopenharmony_ci});
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci// Obtain the real-time downlink data traffic of the cellular network.
66e41f4b71Sopenharmony_cistatistics.getCellularRxBytes().then((stats: number) => {
67e41f4b71Sopenharmony_ci  console.log(JSON.stringify(stats));
68e41f4b71Sopenharmony_ci});
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci// Obtain the real-time uplink data traffic of the cellular network.
71e41f4b71Sopenharmony_cistatistics.getCellularTxBytes().then((stats: number) => {
72e41f4b71Sopenharmony_ci  console.log(JSON.stringify(stats));
73e41f4b71Sopenharmony_ci});
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci// Obtain the real-time downlink data traffic of the all NICs. 
76e41f4b71Sopenharmony_cistatistics.getAllRxBytes().then((stats: number) => {
77e41f4b71Sopenharmony_ci  console.log(JSON.stringify(stats));
78e41f4b71Sopenharmony_ci});
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci// Obtain the real-time uplink data traffic of the all NICs. 
81e41f4b71Sopenharmony_cistatistics.getAllTxBytes().then((stats: number) => {
82e41f4b71Sopenharmony_ci  console.log(JSON.stringify(stats));
83e41f4b71Sopenharmony_ci});
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci// Obtain the real-time downlink data traffic of the specified application. 
86e41f4b71Sopenharmony_cilet uid = 20010038;
87e41f4b71Sopenharmony_cistatistics.getUidRxBytes(uid).then((stats: number) => {
88e41f4b71Sopenharmony_ci  console.log(JSON.stringify(stats));
89e41f4b71Sopenharmony_ci});
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci// Obtain the real-time uplink data traffic of the specified application. 
92e41f4b71Sopenharmony_cilet uids = 20010038;
93e41f4b71Sopenharmony_cistatistics.getUidTxBytes(uids).then((stats: number) => {
94e41f4b71Sopenharmony_ci  console.log(JSON.stringify(stats));
95e41f4b71Sopenharmony_ci});
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci// Obtain the real-time downlink data traffic of the specified socket. 
98e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
99e41f4b71Sopenharmony_citcp.getSocketFd().then((sockfd: number) => {
100e41f4b71Sopenharmony_ci  statistics.getSockfdRxBytes(sockfd).then((stats: number) => {
101e41f4b71Sopenharmony_ci    console.log(JSON.stringify(stats));
102e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
103e41f4b71Sopenharmony_ci    console.error(JSON.stringify(err));
104e41f4b71Sopenharmony_ci  });
105e41f4b71Sopenharmony_ci});
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci// Obtain the real-time uplink data traffic of the specified socket. 
108e41f4b71Sopenharmony_citcp.getSocketFd().then((sockfd: number) => {
109e41f4b71Sopenharmony_ci  statistics.getSockfdTxBytes(sockfd).then((stats: number) => {
110e41f4b71Sopenharmony_ci    console.log(JSON.stringify(stats));
111e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
112e41f4b71Sopenharmony_ci    console.error(JSON.stringify(err));
113e41f4b71Sopenharmony_ci  });
114e41f4b71Sopenharmony_ci});
115e41f4b71Sopenharmony_ci```
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci<!--Del-->
118e41f4b71Sopenharmony_ci## Obtaining Historical Traffic Data by NIC or UID
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci1. Obtain the historical data traffic of the specified NIC. 
121e41f4b71Sopenharmony_ci2. Obtain the historical data traffic of the specified application. 
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci```ts
124e41f4b71Sopenharmony_ciimport { statistics } from '@kit.NetworkKit';
125e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ciclass IfaceInfo {
128e41f4b71Sopenharmony_ci  iface: string = "wlan0"
129e41f4b71Sopenharmony_ci  startTime: number = 1685948465
130e41f4b71Sopenharmony_ci  endTime: number = 16859485670
131e41f4b71Sopenharmony_ci}
132e41f4b71Sopenharmony_ci// Obtain the historical data traffic of the specified NIC. 
133e41f4b71Sopenharmony_cistatistics.getTrafficStatsByIface(new IfaceInfo()).then((statsInfo: statistics.NetStatsInfo) => {
134e41f4b71Sopenharmony_ci  console.log(
135e41f4b71Sopenharmony_ci    "getTrafficStatsByIface bytes of received = " +
136e41f4b71Sopenharmony_ci    JSON.stringify(statsInfo.rxBytes)
137e41f4b71Sopenharmony_ci  );
138e41f4b71Sopenharmony_ci  console.log(
139e41f4b71Sopenharmony_ci    "getTrafficStatsByIface bytes of sent = " +
140e41f4b71Sopenharmony_ci    JSON.stringify(statsInfo.txBytes)
141e41f4b71Sopenharmony_ci  );
142e41f4b71Sopenharmony_ci  console.log(
143e41f4b71Sopenharmony_ci    "getTrafficStatsByIface packets of received = " +
144e41f4b71Sopenharmony_ci    JSON.stringify(statsInfo.rxPackets)
145e41f4b71Sopenharmony_ci  );
146e41f4b71Sopenharmony_ci  console.log(
147e41f4b71Sopenharmony_ci    "getTrafficStatsByIface packets of sent = " +
148e41f4b71Sopenharmony_ci    JSON.stringify(statsInfo.txPackets)
149e41f4b71Sopenharmony_ci  );
150e41f4b71Sopenharmony_ci});
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ciclass UidInfo {
153e41f4b71Sopenharmony_ci  uid: number = 20010037
154e41f4b71Sopenharmony_ci  ifaceInfo: IfaceInfo = new IfaceInfo()
155e41f4b71Sopenharmony_ci}
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_cilet uidInfo = new UidInfo()
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci// Obtain the historical data traffic of the specified application. 
160e41f4b71Sopenharmony_cistatistics.getTrafficStatsByUid(uidInfo).then((statsInfo: statistics.NetStatsInfo) => {
161e41f4b71Sopenharmony_ci  console.log("getTrafficStatsByUid bytes of received = " + JSON.stringify(statsInfo.rxBytes));
162e41f4b71Sopenharmony_ci  console.log("getTrafficStatsByUid bytes of sent = " + JSON.stringify(statsInfo.txBytes));
163e41f4b71Sopenharmony_ci  console.log("getTrafficStatsByUid packets of received = " + JSON.stringify(statsInfo.rxPackets));
164e41f4b71Sopenharmony_ci  console.log("getTrafficStatsByUid packets of sent = " + JSON.stringify(statsInfo.txPackets));
165e41f4b71Sopenharmony_ci})
166e41f4b71Sopenharmony_ci```
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci## Subscribing to Traffic Change Events
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci1. Subscribe to traffic change events.
171e41f4b71Sopenharmony_ci2. Unsubscribe from traffic change events.
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ci```ts
174e41f4b71Sopenharmony_ciimport { statistics } from '@kit.NetworkKit';
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ciclass Data {
177e41f4b71Sopenharmony_ci  iface: string = ""
178e41f4b71Sopenharmony_ci  uid?: number = 0
179e41f4b71Sopenharmony_ci}
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_cilet callback = (data: Data) => {
182e41f4b71Sopenharmony_ci  console.log('on netStatsChange, data:' + JSON.stringify(data));
183e41f4b71Sopenharmony_ci};
184e41f4b71Sopenharmony_ci// Subscribe to traffic change events.
185e41f4b71Sopenharmony_cistatistics.on('netStatsChange', callback);
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci// 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.
188e41f4b71Sopenharmony_cistatistics.off('netStatsChange', callback);
189e41f4b71Sopenharmony_cistatistics.off('netStatsChange');
190e41f4b71Sopenharmony_ci```
191e41f4b71Sopenharmony_ci<!--DelEnd-->
192