Using the Agent SDK
The Connectifi Agent SDK allows your web app to connect to the Connectifi Platform and to interact with other Connectifi-enabled apps using FDC3-compliant methods. By integrating the SDK, your application can subscribe to context broadcasts, raise intents, and perform actions like broadcasting context updates or invoking external services. This gives you the capability to build rich interoperability workflows while leveraging industry standards.
Integration Steps
-
Installation and Import
• Include the Connectifi Agent SDK in your project.
• Import the createAgent method from the Connectifi-hosted bundle. -
Creating the Agent
• Initialize the agent by calling createAgent with the platform URL and your app’s directory identifier.
• Once created, the Agent SDK instance provides FDC3 methods (broadcast, addContextListener, raiseIntent, etc.). -
Registering Listeners
• Use addContextListener to listen for incoming context data.
• Optionally, register intent listeners with addIntentListener if you need to respond to raised intents. -
Sending Context and Raising Intents
• Broadcast context data to other apps using agent.broadcast.
• Raise an intent (for example, to initiate a workflow or query data) using agent.raiseIntent and handle the result accordingly.
Code Example
Below is a basic TypeScript example to demonstrate these steps:
import { DesktopAgent } from '@finos/fdc3';
import { createAgent } from "https://platform.connectifi.app/agent/main.bundle.js";
// Initialize the Connectifi Agent once your app loads
const initAgent = async () => {
try {
// Connectifi platform URL and your app's directory identifier
const agent: DesktopAgent = await createAgent('https://platform.connectifi.app', 'APP@DIRECTORY');
// Register a context listener for a specific context type
agent.addContextListener('fdc3.someContextType', (context) => {
console.log('Received context:', context);
// Handle the incoming context (for example, update your UI)
});
// Function to broadcast a context message
const broadcastContext = async () => {
const sampleContext = {
type: 'fdc3.someContextType',
data: { message: 'Hello from my Web App' }
};
await agent.broadcast(sampleContext);
};
// Function to raise an intent and process the result
const raiseMyIntent = async () => {
const result = await agent.raiseIntent('SomeIntentName', {
type: 'fdc3.someContextType',
data: { detail: 'Intent payload' }
});
console.log('Intent result:', result);
};
// Optionally, call the functions based on user interaction:
// broadcastContext();
// raiseMyIntent();
} catch (error) {
console.error('Error initializing Connectifi Agent:', error);
}
};
initAgent();
In this example:
• The agent is created by calling createAgent with required parameters.
• A context listener is added to process any incoming messages matching ‘fdc3.someContextType’.
• There are helper functions to broadcast context data and raise an intent.