Skip to content
프로젝트

소개

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

시스템 및 인프라

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
기록 수집 중
in 7 days
기록 수집 중
in 15 days
기록 수집 중
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

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

관련 프로젝트 살펴보기

불러오는 중…