Tired of wrestling with RPC boilerplate? Qubit simplifies communication between your Rust services and TypeScript clients, offering a type-safe and feature-rich development experience, so you can focus on building amazing applications.
Features:
-
Generated Type-Safe Clients: Say goodbye to manual type definitions, Qubit automatically generates TypeScript clients based on your Rust API, ensuring a smooth development experience.
-
Subscriptions: Build real-time, data-driven applications with subscriptions, allowing for your Rust server to push data directly to connected TypeScript clients.
-
Build Modular APIs: Organise your API handlers into nested routers, ensuring simplicity and maintainability as your service grows.
-
Serde Compatibility: Leverage Serde for seamless data serialisation and deserialisation between Rust and TypeScript.
-
Built on JSONRPC 2.0: Need a non-TypeScript client? Use any JSONRPC client in any language over WebSockets or HTTP.
-
Proven Base: Built on established libraries like
ts-rsfor type generation andjsonrpseeas the JSONRPC implementation.
Getting Started
- Add the required dependencies
# Cargo.toml
[]
= "0.6.1"
= "8.1.0" # Required to generate TS types
= { = "1.0", = ["derive"] } # Required for serialisable types
= "0.3.30" # Required for streaming functionality
= { = "1.38", = ["full"] }
= "0.7"
= { = "1.0", = ["server"] }
- Setup a Qubit router, and save the generated types
async
let router = new
.handler;
router.write_bindings_to_dir;
- Attach the Qubit router to an Axum router, and start it
// Create a service and handle
let = router.to_service;
// Nest into an Axum router
let axum_router = new
.nest_service;
// Start a Hyper server
serve
.await
.unwrap;
qubit_handle.stop.unwrap;
- Make requests from the TypeScript client
// Import transport from client, and generated server type
import { build_client, http } from "@qubit-rs/client";
import type { QubitServer } from "./bindings";
// Connect with the API
const api = build_client<QubitServer>(http("http://localhost:9944/rpc"));
// Call the handlers
const message = await api.hello_world.query();
console.log("received from server:", message);
Examples
Checkout all the examples in the examples directory.
FAQs
Qubit?
The term "Qubit" refers to the fundamental unit of quantum information. Just as a qubit can exist in a superposition of states, Qubit bridges the gap between Rust and TypeScript, empowering developers to create truly exceptional applications.
Prior Art
-
rspc: Similar concept, however uses a bespoke solution for generating TypeScript types from Rust structs, which isn't completely compatible with all of Serde's features for serialising and deserialising structs. -
trpc: Needs no introduction, however it being restricted to TypeScript backends makes it relatively useless for Rust developers.