Kacy
Kacy

Reputation: 3430

es6 object's getters, setters, and functions don't work with remote procedure calls (zerorpc)

I have the following class.

class User 
{
    constructor( username )
    {
        this._username = username;  
    }

    get username()
    {
        return this._username;
    }
}

When I instantiate a User object and send it over the wire, it says that myUserObject.username is undefined when I try to access it on the receiver end, but myUserObject._username does contain the value I set in the sender. I am using zerorpc which is built on zeromq and messagepack. Is there something I can add or do differently to make this work? This problem also occurs for normal class functions. Right now, my makeshift solution is to recreate the object on the receiver end by passing in the defective object into the constructor and reassigning the fields.

Sender:

import User from './User.js'
const client = new zerorpc.Client();

//code for client to connect

function createUser()
{
    let user = new User( "kacy" );
    console.log( user.username ); //kacy
    client.invoke( 'createUser', user, someCallback );
}

Receiver:

import User from './User.js'

createUser( user )
{
    console.log( user._username ); //kacy
    console.log( user.username ); //undefined
    let user2 = new User( user._username );
    console.log( user2.username ); //kacy
}

Upvotes: 0

Views: 196

Answers (1)

bombela
bombela

Reputation: 633

zerorpc doesn't send code trough the wire. username() is a function and wont be sent over the network, while _username is an attribute. zerorpc can send any json-like datatype, no more, no less.

Few reasons that I can think of behind this limitation:

  • sending code is an easy path to security risks (its literally remote code injection)
  • code has finicky behaviors vs inert data, making it even easier to break the contract between client/server in very subtle ways
  • it is definitively not easy to send code across languages (as zerorpc aims to be multi-language). This would require a common interpreted language (like lua for example).
  • If you do not fear the consequences, feel free to send code as literal text, and eval() it on the other side

Upvotes: 1

Related Questions