Socket
Book a DemoInstallSign in
Socket

@agree-able/rpc

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@agree-able/rpc

create and use rpc using p2p

3.0.0
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

Agreeable RPC

🤝 Type-safe P2P RPC that just works. A 🍐 project.

Why Agreeable RPC?

  • Simple API: Build P2P services with minimal boilerplate
  • Developer Experience: Interactive testing UI included
  • Trust: Define clear contracts between peers
  • Type Safety: Full TypeScript/JSDoc support with runtime validation

Quick Start

1. Install

npm i @agree-able/contract @agreeable/rpc

2. Define Your API (agreement.mjs)

import { z, addRoute } from '@agree-able/contract'

// Define your functions with Zod
export const AddTwo = z.function()
  .args(z.object({
    a: z.number(),
    b: z.number()
  }))
  .returns(z.promise(z.number()))

// Create the API contract
export default {
  role: 'calculator',
  version: '1.0.0',
  routes: {
    addTwo: addRoute(AddTwo)
  }
}

3. Implement the Server (server.mjs)

import { loadAgreement, host } from '@agree-able/rpc'

// Implement your functions
const addTwo = async ({a, b}) => a + b

// Start the server
host(await loadAgreement('./agreement.mjs', import.meta.url), { addTwo })

4. Create the Client (client.mjs)

import { Caller } from '@agree-able/rpc'
import agreement from './agreement.mjs'

const peerKey = process.argv[2]
const caller = new Caller(peerKey)
const { addTwo } = caller.proxy(agreement)

// Call remote functions
const result = await addTwo({ a: 1, b: 2 })
console.log(result) // 3
caller.destroy()

Running the Example

  • Start the server:
node server.mjs
# Will print a public key like: 3e32bb2d191316d952ae77439f7ec00a5c4fea8a01953b84d1b4eee36173e1ca
  • Run the client with the server's public key:
node client.mjs 3e32bb2d191316d952ae77439f7ec00a5c4fea8a01953b84d1b4eee36173e1ca

Interactive Testing

Use our UI tool to explore and test your APIs: https://github.com/agree-able/ui

Learn More

Contributing

We welcome contributions! See our contributing guide for details.

Keywords

agreeable

FAQs

Package last updated on 28 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.