Krestianstvo SDK 4 | Implementing Croquet and Recursive Portals on Solid JS
Tags: webdev, javascript, webxr, opensource
Published: 2022-09-01 04:42:02

Let me introduce to you the new **4th** version of **[Krestianstvo SDK](https://play.krestianstvo.org/)**!

The main feature of the 4th version is the full-featured implementation of the [**Croquet Application Architecture**](https://croquet.io/) in **Functional Reactive Paradigm** using **[SolidJS](https://www.solidjs.com/) / [S.JS](https://github.com/adamhaile/S)** and introduction of **Recursive Portals**. The only parts, that stayed the same as in the 3rd version of Krestianstvo SDK are: Reflector and Virtual Time. Their implementations are based on the [Virtual World Framework](https://github.com/virtual-world-framework/vwf), thus they were just slightly modified to suit Functional Reactive Paradigm.

{% embed https://vimeo.com/742885266  %}

The 4th version of Krestianstvo SDK is distributed as a standalone [ESM module](https://www.npmjs.com/package/krestianstvo) for JavaScript. It could be easily bundled with any [Solid JS](https://www.solidjs.com/) or [Astro](https://astro.build/) web application being hosted and running just in the Cloud.

The work on the 4th version was started in 2019, when I was working on [Luminary](https://blog.krestianstvo.org/en/krestianstvo-luminary-for-open-croquet-architecutre-and-virtual-world-framework-in-peer-to-peer-web/) and introduced the Functional Reactive Streams into the Croquet application architecture. [The project with Rubik’s Cube](https://blog.krestianstvo.org/en/collaborative-robots-rubiks-cube-lego-boost/) has shown, how Croquet gets a new look on Virtual Time and Promises inside actions, all backed by FRP. For example, the participants could delay the Virtual Time and observe their actions in virtual world in “slow motion”.

Then in 2021 I participated in the first [Festival for Web based Music](https://browsersound.com/). At the festival me and artist Delia Ramos were creating the web art project — [“THIS IS NOT A CONCERT”](https://concert.livecoding.space/). In that project, an audience collaboratively explored the artwork inside virtual canvas space within multi-contextual / conceptual creative layers by touching the virtual objects, interacting or viewing through “filters” in Web Browser. The core object, that I needed to for that project was a Recursive Portal, that should work as a filter, portal to other virtual canvas spaces and also as a portal to itself.

But, comparable to the original Smalltalk version of the Croquet, as in the Virtual World Framework and in the 3rd version of Krestianstvo SDK there are still lack of Portals, including dependence on global object Window. The [Croquet.io](https://croquet.io/) has introduced Portals for the Web, by using sandboxed HTML iFrames ([Multiplane Portals for the Metaverse](https://davidasmith.medium.com/multiplane-portals-for-the-metaverse-a4b7af913e8e)). But in Krestianstvo I wanted to implement Portals the same way as Croquet Smalltalk version does, as pure objects with an ability to make links to themselves, forming a Recursive Portals.

Finally, I was lucky to do it just in pure JavaScript, thanks to Solid JS! To make these all happen, it demanded of me to implement the Croquet application architecture nearly from scratch using functional reactive paradigm in Solid JS / S.JS.

{% embed https://vimeo.com/745245330 %}

The typical application in the 4th version of [Krestianstvo SDK](https://play.krestianstvo.org/) is an application, that will be build up from Solid JS main types of building blocks: Components and Reactive Primitives. Solid’s fine-grained reactivity is built on three core primitives: Signals, Memos, and Effects. Together, they form an auto-tracking synchronization engine. Reactive computations take the form of function-wrapped expressions that execute synchronously ([more in SolidJS documentation](https://www.solidjs.com/guides/getting-started#learn-solid)).

Krestianstvo uses Solid JS reactivity for implementing Croquet’s concept of Model — View interconnection. Croquet Models — are just pure Solid JS Signals, Memos and Stores. Croquet Views — are just Solid JS lightweight Components with no state and no instances. All received external messages from Reflector and all internal Future messages, that responsible for actions are wrapped in a Signals and then dispatched by an Effects accordingly. For example, Smalltalk like “**doesNotUnderstand**” message is easily realised in Krestianstvo.

Here is an example of simple working app in Krestianstvo SDK | 4 (run it on [codesandbox](https://codesandbox.io/s/krestianstvo-helloworld-pnimfu))

```js
import { render } from "solid-js/web";
import { Selo, createLocalStore } from "krestianstvo";

const Counter = (props) => {
  const [local, setLocal] = createLocalStore(
    {
      data: {
        type: "Node",
        nodeID: props.nodeID,
        properties: {
          name: props.nodeID,
          count: 0,
          ticking: false,
          initialized: false
        }
      }
    },
    props
  );

  const inc = () => {
    setLocal("data", "properties", "count", (c) => c + 1);
    props.selo.future(props.nodeID, "inc", 1);
  };

  const initialize = () => {
    inc();
  };

  props.selo.createAction(props.nodeID, "inc", inc);
  props.selo.createAction(props.nodeID, "initialize", initialize);

  return (
    <>
      <h1>{local.data.properties.count}</h1>
    </>
  );
};

render(
  () => (
    <Selo
      nodeID={"counter"}
      seloID={"sandbox_counter"}
      component={Counter}
      reflectorHost={"https://time.krestianstvo.org"}
    />
  ),
  document.getElementById("root")
);
 ```

To be continued…


Demo playground: https://play.krestianstvo.org
Glitch demo project: https://krestianstvo-playground.glitch.me

The source code for **Krestianstvo SDK 4**: 

* Playground: [GitHub](https://github.com/NikolaySuslov/krestianstvo-playground)
* Core library on Solid JS: [GitHub](https://github.com/NikolaySuslov/krestianstvo)
* Prototype on S.js: [GitHub](https://github.com/NikolaySuslov/krestianstvo-s.js)
* Reflector server: [GitHub](https://github.com/NikolaySuslov/lcs-reflector)