#json-rpc #nimiq #json #rpc

nimiq-jsonrpc-core

Core utilities for implementing libraries around JSON-RPC

6 releases

new 0.3.0 Apr 29, 2025
0.2.2 Jan 29, 2025
0.1.2 Jan 8, 2025
0.1.1 Nov 23, 2024

#323 in HTTP client

Download history 195/week @ 2025-01-10 646/week @ 2025-01-17 1694/week @ 2025-01-24 1945/week @ 2025-01-31 2839/week @ 2025-02-07 2643/week @ 2025-02-14 1475/week @ 2025-02-21 1411/week @ 2025-02-28 1459/week @ 2025-03-07 1544/week @ 2025-03-14 438/week @ 2025-03-21 1474/week @ 2025-03-28 1478/week @ 2025-04-04 1406/week @ 2025-04-11 1413/week @ 2025-04-18 1505/week @ 2025-04-25

6,038 downloads per month
Used in 3 crates

Apache-2.0

15KB
335 lines

Build + Test

Nimiq JSON-RPC

A Rust implementation of the JSON-RPC 2.0 specification.

  • nimiq-jsonrpc-core implements the data structures (using serde) for JSON-RPC.
  • nimiq-jsonrpc-client is a client implementation for a HTTP (using reqwest) and websocket client (using tokio-tungstenite).
  • nimiq-jsonrpc-server is a server implementation for HTTP and websocket (using warp).

Example

use async_trait::async_trait;
use serde::{Serialize, Deserialize};

use nimiq_jsonrpc_server::{Server, Config};
use nimiq_jsonrpc_client::http::HttpClient;


#[nimiq_jsonrpc_derive::proxy]
#[async_trait]
trait Foobar {
    async fn hello(&self, name: String) -> String;
}

struct FoobarService;

#[nimiq_jsonrpc_derive::service]
#[async_trait]
impl Foobar for FoobarService {
    async fn hello(&self, name: String) -> String {
        println!("Hello, {}", name);
        format!("Hello, {}", name)
    }
}


#[tokio::main]
async fn main() {
    dotenvy::dotenv().ok();
    pretty_env_logger::init();

    let config = Config::default();

    log::info!("Listening on: {}", config.bind_to);

    let server = Server::new(config, FoobarService);
    tokio::spawn(async move {
        server.run().await;
    });

    let client = HttpClient::new("http://localhost:8000/");
    let mut proxy = FoobarProxy::new(client);

    let retval = proxy.hello("World".to_owned()).await;
    log::info!("RPC call returned: {}", retval);
}

TODO

  • Share code between websocket clients.

Dependencies

~0.7–1.6MB
~35K SLoC