How SharedTree supercharges and simplifies Fluid Framework 2.0

Nick Simons

Fluid Framework is a platform for building distributed, real-time, collaborative applications. It allows you to create rich and interactive experiences that can sync data across multiple clients and devices. However, Fluid Framework 1.0 could be challenging when managing the state of very complex and dynamic data.

That’s where SharedTree in Fluid Framework 2.0 comes in. SharedTree is a new distributed data structure (DDS) that is designed to make it even easier to build collaborative applications. SharedTree is a tree-like data structure that can store any kind of data you might represent as JSON or a TypeScript object model. It includes a new, intuitive API that makes working with distributed data as simple as working with TypeScript data structures. And, as with all Fluid data, SharedTree handles data synchronization automatically.

SharedTree is different

SharedTree allows you to create a scalable, hierarchical data model from objects, maps, and arrays. You can model very complex data in a single DDS. This makes managing changes to the data – especially moving data around – much easier.

Further, the SharedTree data model is defined by schema. This schema allows you to use Fluid data structures the same way you would use other TypeScript data structures. You can define classes in your SharedTree schema and use them just like any other TypeScript class – except that the data is automatically kept in sync between clients.

Schema also makes development safer and easier. SharedTree classes are typed which enables autocomplete and IntelliSense in IDEs like Visual Studio Code. And since these are normal TypeScript classes, you can include your own custom methods to make working with data more convenient. For example, you could create a coordinate object with an x property and and a y property that is synched between clients. You could then include methods for moving left, right, up, and down directly in the class.

export class Position extends factory.object('Position', {
    x: factory.number,
    y: factory.number,
}) {
    public moveLeft() {
        this.x -= 1;
    }

    public moveRight() {
        this.x += 1;
    }

    moveUp() {
        this.y -= 1;
    }

    moveDown() {
        this.y += 1;
    }
}

A significant addition to Fluid Framework 2.0 is full support for distributed arrays. SharedTree allows you to define arrays that can hold anything you define using SharedTree schema. SharedTree arrays include sophisticated merge semantics so that concurrent changes to an array will be merged rationally and without data loss. Arrays support insert and remove operations, but they also support move operations both within an array and between two arrays. This means that moves are handled atomically such that concurrent moves never result in duplicated content or incoherent merge outcomes.

The experience of building a Fluid Framework app using SharedTree almost feels like cheating. Defining schema is as simple as defining TypeScript classes and once defined, you can build a complex hierarchical data model with simple TypeScript code. And because these are Fluid data structures, the data is automatically persisted to storage and synchronized between all clients. There is no need for separate file I/O code or database code. Further, if you need to export your data, the SharedTree data is designed to export to JSON very cleanly.

What the future holds

Fluid Framework 2.0 with SharedTree is already a very powerful tool for building collaborative applications. But we are just getting started. We are actively working on building support for a collaborative text data type which will make building experiences that support real-time typing much easier. We will be adding support for tabular data. We are also making significant investments to allow SharedTree to scale past the limits of client memory by allowing you to load just the parts of the tree you need.

Our goal for SharedTree is to make using Fluid Framework as approachable as possible while delivering a powerful tool for modeling collaborative data. We love using it ourselves and the early feedback has been extremely positive. We’re excited to see what you build with SharedTree and we’d love to get your feedback!

To get started with Fluid Framework and SharedTree visit https://aka.ms/fluid.

Follow us on X (Twitter) / @Microsoft365Dev and subscribe to our YouTube channel to stay up to date on the latest developer news and announcements.

0 comments

Discussion is closed.

Feedback usabilla icon