15 releases

0.6.4 Apr 16, 2025
0.6.3 Mar 29, 2025
0.6.2 Jul 6, 2024
0.5.3 Dec 2, 2023
0.3.1 Jun 29, 2022

#751 in Network programming

Download history 187/week @ 2025-12-23 468/week @ 2025-12-30 371/week @ 2026-01-06 377/week @ 2026-01-13 685/week @ 2026-01-20 776/week @ 2026-01-27 1241/week @ 2026-02-03 740/week @ 2026-02-10 929/week @ 2026-02-17 1277/week @ 2026-02-24 1187/week @ 2026-03-03 1378/week @ 2026-03-10 1065/week @ 2026-03-17 505/week @ 2026-03-24 829/week @ 2026-03-31 713/week @ 2026-04-07

3,277 downloads per month
Used in yerpc-tide

Apache-2.0/MIT

41KB
933 lines

yerpc

docs.rs Crates.io

A JSON-RPC 2.0 server handler for Rust, with automatic generation of a TypeScript client.

yerpc includes (optional) integration with axum and tokio-tungstenite for easy setup and usage. Enable the support-axum and support-tungstenite feature flags for these integrations.

Example

use axum::{
    extract::ws::WebSocketUpgrade, http::StatusCode, response::Response, routing::get, Router,
};
use std::net::SocketAddr;
use yerpc::{rpc, RpcClient, RpcSession};
use yerpc::axum::handle_ws_rpc;

struct Api;

#[rpc(all_positional, ts_outdir = "typescript/generated", openrpc_outdir = "./")]
impl Api {
    async fn shout(&self, msg: String) -> String {
        msg.to_uppercase()
    }
    async fn add(&self, a: f32, b: f32) -> f32 {
        a + b
    }
}

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
    let api = Api {}
    let app = Router::new()
        .route("/rpc", get(handler))
        .layer(Extension(api));
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    eprintln!("listening on {}", addr);
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();

    Ok(())
}

async fn handler(
    ws: WebSocketUpgrade,
    Extension(api): Extension<Api>,
) -> Response {
    let (client, out_channel) = RpcClient::new();
    let session = RpcSession::new(client, api);
    handle_ws_rpc(ws, out_channel, session).await
}

Now you can connect any JSON-RPC client to ws://localhost:3000/rpc and call the shout and add methods.

After running cargo test you will find an autogenerated TypeScript client in the typescript/generated folder and an openrpc.json file in the root fo your project. See examples/axum for a full usage example with Rust server and TypeScript client for a chat server.

Dependencies

~2.1–6.5MB
~98K SLoC