Skip to content
项目

关于

Remote objects over MQTT: call classes in other programs as if they were local - Node.js, browser, C++, Python, Arduino

系统与基础设施

这些数字如何统计

实测星标增长

24小时
0
7天
正在收集历史
15天
正在收集历史
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

VRPC for JavaScript

License: MIT npm version CI

Call classes in other programs as if they were local. VRPC (Variadic Remote Procedure Call) makes the classes of a Node.js, C++, Python or Arduino program callable from any other program - a backend service, a browser, a device - across any network, without REST routes, schemas or WebSocket plumbing.

This package is VRPC for JavaScript: the agent that offers your Node.js or browser code, and the client that uses remote code from Node.js or the browser.

Why VRPC

  • No API layer. Register a class and its functions are callable remotely: instances, static functions, promises, callbacks and errors work as they do locally.
  • Events that scale. Many clients can listen to the same events; the agent calls your event function once, hands every event to every listener, greets newcomers with the current state, and cleans up when the last one leaves.
  • Reaches everything. Built on MQTT: agents and clients only connect out to a broker, so code behind firewalls and NATs is reachable, and connections that break come back by themselves.
  • One protocol, several languages. A browser can call a C++ agent; a Python service can listen to a Node.js agent.

Install

npm install vrpc

Node.js 18 or newer, or any current browser. VRPC is free and open source; all it needs is an MQTT broker.

Quick start

Offer a class from one program - the agent:

// agent.js
const EventEmitter = require('events')
const { VrpcAdapter, VrpcAgent } = require('vrpc')

class Sensor extends EventEmitter {
  constructor (id) {
    super()
    this._timer = setInterval(() => this.emit('data', { id, value: Math.random() }), 1000)
  }

  async calibrate () {
    return true
  }

  [Symbol.dispose] () {
    clearInterval(this._timer)   // released when the instance is deleted
  }
}

VrpcAdapter.register(Sensor)

new VrpcAgent({ domain: 'my-domain', agent: 'sensors' })
  .serve()
  .then(() => console.log('Agent is serving'))

Use it from another - the client, in Node.js or a browser:

// client.js
const { VrpcClient } = require('vrpc')

async function main () {
  const client = new VrpcClient({ domain: 'my-domain' })
  await client.connect()

  const sensor = await client.create({
    agent: 'sensors',
    className: 'Sensor',
    instance: 'living-room',
    args: ['living-room']
  })

  console.log('calibrated:', await sensor.calibrate())
  await sensor.on('data', ({ id, value }) => console.log(id, value))
}

main()

Both connect to a public test broker by default; use a domain of your own, and your own broker for anything real. The tutorial takes it further in ten minutes.

Event functions

A function that calls a handler again and again returns a registration that tells the agent how to undo the call, and optionally how to greet a new listener:

/**
 * Calls back with every reading, beginning with the current one
 * @param {Function} handler
 * @returns {Registration}
 */
onReading (handler) {
  this._emitter.on('reading', handler)
  return {
    [Symbol.dispose]: () => this._emitter.off('reading', handler),
    greet: greeting => greeting(this._reading)
  }
}

The agent calls it once, however many clients subscribe, and releases it when the last one leaves. See Events and Writing event functions.

Documentation

The VRPC family

Contributing and security

Bug reports and small fixes are welcome; see CONTRIBUTING.md and the code of conduct. Report security issues privately, as described in SECURITY.md.

License

MIT

探索相关项目

加载中…