16 stable releases (7 major)

✓ Uses Rust 2018 edition

new 14.0.0 Oct 9, 2019
13.2.0 Sep 13, 2019
13.1.0 Aug 26, 2019
13.0.0 Jul 29, 2019
7.1.1 Sep 11, 2017

#3 in #jsonrpc

Download history 1040/week @ 2019-06-26 1070/week @ 2019-07-03 1341/week @ 2019-07-10 1154/week @ 2019-07-17 1095/week @ 2019-07-24 1237/week @ 2019-07-31 1282/week @ 2019-08-07 1625/week @ 2019-08-14 2119/week @ 2019-08-21 2009/week @ 2019-08-28 2871/week @ 2019-09-04 5984/week @ 2019-09-11 6959/week @ 2019-09-18 7055/week @ 2019-09-25 6418/week @ 2019-10-02

13,731 downloads per month
Used in 18 crates (7 directly)

MIT license

93KB
2.5K SLoC

Parity JSON-RPC

Rust implementation of JSON-RPC 2.0 Specification. Transport-agnostic core and transport servers for http, ipc, websockets and tcp.

New! Support for clients.

Build Status Build Status

Documentation

Sub-projects

Examples

Basic Usage (with HTTP transport)

use jsonrpc_core::{IoHandler, Value, Params};
use jsonrpc_http_server::{ServerBuilder};

fn main() {
	let mut io = IoHandler::new();
	io.add_method("say_hello", |_params: Params| {
		Ok(Value::String("hello".to_string()))
	});

	let server = ServerBuilder::new(io)
		.threads(3)
		.start_http(&"127.0.0.1:3030".parse().unwrap())
		.unwrap();

	server.wait().unwrap();
}

Basic usage with derive

use jsonrpc_core::Result;
use jsonrpc_derive::rpc;

#[rpc]
pub trait Rpc {
	/// Adds two numbers and returns a result
	#[rpc(name = "add")]
	fn add(&self, u64, u64) -> Result<u64>;
}

pub struct RpcImpl;
impl Rpc for RpcImpl {
	fn add(&self, a: u64, b: u64) -> Result<u64> {
		Ok(a + b)
	}
}

fn main() {
	let mut io = jsonrpc_core::IoHandler::new();
	io.extend_with(RpcImpl.to_delegate())
}

Client support

use jsonrpc_core_client::transports::local;
use jsonrpc_core::futures::future::{self, Future, FutureResult};
use jsonrpc_core::{Error, IoHandler, Result};
use jsonrpc_derive::rpc;

/// Rpc trait
#[rpc]
pub trait Rpc {
	/// Returns a protocol version
	#[rpc(name = "protocolVersion")]
	fn protocol_version(&self) -> Result<String>;

	/// Adds two numbers and returns a result
	#[rpc(name = "add", alias("callAsyncMetaAlias"))]
	fn add(&self, a: u64, b: u64) -> Result<u64>;

	/// Performs asynchronous operation
	#[rpc(name = "callAsync")]
	fn call(&self, a: u64) -> FutureResult<String, Error>;
}

struct RpcImpl;

impl Rpc for RpcImpl {
	fn protocol_version(&self) -> Result<String> {
		Ok("version1".into())
	}

	fn add(&self, a: u64, b: u64) -> Result<u64> {
		Ok(a + b)
	}

	fn call(&self, _: u64) -> FutureResult<String, Error> {
		future::ok("OK".to_owned())
	}
}

fn main() {
	let mut io = IoHandler::new();
	io.extend_with(RpcImpl.to_delegate());

	let fut = {
		let (client, server) = local::connect::<gen_client::Client, _, _>(io);
		client.add(5, 6).map(|res| println!("5 + 6 = {}", res)).join(server)
	};
	fut.wait().unwrap();
}

Dependencies

~1.9–2.6MB
~54K SLoC