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 callback mode. For details about the APIs, see [sms API Reference](../reference/apis/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/js-apis-net-statistics.md). 21 22| Type | API | Description | 23| ------------------- | ------------------------------------------------------------------------------------------- | ------------------------------ | 24| ohos.net.statistics | getIfaceRxBytes(nic: string, callback: AsyncCallback\<number>): void; | Obtains the real-time downlink data traffic of the specified NIC. | 25| ohos.net.statistics | getIfaceTxBytes(nic: string, callback: AsyncCallback\<number>): void; | Obtains the real-time uplink data traffic of the specified NIC. | 26| ohos.net.statistics | getCellularRxBytes(callback: AsyncCallback\<number>): void; | Obtains the real-time downlink data traffic of the cellular network. | 27| ohos.net.statistics | getCellularTxBytes(callback: AsyncCallback\<number>): void; | Obtains the real-time uplink data traffic of the cellular network. | 28| ohos.net.statistics | getAllRxBytes(callback: AsyncCallback\<number>): void; | Obtains the real-time downlink data traffic of the all NICs. | 29| ohos.net.statistics | getAllTxBytes(callback: AsyncCallback\<number>): void; | Obtains the real-time uplink data traffic of the all NICs. | 30| ohos.net.statistics | getUidRxBytes(uid: number, callback: AsyncCallback\<number>): void; | Obtains the real-time downlink data traffic of the specified application. | 31| ohos.net.statistics | getUidTxBytes(uid: number, callback: AsyncCallback\<number>): void; | Obtains the real-time uplink data traffic of the specified application. | 32| ohos.net.statistics | getTrafficStatsByIface(ifaceInfo: IfaceInfo, callback: AsyncCallback\<NetStatsInfo>): void; | Obtains the historical data traffic of the specified NIC. | 33| ohos.net.statistics | getTrafficStatsByUid(uidInfo: UidInfo, callback: AsyncCallback\<NetStatsInfo>): void; | Obtains the historical data traffic of the specified application. | 34| ohos.net.statistics | on(type: 'netStatsChange', callback: Callback\<{ iface: string, uid?: number }>): void | Subscribes to traffic change events. | 35| ohos.net.statistics | off(type: 'netStatsChange', callback?: Callback\<{ iface: string, uid?: number }>): void; | Unsubscribes from traffic change events. | 36 37## Obtaining Real-Time Traffic Data by NIC or UID 38 391. Obtain the real-time data traffic of the specified NIC. 402. Obtain the real-time data traffic of the cellular network. 413. Obtain the real-time data traffic of all NICs. 424. Obtain the real-time data traffic of the specified application. 43 44```ts 45// Import the statistics namespace from @ohos.net.statistics. 46import statistics from '@ohos.net.statistics'; 47import { BusinessError } from '@ohos.base'; 48 49// Obtain the real-time downlink data traffic of the specified NIC. 50statistics.getIfaceRxBytes('wlan0', (error: BusinessError, stats: number) => { 51 console.log(JSON.stringify(error)); 52 console.log(JSON.stringify(stats)); 53}); 54 55// Obtain the real-time uplink data traffic of the specified NIC. 56statistics.getIfaceTxBytes('wlan0', (error: BusinessError, stats: number) => { 57 console.log(JSON.stringify(error)); 58 console.log(JSON.stringify(stats)); 59}); 60 61// Obtain the real-time downlink data traffic of the cellular network. 62statistics.getCellularRxBytes((error: BusinessError, stats: number) => { 63 console.log(JSON.stringify(error)); 64 console.log(JSON.stringify(stats)); 65}); 66 67// Obtain the real-time uplink data traffic of the cellular network. 68statistics.getCellularTxBytes((error: BusinessError, stats: number) => { 69 console.log(JSON.stringify(error)); 70 console.log(JSON.stringify(stats)); 71}); 72 73// Obtain the real-time downlink data traffic of the all NICs. 74statistics.getAllRxBytes((error: BusinessError, stats: number) => { 75 console.log(JSON.stringify(error)); 76 console.log(JSON.stringify(stats)); 77}); 78 79// Obtain the real-time uplink data traffic of the all NICs. 80statistics.getAllTxBytes((error: BusinessError, stats: number) => { 81 console.log(JSON.stringify(error)); 82 console.log(JSON.stringify(stats)); 83}); 84 85// Obtain the real-time downlink data traffic of the specified application. 86let uid = 20010038; 87statistics.getUidRxBytes(uid, (error: BusinessError, stats: number) => { 88 console.log(JSON.stringify(error)); 89 console.log(JSON.stringify(stats)); 90}); 91 92// Obtain the real-time uplink data traffic of the specified application. 93let uids = 20010038; 94statistics.getUidTxBytes(uids, (error: BusinessError, stats: number) => { 95 console.log(JSON.stringify(error)); 96 console.log(JSON.stringify(stats)); 97}); 98``` 99 100## Obtaining Historical Traffic Data by NIC or UID 101 1021. Obtain the historical data traffic of the specified NIC. 1032. Obtain the historical data traffic of the specified application. 104 105```ts 106import statistics from '@ohos.net.statistics'; 107import { BusinessError } from '@ohos.base'; 108 109class IfaceInfo { 110 iface: string = "wlan0" 111 startTime: number = 1685948465 112 endTime: number = 16859485670 113} 114// Obtain the historical data traffic of the specified NIC. 115statistics.getTrafficStatsByIface(new IfaceInfo(), (error: BusinessError, statsInfo: statistics.NetStatsInfo) => { 116 console.log(JSON.stringify(error)) 117 console.log("getTrafficStatsByIface bytes of received = " + JSON.stringify(statsInfo.rxBytes)); 118 console.log("getTrafficStatsByIface bytes of sent = " + JSON.stringify(statsInfo.txBytes)); 119 console.log("getTrafficStatsByIface packets of received = " + JSON.stringify(statsInfo.rxPackets)); 120 console.log("getTrafficStatsByIface packets of sent = " + JSON.stringify(statsInfo.txPackets)); 121}); 122 123class UidInfo { 124 uid: number = 20010037 125 ifaceInfo: IfaceInfo = new IfaceInfo() 126} 127 128let uidInfo = new UidInfo() 129 130// Obtain the historical data traffic of the specified application. 131statistics.getTrafficStatsByUid(uidInfo, (error: BusinessError, statsInfo: statistics.NetStatsInfo) => { 132 console.log(JSON.stringify(error)) 133 console.log("getTrafficStatsByUid bytes of received = " + JSON.stringify(statsInfo.rxBytes)); 134 console.log("getTrafficStatsByUid bytes of sent = " + JSON.stringify(statsInfo.txBytes)); 135 console.log("getTrafficStatsByUid packets of received = " + JSON.stringify(statsInfo.rxPackets)); 136 console.log("getTrafficStatsByUid packets of sent = " + JSON.stringify(statsInfo.txPackets)); 137}); 138``` 139 140## Subscribing to Traffic Change Events 141 1421. Subscribe to traffic change events. 1432. Unsubscribe from traffic change events. 144 145```ts 146import statistics from '@ohos.net.statistics'; 147class Data { 148 iface: string = "" 149 uid?: number = 0 150} 151 152let callback = (data: Data) => { 153 console.log('on netStatsChange, data:' + JSON.stringify(data)); 154}; 155// Subscribe to traffic change events. 156statistics.on('netStatsChange', callback); 157 158// 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. 159statistics.off('netStatsChange', callback); 160statistics.off('netStatsChange'); 161``` 162