2

Is there a library which allows me to call remote objects by web sockets?

My target is to able to use an object like any other object (set/get properties on it, call methods) but the object would actually be a proxy over web sockets, where the actual instance lives on a server:

const obj = new MyObject();
console.log(obj.prop);
obj.prop = 1;
obj.do();

To see what I mean, I imagine such library would use ES6 proxies.

Don Box
  • 3,166
  • 3
  • 26
  • 55
  • What is the reason why you'd need to do this? Even with `WebSockets` this still requires a network call, so the example you've shown wouldn't this way. Sure you could use the `Proxy` to make network calls, but don't expect it to work as it was synchronous. – goto Jun 07 '20 at 11:28
  • @goto1 You might be right, maybe I am overthinking it. On the other hand, it would be nice to have a library which it "proxifies" the object for you and implmenets all the remote calls – Don Box Jun 07 '20 at 16:01
  • So you are right, the synchronous calls like the getter won't work. But I feel like what I am asking it could still work even if it would require the methods to be async – Don Box Jun 07 '20 at 16:04
  • You can implement an async functionality for `[[Get]]` and `[[Set]]` (and some other traps), but there are some places where you just can't do async calls, as it would violate Proxies' invariants... – FZs Jun 07 '20 at 17:22

1 Answers1

0

websockets are useful when you need a low latency connection, and multiple bidirectional connection (meaning the client can send data to the server and the server can send data to the client multiple times).

When you need to fetch and object, why using websockets is necessary? there is no need for low latency (unless the object is huge), and the connection is one off (meaning: the client request an object, the server sends it, and that's it).