Data Sharing Between Apps

Overview

A shared context is a named object (holding a map of key/value pairs) that stores cross app data. The context object can hold any cross-app data. Any app can update a context or subscribe for context updates and react to them by using the name of the context.

The Shared Contexts API offers a simple and effective solution for sharing data between your apps. Imagine you have an app showing a list of clients and an app showing client portfolios. What you need, is your "Portfolio" app to show the portfolio of a specific client that the user has selected from the "Clients" app. You can easily achieve this in a few simple steps by using the Shared Contexts API:

  • instruct the "Clients" app to publish updates to a context object holding the id of the currently selected client;
  • instruct the "Portfolio" app to subscribe for updates of that same context object and specify how the "Portfolio" app should handle the received data in order to update its current state;

The Live Examples section demonstrates using the Shared Contexts API. To see the code and experiment with it, open the embedded examples directly in CodeSandbox.

Retrieving Context Data

The Shared Contexts API is accessible through the glue.contexts object.

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 = glue.contexts.all();

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

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

Updating a Context

Use the update() method to create a new shared context or update the properties of an existing shared context. New properties (context keys) will be added, existing ones will be updated, and you can also remove context keys by setting them to null.

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

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

To remove keys, send a context update and set them to null:

const keysToRemove = { alternativeColor: null };

await glue.contexts.update("app-styling", keysToRemove);

Replacing a Context

Other than updating a context, you have the option to replace its value completely by using the set() method:

const newContext = { backgroundColor: "purple" };

// This will completely overwrite the existing context value.
await glue.contexts.set("app-styling", newContext);

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

Subscribing for Context Updates

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

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

    console.log(bgColor);
});

await glue.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 glue.contexts.subscribe("app-styling", handler);

unsubscribe();

Destroying a Context

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

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

Live Examples

Setting and Getting Context

The apps below demonstrate how to set and get context using the get() and set() methods of the Shared Contexts API.

Create a value in App B (any string) that will be assigned to a pre-defined property of the context object and set the "G42Core" context by clicking the "Set Context" button. Click "Get Context" in App A to print the current value of the shared context object.

Open in CodeSandbox

Subscribing for Context Updates

The apps below demonstrate how to update a shared context object and how to subscribe for updates of a context by using the update() and subscribe() methods of the Shared Contexts API.

Click the "Subscribe" button in App A to subscribe for updates of the "G42Core" context. Every time the "G42Core" context changes, the context value will be printed. Create a context value and click the "Update Context" button in App B to update the "G42Core" context.

Open in CodeSandbox

Discovering Contexts

The apps below demonstrate how to get a list of all contexts and find a specific context by name.

Create several contexts with different names from App B. Input the name of the context you want to find in App A and click the "Find Context" button to print the context.

Open in CodeSandbox

Reference

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