Giter Site home page Giter Site logo

json_rpc's Introduction

JsonRPC

Hex.pm: version Hex.pm: downloads GitHub: CI status Coveralls: coverage License: MIT

An implementation of the JSON RPC protocol version 2.

This library provides functions to generate and handle JSON RPC requests, responses and error responses. For the transport of the generate RPC data the behaviour JsonRPC.Transport must be implemented.

Installation

The package can be installed by adding json_rpc to your list of dependencies in mix.exs:

def deps do
  [
    {:json_rpc, "~> 0.1"}
    # {:jason, "~> 1.2"}
    # {:poison, "~> 5.0"}
  ]
end

JsonRPC uses jason per default. To customize the JSON library, including the following in your config:

config :json_rpc, parser: :poison

Examples

This repo contains some examples of how to us JsonRPC.

  • SimRpc shows the usage with a "simulated" server. Similar to the example in section Usage.
  • phx_server shows a server implementation with a HTTP endpoint and a WebSocket.
  • http_client shows a HTTP client that communicates with the example server implemented in phx_server
  • ws_client shows a WebSocket client that communicates with the example server implemented in phx_server

Usage

A quick simple example, for more information see the documentation and the examples.

A client:

defmodule Client do
  use JsonRPC, transport: Transport

  rpc add(a, b)
end

An implementation of the behaviour JsonRPC.Transport:

defmodule Transport do
  @behaviour JsonRPC.Transport

  @impl
  def send_rpc(json, _opts) do
    Process.spawn(Transport, :server, [json], [])
    :ok
  end

  def server(json) do
    json
    |> JsonRPC.handle_request(Math)
    |> JsonRPC.handle_response()
  end
end

The module containing the function to be called:

defmodule Math do
  def add(a, b), do: a + b
end

Using the client:

iex> Client.add(1, 2)
{:ok, 3}

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.