This is a fork of scala-json-rpc updated to support Scala 2.13.10 and Scala.js 1.12.0.
Let your servers and clients communicate over function calls!
scala-json-rpc is a Remote Procedure Call (RPC) library honoring JSON-RPC 2.0 spec.
JSON-RPC defines a specification of RPC in JSON format. This means that you can achieve RPC between your components as long as they are capable of
- Serializing and deserializing JSON
- Passing strings around
+--------+ +--------+
| | ---[request as JSON]---> | |
| Client | | Server |
| | <--[response as JSON]--- | |
+--------+ +--------+
Using scala-json-rpc, your server and client can communicate over statically typed interfaces like below:
trait LoggerAPI {
def log(message: String): Unit
}
case class Foo(id: String)
trait FooRepositoryAPI {
def add(foo: Foo): Future[Unit]
def remove(foo: Foo): Future[Unit]
def getAll(): Future[Set[Foo]]
}
class LoggerAPIImpl extends LoggerAPI {
override def log(message: String): Unit = println(message)
}
class FooRepositoryAPIImpl extends FooRepositoryAPI {
var foos: Set[Foo] = Set()
override def add(foo: Foo): Future[Unit] = this.synchronized {
foos = foos + foo
Future() // Acknowledge
}
override def remove(foo: Foo): Future[Unit] = this.synchronized {
foos = foos - foo
Future() // Acknowledge
}
override def getAll(): Future[Set[Foo]] = Future {
foos
}
}
val jsonSerializer = // ...
val server = JSONRPCServer(jsonSerializer)
server.bindAPI[LoggerAPI](new LoggerAPIImpl)
server.bindAPI[FooRepositoryAPI](new FooRepositoryAPIImpl)
def onRequestJSONReceived(requestJSON: String): Unit = {
server.receive(requestJSON).onComplete {
case Success(Some(responseJSON: String)) => sendResponseJSONToClient(responseJSON)
case _ =>
}
}
val jsonSerializer = // ...
val jsonSender = // ...
val client = JSONRPCClient(jsonSerializer, jsonSender)
val loggerAPI = client.createAPI[LoggerAPI]
val fooRepositoryAPI = client.createAPI[FooRepositoryAPI]
loggerAPI.log("Hello, World!")
fooRepositoryAPI.add(Foo("A"))
fooRepositoryAPI.add(Foo("B"))
fooRepositoryAPI.remove(Foo("A"))
fooRepositoryAPI.getAll().onComplete {
case Success(foos: Set[Foo]) => println(s"Received all the foos: $foos")
case _ =>
}
def onResponseJSONReceived(responseJSON: String): Unit = {
client.receive(responseJSON)
}
Platform | SBT | Scala Version | Scala JS Version |
---|---|---|---|
JVM | "io.github.nawforce" %% "scala-json-rpc" % "1.0.0" |
2.13 | |
JS | "io.github.nawforce" %%% "scala-json-rpc" % "1.0.0" |
2.13 | 1.0+ |
scala-json-rpc has no external dependency, so it should fit into any of your Scala JVM & JS applications.
- Unidirectional JSON-RPC from Scala JS to Scala JVM over HTTP
- Bidirectional JSON-RPC between Scala JS and Scala JVM over WebSocket
It should already serve you well as a RPC library, but it still does not fully support JSON-RPC spec yet. Here are list of known JSON-RPC features that's not supported yet.
- Send/receive named parameter
- Define custom parameter name
- Send/receive custom JSON-RPC error
- Define custom JSON-RPC request ID