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