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| 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| getTrafficStatsByIface(ifaceInfo: IfaceInfo, callback: AsyncCallback\<NetStatsInfo>): void; | Obtains the historical data traffic of the specified NIC. | 33| getTrafficStatsByUid(uidInfo: UidInfo, callback: AsyncCallback\<NetStatsInfo>): void; | Obtains the historical data traffic of the specified application. | 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| on(type: 'netStatsChange', callback: Callback\<{ iface: string, uid?: number }>): void | Subscribes to traffic change events. | 37| off(type: 'netStatsChange', callback?: Callback\<{ iface: string, uid?: number }>): void; | Unsubscribes from traffic change events. | 38 39## Obtaining Real-Time Traffic Data by NIC or UID 40 411. Obtain the real-time data traffic of the specified NIC. 422. Obtain the real-time data traffic of the cellular network. 433. Obtain the real-time data traffic of all NICs. 444. Obtain the real-time data traffic of the specified application. 455. Obtains the real-time data traffic of the specified socket. 46 47```ts 48// Import the statistics namespace from @ohos.net.statistics. 49import statistics from '@ohos.net.statistics'; 50import { BusinessError } from '@ohos.base'; 51import socket from "@ohos.net.socket"; 52 53// Obtain the real-time downlink data traffic of the specified NIC. 54statistics.getIfaceRxBytes('wlan0', (error: BusinessError, stats: number) => { 55 console.log(JSON.stringify(error)); 56 console.log(JSON.stringify(stats)); 57}); 58 59// Obtain the real-time uplink data traffic of the specified NIC. 60statistics.getIfaceTxBytes('wlan0', (error: BusinessError, stats: number) => { 61 console.log(JSON.stringify(error)); 62 console.log(JSON.stringify(stats)); 63}); 64 65// Obtain the real-time downlink data traffic of the cellular network. 66statistics.getCellularRxBytes((error: BusinessError, stats: number) => { 67 console.log(JSON.stringify(error)); 68 console.log(JSON.stringify(stats)); 69}); 70 71// Obtain the real-time uplink data traffic of the cellular network. 72statistics.getCellularTxBytes((error: BusinessError, stats: number) => { 73 console.log(JSON.stringify(error)); 74 console.log(JSON.stringify(stats)); 75}); 76 77// Obtain the real-time downlink data traffic of the all NICs. 78statistics.getAllRxBytes((error: BusinessError, stats: number) => { 79 console.log(JSON.stringify(error)); 80 console.log(JSON.stringify(stats)); 81}); 82 83// Obtain the real-time uplink data traffic of the all NICs. 84statistics.getAllTxBytes((error: BusinessError, stats: number) => { 85 console.log(JSON.stringify(error)); 86 console.log(JSON.stringify(stats)); 87}); 88 89// Obtain the real-time downlink data traffic of the specified application. 90let uid = 20010038; 91statistics.getUidRxBytes(uid, (error: BusinessError, stats: number) => { 92 console.log(JSON.stringify(error)); 93 console.log(JSON.stringify(stats)); 94}); 95 96// Obtain the real-time uplink data traffic of the specified application. 97let uids = 20010038; 98statistics.getUidTxBytes(uids, (error: BusinessError, stats: number) => { 99 console.log(JSON.stringify(error)); 100 console.log(JSON.stringify(stats)); 101}); 102 103// Obtain the real-time downlink data traffic of the specified socket. 104let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 105tcp.getSocketFd().then((sockfd: number) => { 106 statistics.getSockfdRxBytes(sockfd, (error: BusinessError, stats: number) => { 107 console.log(JSON.stringify(error)); 108 console.log(JSON.stringify(stats)); 109 }); 110}); 111 112// Obtain the real-time uplink data traffic of the specified socket. 113tcp.getSocketFd().then((sockfd: number) => { 114 statistics.getSockfdTxBytes(sockfd, (error: BusinessError, stats: number) => { 115 console.log(JSON.stringify(error)); 116 console.log(JSON.stringify(stats)); 117 }); 118}); 119``` 120 121## Obtaining Historical Traffic Data by NIC or UID 122 1231. Obtain the historical data traffic of the specified NIC. 1242. Obtain the historical data traffic of the specified application. 125 126```ts 127import statistics from '@ohos.net.statistics'; 128import { BusinessError } from '@ohos.base'; 129 130class IfaceInfo { 131 iface: string = "wlan0" 132 startTime: number = 1685948465 133 endTime: number = 16859485670 134} 135// Obtain the historical data traffic of the specified NIC. 136statistics.getTrafficStatsByIface(new IfaceInfo(), (error: BusinessError, statsInfo: statistics.NetStatsInfo) => { 137 console.log(JSON.stringify(error)) 138 console.log("getTrafficStatsByIface bytes of received = " + JSON.stringify(statsInfo.rxBytes)); 139 console.log("getTrafficStatsByIface bytes of sent = " + JSON.stringify(statsInfo.txBytes)); 140 console.log("getTrafficStatsByIface packets of received = " + JSON.stringify(statsInfo.rxPackets)); 141 console.log("getTrafficStatsByIface packets of sent = " + JSON.stringify(statsInfo.txPackets)); 142}); 143 144class UidInfo { 145 uid: number = 20010037 146 ifaceInfo: IfaceInfo = new IfaceInfo() 147} 148 149let uidInfo = new UidInfo() 150 151// Obtain the historical data traffic of the specified application. 152statistics.getTrafficStatsByUid(uidInfo, (error: BusinessError, statsInfo: statistics.NetStatsInfo) => { 153 console.log(JSON.stringify(error)) 154 console.log("getTrafficStatsByUid bytes of received = " + JSON.stringify(statsInfo.rxBytes)); 155 console.log("getTrafficStatsByUid bytes of sent = " + JSON.stringify(statsInfo.txBytes)); 156 console.log("getTrafficStatsByUid packets of received = " + JSON.stringify(statsInfo.rxPackets)); 157 console.log("getTrafficStatsByUid packets of sent = " + JSON.stringify(statsInfo.txPackets)); 158}); 159``` 160 161## Subscribing to Traffic Change Events 162 1631. Subscribe to traffic change events. 1642. Unsubscribe from traffic change events. 165 166```ts 167import statistics from '@ohos.net.statistics'; 168class Data { 169 iface: string = "" 170 uid?: number = 0 171} 172 173let callback = (data: Data) => { 174 console.log('on netStatsChange, data:' + JSON.stringify(data)); 175}; 176// Subscribe to traffic change events. 177statistics.on('netStatsChange', callback); 178 179// 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. 180statistics.off('netStatsChange', callback); 181statistics.off('netStatsChange'); 182``` 183