xFrame API

Host API

XFrameManager

Copy
Copied
const xFrameManager = new XFrameManager()

Properties

Name Type Description Default
hostWindow (optional) DOM Window Object Host window object window
onDataCallback (optional) Function A function that handles incoming data null

XFrameManager.createGuest()

Copy
Copied
const guest = await xFrameManager.createGuest()

Creates a guest agent and appends it to the host agent's peers & to the manager's guest agents array.

Returns: Promise<PeerAgent>

XFrameManager.registerGuest(guestWindow, peerId)

  • Should be used in one of two ways:
javascriptjavascript
Copy
Copied
const iframe = document.getElementById('frameId')
let guest = await xFrameManager.createGuest()
guest = await xFrameManager.registerGuest(iframe.contentWindow, guest.id)
Copy
Copied
const iframe = document.getElementById('frameId')
const guest = await xFrameManager.registerGuest(iframe.contentWindow)

When registering a guest, a communication channel between the host & guest windows is created.

Returns: Promise<PeerAgent>

Parameters

Name Type Description
guestWindow DOM Window Object Guest window object
peerId (optional) string Existing guest ID (useful when there are multiple guests). If not provided, internally uses the createGuest method before registering the guest.

XFrameManager.createFrameByUrl(url)

Copy
Copied
const guest = await xFrameManager.createFrameByUrl('http://guestURL.com')

Creates an HTML iframe and appends it to the host's document body. Internally uses the registerGuest method - a connection is established between the windows.

Returns: Promise<PeerAgent>

Parameters

Name Type Description
url string Guest URL - iframe's src

XFrameManager.removeGuest(peerId)

Copy
Copied
xFrameManager.removeGuest(guest.id)

Removes the guest from the host agent's peers & manager's guest agents array. The guest agent no longer exists, therefore the communication is channel is dead.

Returns: void

Parameters

Name Type Description
peerId string Guest agent ID

PeerAgent

PeerAgent.send(data)

Copy
Copied
const xFrameManager = new XFrameManager()
const guest = await xFrameManager.createFrameByUrl('http://guestURL.com')
guest.send('Hello from host window!')

Sends a message/data from the host window to the guest window (iframe).

Returns: void

Parameters

Name Type Description
data any Data that is being passed in messages between agents.

PeerAgent.sendSync(data)

Copy
Copied
await guest.sendSync({ action: 'createPost', content: 'Hello World' })

Sends a message/data from the host window to the guest window (iframe).

Returns: Promise<any>

Parameters

Name Type Description
data any Data that is being passed in messages between agents.

PeerAgent.sendCode(code)

Copy
Copied
const result = await guest.sendCode(
    "function helloWorld(name) { return 'Hello ' + name }"
)
// result = { message: '', result: 'success' }

Sends a code message from the host window to the guest window (iframe). The code is being loaded into the guest window.

Returns: Promise<IResult>

Parameters

Name Type Description
code string Javascript written-code to be loaded in the guest window

PeerAgent.sendRPC(data)

Copy
Copied
// Load the function 'helloWorld' in the guest window
await guest.sendCode(
    "function helloWorld(firstName, lastName) { return 'Hello ' + name + ' ' + lastName }"
)

// Execute the function 'helloWorld' in the guest window
const rpc = {
    func: 'helloWorld',
    inputs: ['Tom', 'Hanks']
}
const result = await guest.sendRPC(rpc)
// result = { output: 'Hello Tom Hanks', result: 'success', message: ''}

Sends a Remote Procedure Call (RPC) message.

Returns: Promise<IResult>

Parameters

Name Type Description
data EventPayload Custom events

PeerAgent.sendEvent(data)

Copy
Copied
// Send a custom event to the guest window
guest.sendEvent({ name: 'customEventName', payload: 'Test payload' })

Sends an event message to the host.

Returns: void

Guest API

GuestAgent

Copy
Copied
const guest = new GuestAgent()

Properties

Name Type Description Default
win (optional) DOM Window Object Guest window object window
onDataCallback (optional) Function A function that handles incoming data null

Events it can listen for

Name Description
ready Connection has been made with the host
data Incoming data (AgentMessage)
- Custom events
Copy
Copied
guest.on('ready', () => {
    // xFrame is now available
    console.log('I am ready!')
})
guest.on('data', (msg) => {
    if (msg.data === 'hello') {
        console.log('The host says "hello"!')
    }
})
guest.on('customEventName', (payload) => {
    console.log(payload)
})

Interfaces

IResult

Copy
Copied
interface IResult {
    output: any
    result: string
    message: string
}

EventPayload

Copy
Copied
interface EventPayload {
    name: string
    payload: any
}