Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
react-native-networking-patch
Advanced tools
Improves the performance of the React Native network module and adds a timeout feature.
React Native uses 6 event listeners to process networking events. For each request, these 6 listeners are registered and removed whenever processing. This is not the most effective approach. This patch makes RN's networking module work more efficiently.
It improves performance using only one event for each request. When tested in my environments, there was about 20% improvement in performance.
The following bridge communications occur for each fetch
request.
JS->N : Networking.addListener(["didSendNetworkData"])
JS->N : Networking.addListener(["didReceiveNetworkResponse"])
JS->N : Networking.addListener(["didReceiveNetworkData"])
JS->N : Networking.addListener(["didReceiveNetworkIncrementalData"])
JS->N : Networking.addListener(["didReceiveNetworkDataProgress"])
JS->N : Networking.addListener(["didCompleteNetworkResponse"])
JS->N : Networking.sendRequest([{"method":"GET","url":"...."....])
N->JS : <callback for Networking.sendRequest>([2])
N->JS : RCTDeviceEventEmitter.emit(["didReceiveNetworkResponse", ....])
N->JS : RCTDeviceEventEmitter.emit(["didReceiveNetworkData", ...])
N->JS : RCTDeviceEventEmitter.emit(["didCompleteNetworkResponse", ...])
JS->N : Networking.removeListeners([1])
JS->N : Networking.removeListeners([1])
JS->N : Networking.removeListeners([1])
JS->N : Networking.removeListeners([1])
JS->N : Networking.removeListeners([1])
JS->N : Networking.removeListeners([1])
The patched version looks like this.
JS->N : Networking.addListener(["events"])
JS->N : Networking.sendRequest([{"method":"GET","url":"...."....])
N->JS : <callback for Networking.sendRequest>([2])
N->JS : RCTDeviceEventEmitter.emit(["events", ....])
N->JS : RCTDeviceEventEmitter.emit(["events", ...])
N->JS : RCTDeviceEventEmitter.emit(["events", ...])
JS->N : Networking.removeListeners([1])
Other optimizations are also included.
fetch
has no built-in timeout option. As you know, there are many workarounds such as using XMLHttpRequest
API, AbortController
and setTimeout
+ Promise
. I want a simple and easy way. Now, you can set a global timeout without these workarounds.
// RN >= 0.62
import { Networking } from 'react-native';
// RN < 0.62
// import Networking from 'react-native/Libraries/Network/RCTNetworking';
// Setting default global timeout. You only need to set it once.
Networking.setTimeout(3000);
// After 3 seconds, a timeout exception is thrown.
async function getItem() {
let item = null;
try {
const response = await fetch(....);
item = await response.json();
} catch (e) {
console.error(e);
}
return item;
}
// `axios` works with a 10 second timeout, not 3 seconds.
async function getLongItem() {
const instance = axios.create({
baseURL: '...',
timeout: 10000,
});
....
}
It works with the latest versions such as v0.62.2, v0.61.5 and v0.60.6. If not, please upgrade to the latest version. Of course, it works on Expo
.
Once installed, react-native is automatically patched.
yarn add react-native-networking-patch --dev
postinstall
, postuninstall
should be added to prevent this patch from being restored whenever packages are changed.
// package.json
{
...
"scripts": {
...,
"postinstall": "yarn rn-networking-patch",
"postuninstall": "yarn rn-networking-patch"
}
}
If you were already using postinstall
, you can add the patch script later.
"postinstall": "yarn jetify; yarn rn-networking-patch"
You can execute the patch manually with the command below.
yarn rn-networking-patch
Just delete the command you added to postinstall
, postuninstall
and remove react-native-networking-patch
package.
FAQs
Improves the performance of the React Native network module and adds a timeout feature.
The npm package react-native-networking-patch receives a total of 147 weekly downloads. As such, react-native-networking-patch popularity was classified as not popular.
We found that react-native-networking-patch demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.