Shared Contexts
Shared Contexts are named objects that act as a global state store shared between all applications in the io.Connect environment. They are the primary mechanism for cross-app data synchronization.
fdc3.contact or fdc3.instrument for maximum interoperability.Overview
The Shared Contexts API is accessible via the io.contexts object. Contexts persist even after the publishing app disconnects, ensuring subscriber apps always have the latest state.
Key characteristics:
- Named — each context has a unique string name (e.g.,
"SelectedClient") - Persistent — data remains available after the publisher disconnects
- Global — visible to all apps in the environment
- Mergeable —
update()merges properties;set()replaces entirely
Setting Contexts
Use set() to create a new shared context or completely replace an existing one:
const clientData = {
type: "fdc3.contact",
id: { email: "jane@example.com" },
name: "Jane Smith",
};
// Creates the context, or replaces its value entirely
await io.contexts.set("SelectedClient", clientData);set() overwrites the entire context object. Use update() if you only want to modify specific properties.Updating Contexts
Use update() to merge new properties into an existing context. New properties are added, existing ones are updated. Set a property to null to remove it.
// Merge new data — existing properties are preserved
await io.contexts.update("SelectedClient", {
portfolio: { totalValue: 1250000 },
});
// Remove a property by setting it to null
await io.contexts.update("SelectedClient", {
portfolio: null,
});Updating Specific Paths
For deeply nested updates, use setPath() to target a specific property path:
await io.contexts.setPath(
"SelectedClient",
"portfolio.holdings[0].quantity",
500
);Getting Contexts
Retrieve the current value of a context at any time:
const data = await io.contexts.get("SelectedClient");
console.log(data.name); // "Jane Smith"Subscribing for Updates
Subscribe to receive real-time notifications whenever a context changes. The callback fires immediately with the current value, then on every subsequent update.
const unsubscribe = await io.contexts.subscribe(
"SelectedClient",
(data, delta, removed) => {
console.log("Full context:", data);
console.log("Changed properties:", delta);
console.log("Removed keys:", removed);
}
);
// Later: clean up
unsubscribe();React Pattern
In React, use the useIOConnect hook or useEffect with cleanup:
import { useContext, useState } from "react";
import { IOConnectContext, useIOConnect } from "@interopio/react-hooks";
interface ClientData {
type: string;
id: { email: string };
name: string;
}
function ClientDisplay() {
const [client, setClient] = useState<ClientData | null>(null);
useIOConnect(async (io) => {
const unsub = await io.contexts.subscribe(
"SelectedClient",
(data: ClientData) => setClient(data)
);
return () => unsub();
}, []);
if (!client) return <p>No client selected</p>;
return (
<div>
<h2>{client.name}</h2>
<p>{client.id.email}</p>
</div>
);
}Listing All Contexts
// Returns an array of context names
const contextNames = io.contexts.all();
// ["SelectedClient", "AppSettings", "MarketData"]Destroying Contexts
await io.contexts.destroy("SelectedClient");API Reference
| Parameter | Type | Description |
|---|---|---|
namerequired | string | The unique name of the shared context. |
datarequired | object | The context data object. Supports any serializable structure. |
Best Practices
- Use FDC3-compliant context types (
fdc3.contact,fdc3.instrument) for interoperability - Always return the
unsubscribefunction inuseEffectcleanups - Prefer
update()overset()for partial modifications to avoid overwriting other subscribers' data - Use descriptive context names like
"SelectedClient"rather than generic names - Keep context objects reasonably small — don't store large datasets in contexts