heisenware/vrpc-js
概要
Remote objects over MQTT: call classes in other programs as if they were local - Node.js, browser, C++, Python, Arduino
システム・インフラ計測したスター増加
- 24時間
- 履歴を収集中
- 7日間
- 履歴を収集中
- 15日間
- 履歴を収集中
- 1か月
- 履歴を収集中
期間ごとの純増数です。期間は重複しており、日別の値ではありません。未計測の値は空欄です。スター数は注目度の指標であり、品質の保証ではありません。
README.md
オリジナルVRPC for JavaScript
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
- Getting started - a ten-minute tutorial
- How VRPC works, Instances, Calls, Events
- Guides: offering classes, the browser, persistence, security, production, troubleshooting
- API reference
- Protocol specification, for every VRPC implementation
- All documentation, and vrpc.io
- llms.txt - the documentation's index for AI coding assistants; every page is also served as Markdown
The VRPC family
- vrpc-hpp - C++
- vrpc-py - Python
- vrpc-arduino - Arduino and ESP32
- vrpc-react - React hooks
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
関連プロジェクトを探す
読み込み中…