scalapb-grpcweb-code-gen


License

License

Categories

Categories

Scala Languages gRPC Net Networking
GroupId

GroupId

com.thesamet.scalapb.grpcweb
ArtifactId

ArtifactId

scalapb-grpcweb-code-gen_3
Last Version

Last Version

0.6.4
Release Date

Release Date

Type

Type

jar
Description

Description

scalapb-grpcweb-code-gen
scalapb-grpcweb-code-gen
Project URL

Project URL

https://github.com/scalapb/scalapb-grpcweb
Project Organization

Project Organization

com.thesamet.scalapb.grpcweb
Source Code Management

Source Code Management

https://github.com/scalapb/scalapb-grpcweb

Download scalapb-grpcweb-code-gen_3

How to add to project

<!-- https://jarcasting.com/artifacts/com.thesamet.scalapb.grpcweb/scalapb-grpcweb-code-gen_3/ -->
<dependency>
    <groupId>com.thesamet.scalapb.grpcweb</groupId>
    <artifactId>scalapb-grpcweb-code-gen_3</artifactId>
    <version>0.6.4</version>
</dependency>
// https://jarcasting.com/artifacts/com.thesamet.scalapb.grpcweb/scalapb-grpcweb-code-gen_3/
implementation 'com.thesamet.scalapb.grpcweb:scalapb-grpcweb-code-gen_3:0.6.4'
// https://jarcasting.com/artifacts/com.thesamet.scalapb.grpcweb/scalapb-grpcweb-code-gen_3/
implementation ("com.thesamet.scalapb.grpcweb:scalapb-grpcweb-code-gen_3:0.6.4")
'com.thesamet.scalapb.grpcweb:scalapb-grpcweb-code-gen_3:jar:0.6.4'
<dependency org="com.thesamet.scalapb.grpcweb" name="scalapb-grpcweb-code-gen_3" rev="0.6.4">
  <artifact name="scalapb-grpcweb-code-gen_3" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.thesamet.scalapb.grpcweb', module='scalapb-grpcweb-code-gen_3', version='0.6.4')
)
libraryDependencies += "com.thesamet.scalapb.grpcweb" % "scalapb-grpcweb-code-gen_3" % "0.6.4"
[com.thesamet.scalapb.grpcweb/scalapb-grpcweb-code-gen_3 "0.6.4"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala3-library_3 jar 3.0.0
com.thesamet.scalapb : compilerplugin_3 jar 0.11.3

Project Modules

There are no modules declared in this project.

Snapshot Artifacts

scalapb-grpcweb

Experimental grpc-web support for ScalaPB

This library provides a code generator and runtime that enables calling gRPC services from your Scala.js code using grpc-web.

Usage:

  1. Add the following to your project/plugins.sbt:

    val grpcWebVersion = "0.6.2"
    
    resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
    
    addSbtPlugin("com.thesamet" % "sbt-protoc" % "1.0.3")
    
    libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.11.3"
    
    libraryDependencies += "com.thesamet.scalapb.grpcweb" %% "scalapb-grpcweb-code-gen" % grpcWebVersion
    
    addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.20.0")
    
  2. Add a cross-project with the protos that will be shared by Scala.js and JVM:

    lazy val protos =
      crossProject(JSPlatform, JVMPlatform)
         .crossType(CrossType.Pure)
         .in(file("protos"))
         .settings(
           PB.protoSources in Compile := Seq(
             (baseDirectory in ThisBuild).value / "protos" / "src" / "main" / "protobuf"
           ),
           libraryDependencies += "com.thesamet.scalapb" %%% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion
         )
         .jvmSettings(
           libraryDependencies += "com.thesamet.scalapb" %% "scalapb-runtime-grpc" % scalapb.compiler.Version.scalapbVersion,
           PB.targets in Compile := Seq(
             scalapb.gen() -> (sourceManaged in Compile).value
           ),
         )
         .jsSettings(
           // publish locally and update the version for test
           libraryDependencies += "com.thesamet.scalapb.grpcweb" %%% "scalapb-grpcweb" % scalapb.grpcweb.BuildInfo.version,
           PB.targets in Compile := Seq(
             scalapb.gen(grpc=false) -> (sourceManaged in Compile).value,
             scalapb.grpcweb.GrpcWebCodeGenerator -> (sourceManaged in Compile).value
           )
         )
    
  3. In your client code, instantiate the stub like this:

    val stub = TestServiceStub.stub(Channels.grpcwebChannel("http://localhost:8081"))
    
  4. Now, you can call it like:

    // Make an async unary call
    stub.unary(req).onComplete {
      f => println("Unary" -> f)
    }
    
    // You can also pass metadata
    // Make sure header1 is accepted on the envoy config, otherwise the request will be rejected
    stub.unary(req, Metadata("header1" -> "value1")).onComplete {
      f => println("Unary" -> f)
    }
    
    // Make an async server streaming call
    stub.serverStreaming(req, new StreamObserver[Res] {
      override def onNext(value: Res): Unit = {
        println("Next: " + value)
      }
    
      override def onError(throwable: Throwable): Unit = {
        println("Error! " + throwable)
      }
    
      override def onCompleted(): Unit = {
        println("Completed!")
      }
    })
    

Check the full example

com.thesamet.scalapb.grpcweb

ScalaPB

ScalaPB

Versions

Version
0.6.4