Giter VIP home page Giter VIP logo

scalapb-grpcweb's Introduction

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.3.0"
    
    resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
    
    addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.31")
    
    libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.10.2"
    
    libraryDependencies += "com.thesamet.scalapb.grpcweb" %% "scalapb-grpcweb-code-gen" % grpcWebVersion
    
    addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.17.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

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.