5 releases

0.2.2 Jan 29, 2025
0.2.1 Jan 28, 2025
0.1.2 Jan 8, 2025
0.1.1 Nov 23, 2024

#266 in HTTP server

Download history 213/week @ 2024-11-08 212/week @ 2024-11-15 566/week @ 2024-11-22 124/week @ 2024-11-29 83/week @ 2024-12-06 41/week @ 2024-12-13 24/week @ 2024-12-20 35/week @ 2024-12-27 201/week @ 2025-01-03 198/week @ 2025-01-10 646/week @ 2025-01-17 1675/week @ 2025-01-24 1945/week @ 2025-01-31 2825/week @ 2025-02-07

7,116 downloads per month
Used in nimiq-jsonrpc-derive

Apache-2.0

49KB
1K SLoC

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(&mut self, name: String) -> String;
}

struct FoobarService;

#[nimiq_jsonrpc_derive::service]
#[async_trait]
impl Foobar for FoobarService {
    async fn hello(&mut 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.

lib.rs:

This crate implements a JSON-RPC HTTP server using warp. It accepts POST requests at / and requests over websocket at /ws.

Dependencies

~9–16MB
~205K SLoC