SharedState for object created on the client side


I am in the following situation (given by 3rd party code that can’t be changed) and I honestly do not know (from an architectural & implementation point of view) on how to resolve it.

I Need to implement an AbstractJavaScript extension. What I did was creating the serverside class and the javascript code with the method corresponding to the class name prefixed by the full package name, everything works fine, I can call Javascript from the server and the other way round.

The actual challenge:

On the Javascript side imagine a method that builds a Javascript object.
Now I somehow would need to create a class instance on the server side with a shared state for exactly this object. The object would have a method setValue on the JavaScript side that I want to be able to call from the serverside.

Basically inside my extension I create another object that I need to connect to the server:

window.my_custom_package_CustomClass = function() {
    var myOtherObject = thirdParty.buildOtherObject();

    // How to connect for RPCs?

Is there a way to do this?