Giter VIP home page Giter VIP logo

akshetpandey / react-native-cronet Goto Github PK

View Code? Open in Web Editor NEW
135.0 9.0 15.0 5.79 MB

This package allows you to use the Cronet for your react native apps.

Home Page: https://www.npmjs.com/package/react-native-cronet

License: MIT License

Java 44.75% JavaScript 14.22% Ruby 6.61% Objective-C 8.68% Objective-C++ 10.54% TypeScript 13.96% Starlark 0.93% Swift 0.15% C 0.16%
react-native cronet networking-stack

react-native-cronet's Introduction

react-native-cronet: Chrome's networking stack for your react-native application

PRs Welcome License Known Vulnerabilities NPM Version

Cronet is the networking stack of Chromium put into a library for use on mobile. This is the same networking stack that is used in the Chrome browser by over a billion people. It offers an easy-to-use, high performance, standards-compliant, and secure way to perform HTTP requests. Cronet has support for both Android and iOS.

This module allows you to use the Cronet stack for your react native apps. Checkout default react-native vs react-native-cronet comparison on android when loading many images on a high lateceny and packetloss network

Preview

Support Matrix

React Native react-native-cronet
>=0.62 0.5.0
>=0.60 0.4.0
<0.60 unsupported

Getting started

Using npm:

npm install --save react-native-cronet

or using yarn:

yarn add react-native-cronet

Automatic installation

react-native-cronet will link automatically using the autolink.

Make sure to run pod install in your iOS folder to get the Cronet.framework dependency included.

Manual installation

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-cronet and add RNCronet.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNCronet.a to your project's Build PhasesLink Binary With Libraries
  4. Add pod 'Cronet' as a dependency in your iOS Podfile and run pod install, alternatively manaually link Cronet.framework to your project
  5. Run your project (Cmd+R)<

Android

  1. Open up android/app/src/main/java/[...]/MainApplication.java
  • Add import com.akshetpandey.rncronet.RNCronetPackage; to the imports at the top of the file
  • Add new RNCronetPackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
    include ':react-native-cronet'
    project(':react-native-cronet').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-cronet/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
      compile project(':react-native-cronet')
    

Basic Usage

iOS

For iOS, you will have to disable bitcode for your target.

  • In XCode, in the project navigator, select your project. Build SettingsEnable BitcodeNo

Android

For Android, in your MainApplication.java, you will have to change how RN initializes FrescoModule by adding these lines:

import com.akshetpandey.rncronet.RNCronetFrescoImagePipelineConfig; // <--- ADD THIS
import com.facebook.imagepipeline.core.ImagePipelineConfig;         // <--- ADD THIS
import com.facebook.react.shell.MainPackageConfig;                  // <--- ADD THIS

public class MainApplication extends Application implements ReactApplication {
  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    //...
    @Override
    protected List<ReactPackage> getPackages() {
      ImagePipelineConfig pipelineConfig = RNCronetFrescoImagePipelineConfig.build(getApplicationContext());  // <--- ADD THIS
      MainPackageConfig config = new MainPackageConfig.Builder().setFrescoConfig(pipelineConfig).build();     // <--- ADD THIS
      List<ReactPackage> packages = new PackageList(this, config).getPackages();                              // <--- CHANGE THIS TO INCLUDE CONFIG
      // Packages that cannot be autolinked yet can be added manually here, for example:
      return packages;
    }
    //...
  }
}

Advanced Usage

Although the library is capable of automatically configuring itself, you can also initialize the cronet engine based on your use case. One reason to do this would be to provide QUIC hints for your domain that you know supports QUIC, or to customize cache size and type.

Make sure this is done before the react native bridge gets initialized.

Nothing needs to be done on the JS side.

iOS

Somewhere in your app startup flow, ex. in AppDelegate.m, you can install an initializer block that can initialize the library for your requirements.

iOS documentation for the cronet library initialization is sparse, but you can look at Cronet/Cronet.h

#import <RCTCronetHTTPRequestHandler.h>
#import <Cronet/Cronet.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // ...

  [RCTCronetHTTPRequestHandler setCustomCronetBuilder:^{
    [Cronet setHttp2Enabled:YES];
    [Cronet setQuicEnabled:YES];
    [Cronet setBrotliEnabled:YES];
    [Cronet setHttpCacheType:CRNHttpCacheTypeDisk];

    [Cronet addQuicHint:@"www.google.com" port:443 altPort:443];

    [Cronet start];

    [Cronet registerHttpProtocolHandler];
  }];

  // ...
}

Android

Somewhere in your app startup flow, ex in MainActivity.java, you can install an initializer block that can initialize the library for your requirements.

Android documentation for the cronet library initialization is available in CronetEngine.Builder

import org.chromium.net.CronetEngine;

public class MainActivity extends ReactActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // ...
    RNCronetNetworkingModule.setCustomCronetBuilder(context -> {
        File cacheDir = new File(context.getCacheDir(), "cronet-cache");
        cacheDir.mkdirs();
        CronetEngine cronetEngine = new CronetEngine.Builder(context)
                .enableBrotli(true)
                .enableHttp2(true)
                .enableQuic(true)
                .setStoragePath(cacheDir.getAbsolutePath())
                .enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK, 10 * 1024 * 1024)
                .addQuicHint("www.google.com", 443, 443)
                .build();
        URL.setURLStreamHandlerFactory(cronetEngine.createURLStreamHandlerFactory());
        return cronetEngine;
    });
    // ...
  }
}

react-native-cronet's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-native-cronet's Issues

WebTransport support

Hi, thank you for this library!

WebTransport seems to be supported by Cronet as the WebTransport APIs are available in Chromium/Chrome since version 97. I was wondering if you have considered adding WebTransport to this library?

It'd be huge to add WebTransport support to help RN devs develop real-time apps and games! WebTransport is now supported by 76%+ of global devices and WebKit proclaimed their support of WebTransport so we might get Safari support this summer, which would make it supported across all major browsers. WebTransport is about to take off, and it'd be awesome to have support for it in RN!

Example does not compile

After cloning this repo, tried to compile and run the example on Windows 10 Android emulator:

git clone https://github.com/akshetpandey/react-native-cronet.git
cd react-native-cronet\example
yarn
yarn android

I get the following error:

FAILURE: Build completed with 2 failures.

1: Task failed with an exception.
-----------
* Where:
Build file 'E:\Sandbox\react-native-cronet\example\node_modules\react-native\ReactAndroid\build.gradle' line: 220

* What went wrong:
A problem occurred evaluating project ':ReactAndroid'.
> extensionSupplier.get()!!.compileSdkVersion must not be null

2: Task failed with an exception.
-----------
* What went wrong:
A problem occurred configuring project ':ReactAndroid'.
> compileSdkVersion is not specified.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.