npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@uipath/apps-communication-driver

v2.0.8

Published

A lightweight and secure communication library for cross-window messaging between parent windows and iframes.

Downloads

38

Readme

UiPath Communication Driver

A lightweight and secure communication library for cross-window messaging between parent windows and iframes.

Features

  • Secure cross-window communication with origin validation
  • Promise-based handshake mechanism with configurable timeout
  • Event-based message passing
  • Channel caching for improved performance
  • TypeScript support

Installation

npm install @uipath/apps-communication-driver
# or
yarn add @uipath/apps-communication-driver

Usage

Parent Window Integration

Here's how to integrate the communication-driver in your parent window:

import { handshake } from '@uipath/apps-communication-driver;

// Create an iframe element
const iframe = document.createElement('iframe');
iframe.src = 'https://your-child-origin.com';
document.body.appendChild(iframe);

// Initialize communication
const { channel } = await handshake({
  targetOrigin: 'https://your-child-origin.com',
  iframe: iframe,
  timeout: 10000 // Optional: Set a custom timeout in milliseconds (default: 5000ms)
});

// Listen for messages from the child
channel.on('someEvent', (data) => {
  console.log('Received from child:', data);
});

// Send messages to the child
channel.send('someEvent', { message: 'Hello from parent!' });

Reference Implementation Example

Here's a complete example of a parent window implementation:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Parent Window</title>
</head>
<body>
    <h1>Parent Window</h1>
    <div style="display: flex; gap: 16px; margin-bottom: 16px;">
        <input type="text" id="messageInput" placeholder="Enter your message">
        <input type="text" id="eventName" placeholder="Enter event name">
        <button onclick="sendMessage()">Submit</button>
    </div>
    <iframe id="app-frame"
        src="https://your-child-origin.com"
        width="100%" height="800">
    </iframe>

    <script>
        var channel;
        function sendMessage() {
            const messageInput = document.getElementById('messageInput');
            const eventName = document.getElementById('eventName').value;
            const message = messageInput.value;
            if (message.trim()) {
                channel.send(`${eventName}`, { message });
                messageInput.value = '';
            }
        }
    </script>
    <script type="module">
        import { handshake } from './driver.js';

        try {
            const iframe = document.getElementById("app-frame");
            const { channel: localChannel } = await handshake({ 
                targetOrigin: "https://your-child-origin.com", 
                iframe, 
                targetWindow: iframe.contentWindow,
                timeout: 10000 // Optional: Set a custom timeout in milliseconds
            });
            channel = localChannel;
            
            // Listen for messages from the child
            channel.on("EVENT_FROM_APP", (data) => {
                console.log("Received from App:", data);
            });

        } catch (error) {
            console.error('Communication error:', error);
        }
    </script>
</body>
</html>

This example demonstrates:

  • Setting up an iframe with a specific source
  • Creating input fields for message and event name
  • Implementing a send message function
  • Properly initializing the communication channel with error handling
  • Setting up event listeners for receiving messages

Child Window (iframe) Integration

In your child window or iframe:

import { handshake } from '@uipath/apps-communication-driver';

// Initialize communication with parent
const { channel } = await handshake({
  targetOrigin: 'https://your-parent-origin.com',
  timeout: 10000 // Optional: Set a custom timeout in milliseconds
});

// Listen for messages from the parent
channel.on('someEvent', (data) => {
  console.log('Received from parent:', data);
});

// Send messages to the parent
channel.send('someEvent', { message: 'Hello from parent!' });

API Reference

Handshake Options

interface HandshakeOptions {
  targetOrigin: string;      // The expected origin for messages
  targetWindow?: Window;     // The window to communicate with (optional)
  iframe?: HTMLIFrameElement; // Optional iframe instance for communication
  timeout?: number;          // Optional timeout in milliseconds (default: 5000ms)
}

Channel Interface

interface Channel {
  send: (event: string, data: any) => void;
  on: (event: string, callback: (data: any) => void) => void;
}

Security Considerations

  • Always specify the correct targetOrigin to ensure messages are only accepted from trusted sources
  • The library automatically validates message origins
  • Messages are only processed if they match the expected origin

Best Practices

  1. Error Handling: Always wrap the handshake in a try-catch block:

    try {
      const { channel } = await handshake({
        targetOrigin: 'https://your-child-origin.com',
        iframe: iframe,
        timeout: 10000 // Set an appropriate timeout for your use case
      });
    } catch (error) {
      console.error('Failed to establish communication:', error);
      // Handle timeout errors specifically
      if (error.message.includes('timed out')) {
        console.error('Handshake timed out. The other party did not respond in time.');
      }
    }
  2. Event Naming: Use consistent and descriptive event names across your application

  3. Data Validation: Validate data received through the channel before processing

  4. Cleanup: Remove event listeners when they're no longer needed:

    channel.on('someEvent', null); // Remove the listener
  5. Timeout Configuration: Set an appropriate timeout value based on your application's needs:

    • For local development, shorter timeouts (1000-2000ms) can help identify issues faster
    • For production, consider network latency and set timeouts accordingly (5000-10000ms)
    • For high-latency environments, you may need longer timeouts

Browser Support

The library uses the standard postMessage API and is supported in all modern browsers:

  • Chrome 4+
  • Firefox 3+
  • Safari 4+
  • Edge 12+
  • IE 8+

License

MIT