Data Sharing

Overview

The Shared Contexts API is accessible via the io.contexts object.

See the JavaScript Shared Contexts example on GitHub.

Setting Contexts

To create a shared context or replace entirely the value of an existing one, use the set() method:

const newContext = { backgroundColor: "purple" };

// This will create a new shared context, or if the context already exists,
// will completely overwrite its value.
await io.contexts.set("app-styling", newContext);

The set() method overwrites the existing value of the shared context object, as opposed to the update() method, which only updates the values of its properties.

Getting Contexts

To get the value of a specific shared context, use the get() method:

const data = await io.contexts.get("app-styling");

Listing All Contexts

To get the names of all currently available shared contexts, use the all() method:

// Returns a string array with the available context names.
const availableContexts = io.contexts.all();

Updating Contexts

To update the value of an existing shared context, use the update() method. New properties will be added, existing ones will be updated, and you can also remove shared context keys by setting them to null. If the specified shared context doesn't exist, it will be created:

const contextUpdate = {
    backgroundColor: "red",
    alternativeColor: "green"
};

await io.contexts.update("app-styling", contextUpdate);

To remove a shared context key, set it to null:

const keyToRemove = { alternativeColor: null };

await io.contexts.update("app-styling", keyToRemove);

Updating Specific Properties

You can use the setPath() and setPaths() methods to update specific shared context properties using a dot-separated string path (e.g., "prop1.prop2") to point to the location of the property within the shared context object. If the property (or the path) doesn't exist, it will be created. These methods are useful for updating or creating one or more nested properties within the shared context object.

To update or create a single property, use the setPath() method. It accepts the name of the shared context, a path to the property to update, and a value for the property as required arguments:

const path = "text.color";
const value = "grey";

await io.contexts.setPath("app-styling", path, value);

// Assuming the context already exists and its data has this shape:
// { backgroundColor: "red" }, it will be updated as follows:
// { backgroundColor: "red", text: { color: "grey" } }

To update or create multiple properties, use the setPaths() method. It accepts as required arguments the name of the shared context and a list of PathValue objects each containing a path to the property to update and a value for it:

const updates = [
    { path: "table.cells", value: { width: 50, height: 30 } },
    { path: "text.color", value: "white" }
];

await io.contexts.setPaths("app-styling", updates);

// Assuming the context already exists and its data has this shape:
// { backgroundColor: "red", text: { color: "grey" } }, it will be updated as follows:
//
// {
//     backgroundColor: "red",
//     text: {
//         color: "white"
//     },
//     table: {
//         cells: {
//             width: 50,
//             height: 30
//         }
//     }
// }

Subscribing for Context Updates

To subscribe for context updates, use the subscribe() method. It accepts the name of the shared context as a first required argument and a function that will handle the context updates as a second required argument:

const handler = (context, delta, removed) => {
    const bgColor = context.backgroundColor;

    console.log(bgColor);
};

await io.contexts.subscribe("app-styling", handler);

Unsubscribing

The subscribe() method returns a Promise which resolves with a function you can use to unsubscribe from context updates:

const unsubscribe = await io.contexts.subscribe("app-styling", handler);

unsubscribe();

Destroying Contexts

To destroy a context object, use the destroy() method:

await io.contexts.destroy("app-styling");

Reference

For a complete list of the available Shared Contexts API methods and properties, see the Shared Contexts API Reference Documentation.