Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
# Changelog
## [Unreleased] - 2023-09-04

### Added
1. Channel Creation happening from JS layer
2. Reset Connection
3. Reset Connection State based on current state
4. UI log to debug release build

## [1.0.5] - 2023-04-12

### Added

LINT FIX

## [1.0.4] - 2023-04-12

### Added

Add option to configure keepalive


## [1.0.3] - 2023-04-12

### Added

Reverted swift dependency for ios pods

## [1.0.0-6] - 2023-01-13

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ gRPC for react-native
## Installation

```sh
npm install @mitch528/react-native-grpc
npm install @krishnafkh/react-native-grpc
```

## Usage

```ts
import { GrpcClient, GrpcMetadata } from '@mitch528/react-native-grpc';
import { GrpcClient, GrpcMetadata } from '@krishnafkh/react-native-grpc';

GrpcClient.setHost('example.com');

Expand Down
3 changes: 2 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ def getExtOrIntegerDefault(name) {
}

android {
namespace 'com.reactnativegrpc'
compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
buildToolsVersion getExtOrDefault('buildToolsVersion')
defaultConfig {
minSdkVersion 16
minSdkVersion 24
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
versionCode 1
versionName "1.0"
Expand Down
77 changes: 66 additions & 11 deletions android/src/main/java/com/reactnativegrpc/GrpcModule.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.reactnativegrpc;

import android.util.Base64;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.UiThread;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
Expand All @@ -19,6 +22,7 @@

import io.grpc.CallOptions;
import io.grpc.ClientCall;
import io.grpc.ConnectivityState;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
Expand All @@ -37,6 +41,7 @@ public class GrpcModule extends ReactContextBaseJavaModule {
private boolean keepAliveEnabled = false;
private Integer keepAliveTime;
private Integer keepAliveTimeout;
private boolean isUiLogEnabled = false;

private ManagedChannel managedChannel = null;

Expand All @@ -63,34 +68,29 @@ public void getIsInsecure(final Promise promise) {
@ReactMethod
public void setHost(String host) {
this.host = host;
this.handleOptionsChanged();
}

@ReactMethod
public void setInsecure(boolean insecure) {
this.isInsecure = insecure;
this.handleOptionsChanged();
}

@ReactMethod
public void setCompression(Boolean enable, String compressorName, String limit) {
public void setCompression(Boolean enable, String compressorName) {
this.withCompression = enable;
this.compressorName = compressorName;
this.handleOptionsChanged();
}

@ReactMethod
public void setResponseSizeLimit(int limit) {
this.responseSizeLimit = limit;
this.handleOptionsChanged();
}

@ReactMethod
public void setKeepalive(boolean enabled, int time, int timeout) {
public void setKeepAlive(boolean enabled, int time, int timeout) {
this.keepAliveEnabled = enabled;
this.keepAliveTime = time;
this.keepAliveTimeout = timeout;
this.handleOptionsChanged();
}

@ReactMethod
Expand Down Expand Up @@ -326,15 +326,14 @@ private static String normalizePath(String path) {
if (path.startsWith("/")) {
path = path.substring(1);
}

return path;
}

private void handleOptionsChanged() {
@ReactMethod
public void initGrpcChannel() {
if (this.managedChannel != null) {
this.managedChannel.shutdown();
}

this.managedChannel = createManagedChannel();
}

Expand All @@ -353,10 +352,66 @@ private ManagedChannel createManagedChannel() {
channelBuilder = channelBuilder
.keepAliveWithoutCalls(true)
.keepAliveTime(keepAliveTime, TimeUnit.SECONDS)
.keepAliveTimeout(keepAliveTime, TimeUnit.SECONDS);
.keepAliveTimeout(keepAliveTimeout, TimeUnit.SECONDS);
}

managedChannel = channelBuilder.build();
return managedChannel;
}

@ReactMethod
public void resetConnection(final String message){
if(null == managedChannel) return;

this.managedChannel.resetConnectBackoff();

this.initGrpcChannel();

showToast("resetConnection "+message);
}

@ReactMethod
public void onConnectionStateChange(){
if(null == managedChannel) return;

final ConnectivityState connectivityState = managedChannel.getState(true);
if(ConnectivityState.CONNECTING == connectivityState){
showToast("onConnectionState CONNECTING");
} else if(ConnectivityState.IDLE == connectivityState){
showToast("onConnectionState IDLE");
} else if(ConnectivityState.READY == connectivityState){
showToast("onConnectionState READY");
} else if(ConnectivityState.TRANSIENT_FAILURE == connectivityState){
showToast("onConnectionState TRANSIENT_FAILURE");
} else if(ConnectivityState.SHUTDOWN == connectivityState){
showToast("onConnectionState SHUTDOWN");
} else {
showToast("onConnectionState UNDEFINED");
}
if(ConnectivityState.TRANSIENT_FAILURE == connectivityState && managedChannel.isTerminated() || managedChannel.isShutdown()){
resetConnection("onConnectionStateChange");
}
}

@ReactMethod
public void enterIdle(){
if(null == managedChannel) return;

managedChannel.enterIdle();

showToast("enterIdle");
}

@ReactMethod
public void setUiLogEnabled(boolean isUiLogEnabled){
this.isUiLogEnabled = isUiLogEnabled;
}

@UiThread
private void showToast(final String message){
if(!isUiLogEnabled || null == context) return;

Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
Log.d("GRPC_MODULE",message);
}
}
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ PODS:
- React-jsinspector (0.70.6)
- React-logger (0.70.6):
- glog
- react-native-grpc (1.0.0-1):
- react-native-grpc (1.0.2):
- gRPC-Swift
- React-Core
- React-perflogger (0.70.6)
Expand Down Expand Up @@ -637,7 +637,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: b4a65947391c658450151275aa406f2b8263178f
React-jsinspector: 60769e5a0a6d4b32294a2456077f59d0266f9a8b
React-logger: 1623c216abaa88974afce404dc8f479406bbc3a0
react-native-grpc: 328ef8ca6228c1a408dc0ebe3d0d5eab949fd13a
react-native-grpc: 9cfbb1f3064b90afaad667ee7a517cd37e7e9e66
React-perflogger: 8c79399b0500a30ee8152d0f9f11beae7fc36595
React-RCTActionSheet: 7316773acabb374642b926c19aef1c115df5c466
React-RCTAnimation: 5341e288375451297057391227f691d9b2326c3d
Expand Down
2 changes: 1 addition & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'text-encoding';
import { GrpcClient } from '@mitch528/react-native-grpc';
import { GrpcClient } from '@krishnafkh/react-native-grpc';
import React, { useEffect, useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { RNGrpcTransport } from './transport';
Expand Down
12 changes: 6 additions & 6 deletions example/src/transport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable eslint-comments/no-unlimited-disable */
import { ClientStreamingCall, DuplexStreamingCall, mergeRpcOptions, MethodInfo, RpcOptions, RpcOutputStreamController, RpcStatus, RpcTransport, ServerStreamingCall, UnaryCall } from '@protobuf-ts/runtime-rpc';
import { GrpcClient, GrpcMetadata } from '@mitch528/react-native-grpc';
import { GrpcClient, GrpcMetadata } from '@krishnafkh/react-native-grpc';
import { AbortSignal } from 'abort-controller';

/* eslint-disable */
Expand Down Expand Up @@ -32,11 +32,11 @@ export class RNGrpcTransport implements RpcTransport {
});
}

const response = call.response.then(resp => method.O.fromBinary(resp));
const response = call.response.then((resp: any) => method.O.fromBinary(resp));
const status = call.trailers.then<RpcStatus, RpcStatus>(() => ({
code: 0,
detail: '',
} as any), ({ error, code }) => ({
} as any), ({ error, code }: any) => ({
code: code,
detail: error
}));
Expand All @@ -52,14 +52,14 @@ export class RNGrpcTransport implements RpcTransport {
const status = call.trailers.then<RpcStatus, RpcStatus>(() => ({
code: 0,
detail: '',
} as any), ({ error, code }) => ({
} as any), ({ error, code }: any) => ({
code: code,
detail: error
}));

const outStream = new RpcOutputStreamController<O>();

call.responses.on('data', (data) => {
call.responses.on('data', (data: any) => {
outStream.notifyMessage(method.O.fromBinary(data));
});

Expand All @@ -69,7 +69,7 @@ export class RNGrpcTransport implements RpcTransport {
}
});

call.responses.on('error', (reason) => {
call.responses.on('error', (reason: any) => {
outStream.notifyError(reason);
});

Expand Down
18 changes: 0 additions & 18 deletions ios/ByteBuffer+GRPCPayload.swift

This file was deleted.

9 changes: 7 additions & 2 deletions ios/Grpc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

@interface Grpc : NSObject <RCTBridgeModule>
@interface Grpc : RCTEventEmitter <RCTBridgeModule>

@end
@property (nonatomic, copy) NSString* grpcHost;
@property (nonatomic, copy) NSNumber* grpcResponseSizeLimit;
@property (nonatomic, assign) BOOL grpcInsecure;

@end
Loading