Broadcasting Context
Below is an example that demonstrates how to use the Connectifi Agent SDK to broadcast and receive context data over an application channel. In this workflow, an app creates (or retrieves) a dedicated channel, adds a listener to that channel to receive any incoming context data, and then broadcasts a sample context. This showcases the power of channel-based communication within a Connectifi-enabled application.
The diagram shows that the application first establishes a channel, registers a listener on that channel, and then broadcasts a context. The channel in turn makes sure that all listeners receive the broadcast.
Implementation Examples & Code
Below is a TypeScript code sample illustrating the steps:
import { DesktopAgent, Context } from '@finos/fdc3';
import { createAgent } from 'https://platform.connectifi.app/agent/main.bundle.js';
async function initializeAgent() {
// Create an Agent instance using Connectifi
const agent: DesktopAgent = await createAgent('https://platform.connectifi.app', 'APP@DIRECTORY');
// Get or create a dedicated channel for your application
const channel = await agent.getOrCreateChannel('appChannel');
// Add a listener to receive context data on the channel
channel.addContextListener((context: Context) => {
console.log("Received context data:", context);
});
// Define a sample context to be broadcast. Ensure the context conform to FDC3 2.0 types.
const sampleContext: Context = {
type: 'fdc3.sample.context',
data: {
message: 'Hello, world from Connectifi!'
}
};
// Broadcast the sample context on the channel
await channel.broadcast(sampleContext);
}
// Initialize the agent to set up channel communication
initializeAgent();
In this example:
- An Agent is created from the Connectifi platform.
- A dedicated app channel named “appChannel” is obtained (or created if it doesn’t exist).
- A context listener is registered to handle incoming broadcast messages.
- A sample context is defined and broadcasted via the channel.
Last updated on