### Start Envoy Server for Web/JS Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/examples/native-integration/readme.md Starts the Envoy proxy server required for the Web/JS gRPC integration. Ensure you are in the 'jvm' directory and have the custom Envoy configuration file. ```bash cd jvm docker run --rm -it -v $(pwd)/envoy-custom.yaml:/envoy-custom.yaml --network=host envoyproxy/envoy-dev:8ec461e3a6ff2503a05e599029c47252d732d87b -c /envoy-custom.yaml ``` -------------------------------- ### Configure Naming Strategy in build.gradle.kts Source: https://github.com/timortel/grpc-kotlin-multiplatform/wiki/Migration-Guide:-Version-1-to-Version-2 Example of how to configure the naming strategy in your build.gradle.kts file. Use NamingStrategy.LEGACY to preserve v1 behavior or NamingStrategy.KOTLIN_IDIOMATIC for new projects. ```kotlin grpcLibrary { // If you want to preserve the old v1 behavior: namingStrategy = NamingStrategy.LEGACY // Recommended for new projects: // namingStrategy = NamingStrategy.KOTLIN_IDIOMATIC } ``` -------------------------------- ### Implement Call Interceptor Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Provides an example of a call interceptor to modify requests and responses. It logs call start and close events. ```kotlin val loggingInterceptor = object : CallInterceptor { override fun onStart(methodDescriptor: MethodDescriptor, metadata: Metadata): Metadata { println("Call started ${methodDescriptor.fullMethodName}") return super.onStart(methodDescriptor, metadata) } override fun onClose( methodDescriptor: MethodDescriptor, status: Status, trailers: Metadata ): Pair { println("Call closed ${methodDescriptor.fullMethodName}") return super.onClose(methodDescriptor, status, trailers) } } val channel = Channel.Builder .forAddress("localhost", 8080) .withInterceptors(loggingInterceptor) .build() ``` -------------------------------- ### Run JVM Gradle Task for Web/JS Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/examples/native-integration/readme.md Executes the Gradle task to run the browser development server for the Web/JS client. This should be run after starting the Envoy server. ```bash gradle :commmon:build grade :js:browserDevelopmentRun ``` -------------------------------- ### Build Library Locally Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Command to publish the kmp-grpc-core library to the local Maven repository. ```bash gradle kmp-grpc-core:publishToMavenLocal ``` -------------------------------- ### Build Script Configuration Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Adds the necessary repositories to the top-level build.gradle.kts file. ```kotlin buildscript { repositories { //... gradlePluginPortal() } // ... } ``` -------------------------------- ### Build Plugin Locally Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Command to publish the kmp-grpc-plugin to the local Maven repository. ```bash gradle kmp-grpc-plugin:publishToMavenLocal ``` -------------------------------- ### Create HelloRequest Proto Object Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Instantiate a proto object for a HelloRequest in your common module. ```kotlin val request = helloRequest { greeting = "My greeting" } ``` -------------------------------- ### Construct Message with Extension Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Shows how to construct a message with a proto extension field. Requires the proto definition to be available. ```protobuf // sample.proto edition = "2023"; message MyMessage { string regularField = 1; extensions 2 to 5; } extend MyMessage { string myExtension = 2; } ``` ```kotlin val msg = Sample.MyMessage( regularField = "val1", extensions = buildExtensions { set(Sample.myExtension, "val2") } ) ``` -------------------------------- ### KMP gRPC Plugin Configuration Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Configures the KMP gRPC plugin in the common module's build.gradle.kts. Specifies proto source folders and naming strategy. ```kotlin plugins { kotlin("multiplatform") //... id("io.github.timortel.kmpgrpc.plugin") version "" } repositories { // ... mavenCentral() } kotlin { // Required applyDefaultHierarchyTemplate() // ... } kmpGrpc { // declare the targets you need. common() // required jvm() android() js() native() // for native targets like iOS // Optional: if the protobuf well known types should be included // https://protobuf.dev/reference/protobuf/google.protobuf/ includeWellKnownTypes = true // Optional: if all generated source files should have 'internal' visibility. internalVisibility = true /* * Defines the naming convention for generated classes and properties. * * - [NamingStrategy.KOTLIN_IDIOMATIC] (Default): Transforms names to match Kotlin * conventions (e.g., snake_case fields become camelCase, messages become PascalCase). * Repeated fields are automatically suffixed with "List". * * - [NamingStrategy.PROTO_LITERAL]: Keeps names exactly as they are defined * in the .proto source files. * * - [NamingStrategy.LEGACY]: Matches the behavior of major version 1 of this library. Keeps the * original .proto names but appends "List" and "Map" suffixes where applicable. */ namingStrategy = NamingStrategy.KOTLIN_IDIOMATIC // Specify the folders where your proto files are located, you can list multiple. protoSourceFolders = project.files("") } ``` -------------------------------- ### Make RPC Call with Deadline Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Make an RPC call using a generated stub, specifying a deadline. Ensure to handle potential StatusExceptions. ```kotlin suspend fun makeCall(): String { val channel = Channel.Builder() .forAddress("localhost", 8082) // replace with your address and your port .usePlaintext() // To force grpc to allow plaintext traffic, if you don't call this https is used. .build() // For each service a unique class is generated. val stub = HelloServiceStub(channel) val request = helloRequest { greeting = "My greeting" } return try { val response: HelloResponse = stub .withDeadlineAfter(10, TimeUnit.SECONDS) // Specify a deadline if you need to .myRpc(request) //Handle response response.response } catch (e: StatusException) { "An exception occurred: $e" } } ``` -------------------------------- ### Wrap Message in Any Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Use the `Any.wrap` extension function to wrap a message inside an `Any` proto message. ```kotlin val myMessage = myMessage {} // Some message val wrapped = Any.wrap(myMessage) ``` -------------------------------- ### Read Message Extension Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Demonstrates how to read the value of a proto extension field from a message. ```kotlin val msg: Sample.MyMessage = // ... val value = msg.extensions[Sample.myExtension] ``` -------------------------------- ### Create Timestamp from Instant Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Creates a Timestamp object from a Java Instant object. ```kotlin val timestamp = Timestamp.fromInstant(Instant.now()) ``` -------------------------------- ### Access Unknown Fields Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Demonstrates how to access unknown fields captured during message parsing. ```kotlin class ExampleMessage { val unknownFields: List } ``` -------------------------------- ### Configure Client Identity for TLS Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Configure a gRPC channel to present a client certificate and private key during the TLS handshake for server authentication. This configuration has no effect on JS/WasmJs targets. ```kotlin val channel = Channel.Builder() .forAddress(/*...*/) .withClientIdentity( certificate = Certificate.fromPem(/* client certificate PEM */), key = PrivateKey.fromPem(/* private key PEM */) ) .build() ``` -------------------------------- ### Configure Trusted Certificates (Trust Only Provided) Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Configure a gRPC channel to trust only the explicitly provided certificates. This overrides default trusted certificates. This configuration has no effect on JS/WasmJs targets. ```kotlin val channel = Channel.Builder() .forAddress(/*...*/) .withTrustedCertificates(/* חיצונית */) .trustOnlyProvidedCertificates() .build() ``` -------------------------------- ### Unwrap Message from Any Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Use the `unwrap` extension function to extract a specific message type from an `Any` proto message. ```kotlin val extractedMessage: MyMessage = wrapped.unwrap(MyMessage.Companion) ``` -------------------------------- ### Configure Channel KeepAlive Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Configure keep-alive settings for a gRPC channel to maintain active connections and detect broken ones. Note that this configuration has no effect on JS/WasmJs targets. ```kotlin val channel = Channel.Builder() .forAddress(/*...*/) .withKeepAliveConfig( // Exemplary configuration KeepAliveConfig.Enabled( time = 30.seconds, // Send keepalive ping every 30 seconds timeout = 10.seconds, // Wait 10 seconds for ping response withoutCalls = true // Send keepalive even when no RPCs are active ) ) .build() ``` -------------------------------- ### Convert Duration to kotlin.time.Duration Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Use the `toDuration` extension function to convert a gRPC `Duration` object to a `kotlin.time.Duration` object. ```kotlin val kotlinDuration: kotlin.time.Duration = duration.toDuration() ``` -------------------------------- ### Convert Timestamp to Instant Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Converts a Timestamp object to a Java Instant object. ```kotlin val instant: Instant = timestamp.toInstant() ``` -------------------------------- ### Configure Trusted Certificates Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Provide additional CA or leaf certificates for TLS connections. On JVM, default device certificates are trusted; on Native, webpki-roots are trusted. This configuration has no effect on JS/WasmJs targets. ```kotlin val channel = Channel.Builder() .forAddress(/*...*/) .withTrustedCertificates( listOf( Certificate.fromPem(/* PEM string */), Certificate.fromPem(/* another PEM */) ) ) .build() ``` -------------------------------- ### Check Message Type in Any Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Use the `isType` extension function to check if an `Any` proto message contains a specific message type. ```kotlin if (wrapped.isType(MyMessage.Companion)) { println("The message is of type MyMessage") } ``` -------------------------------- ### Create Duration from kotlin.time.Duration Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Use the `Duration.fromDuration` extension function to convert a `kotlin.time.Duration` to a gRPC `Duration` object. ```kotlin val duration = Duration.fromDuration(1.5.seconds) ``` -------------------------------- ### Create Duration from Seconds Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Use the `Duration.ofSeconds` extension function to create a `Duration` object from a number of seconds. ```kotlin val duration = Duration.ofSeconds(120) ``` -------------------------------- ### Create Duration from Milliseconds Source: https://github.com/timortel/grpc-kotlin-multiplatform/blob/main/readme.md Use the `Duration.ofMillis` extension function to create a `Duration` object from a number of milliseconds. ```kotlin val duration = Duration.ofMillis(500) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.