Proxy classes over socket.io.
npm i -s soct
// foo.js
class Foo {
constructor() {
this.state = {
bar: true,
};
}
get bar() {
return this.state.bar;
}
set bar(val) {
this.state.bar = val;
}
baz(val) {
return val * 2;
}
}
module.exports = Foo;
//service.js
const {createSoctServer} = require("soct");
const Foo = require("./foo");
createSoctServer(
new Foo(), // class to proxy; must be an instance of the class
5000, // port to proxy on
);
// app.js
const {createSoctClient} = require("soct");
const Foo = require("./foo");
const foo = createSoctClient(
Foo, // class definition
5000, // port to proxy on (matches previous service)
);
const run = async () => {
console.log(await foo.bar); // => true
foo.bar = false;
console.log(await foo.bar); // => false
console.log(await foo.baz(5)); // => 10
};
run();