Giter Site home page Giter Site logo

adsinc / scala-json-rpc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from shogowada/scala-json-rpc

0.0 0.0 0.0 262 KB

Let your servers and clients communicate over function calls! JSON-RPC 2.0 library for Scala and Scala.js

License: MIT License

Scala 100.00%

scala-json-rpc's Introduction

scala-json-rpc

master: Master Build Status

Let your servers and clients communicate over function calls!

scala-json-rpc is a Remote Procedure Call (RPC) library honorring 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]--- |        |
+--------+                          +--------+

Quick look

Shared between server and client

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]]
}

Server

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 _ =>
  }
}

Client

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)
}

Dependency

Platform SBT Scala Version Scala JS Version
JVM "io.github.shogowada" %% "scala-json-rpc" % "0.9.3" 2.12
JS "io.github.shogowada" %%% "scala-json-rpc" % "0.9.3" 2.12 0.6.17+

scala-json-rpc has no external dependency, so it should fit into any of your Scala JVM & JS applications.

Tutorials

Examples

TODOs

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

scala-json-rpc's People

Contributors

emanresusername avatar nadenf avatar shogowada avatar

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.