Skip to content

DomoApps/JSAPIExample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

8 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Domo Embed Frames Example

This project demonstrates how to create a custom application that embeds Domo cards and utilizes postMessage communication between your custom frames and Domo's embedded analytics.

๐ŸŽฏ Purpose

This instructional example teaches developers how to:

  • Embed Domo cards in custom applications
  • Implement bidirectional communication using postMessage API
  • Handle different types of messages and events
  • Create a robust communication framework for embedded analytics

๐Ÿš€ Features

1. Embedded Card Component

  • Configurable card embedding with custom dimensions
  • Automatic iframe setup with security considerations
  • Real-time card configuration updates

2. PostMessage Communication

  • Send messages to embedded cards (filters, refresh commands, custom events)
  • Receive messages from embedded cards (user interactions, data updates)
  • Real-time message logging and debugging

3. Interactive Controls

  • Apply filters to embedded cards
  • Trigger data refreshes
  • Export data functionality
  • Send custom events and commands

4. Message Logger

  • Real-time logging of all postMessage communications
  • Message type categorization (sent/received/system)
  • Timestamped message history
  • JSON data formatting and display

Addressing TODO Comments

This project includes several // TODO: comments to guide further development. Below are the instructions for addressing them:

  1. Page Filters Manager (src/components/page-filters-manager/index.tsx):

    • Line 24 & 46: Update the example filter logic to match the filters you want to apply in your application. Replace the placeholder filter (Talent_Class) with the actual column, data type, and values relevant to your use case.
      const newFilter: PageFilter = {
        column: 'Your_Column_Name',
        columnType: ColumnType.STRING, // Adjust based on your data type
        dataType: PageFilterDataType.String, // Adjust based on your data type
        label: 'Your Label',
        operand: PageFilterOperator.In, // Adjust based on your filter operation
        values: ['Value1', 'Value2'], // Replace with actual filter values
      };
  2. App Component (src/components/app/index.tsx):

    • Line 53: Update the iframeUrl property of the EmbeddedCard component to use the correct Domo instance and card embed IDs for your application.
      <EmbeddedCard
        iframeUrl="https://your-instance.domo.com/embed/card/private/your-embed-id"
        title="Your Card Title"
        width={1000}
        height={400}
      />

๐Ÿ›  Setup Instructions

Prerequisites

  • Node.js (v18 or higher)
  • Yarn package manager
  • Domo Developer Account
  • Access to Domo instance with embedding enabled
  • Ryuu (DomoApps CLI) installed

Installation

  1. Clone and install dependencies:

    git clone <repository-url>
    cd embed-frames-example
    yarn install
  2. Import The Sample Data:

    • CSV is located in the docs folder
    • Upload as a new dataset in your instance
    • Make a note of the dataset ID for later use
  3. Login to your instance with Ryuu:

    domo login
  4. Update configuration:

    • Edit src/components/embedded-card/index.tsx
    • Replace your-domo-instance with your actual Domo instance name
    • Update the domoInstance prop in the EmbeddedCard component
  5. Run the development server:

    yarn start
  6. Upload to Domo (optional):

    yarn upload

๐Ÿ“– Usage Guide

Basic Usage

  1. Enter a Card ID: Input a valid Domo card ID in the configuration section
  2. Configure the card: Set title and display options
  3. View the embedded card: The card will load in an iframe
  4. Test communication: Use the control buttons to send messages

PostMessage API Examples

Sending Messages to Cards

// Filter application
iframe.contentWindow.postMessage(
  {
    type: 'FILTER_CHANGE',
    data: { filters: { region: 'North America' } },
  },
  '*',
);

// Data refresh
iframe.contentWindow.postMessage(
  {
    type: 'REFRESH_DATA',
  },
  '*',
);

// Custom events
iframe.contentWindow.postMessage(
  {
    type: 'CUSTOM_EVENT',
    data: { action: 'highlight', value: 'top-performers' },
  },
  '*',
);

Receiving Messages from Cards

window.addEventListener('message', (event) => {
  // Validate origin for security
  if (event.origin !== 'https://your-domo-instance.domo.com') return;

  switch (event.data.type) {
    case 'CARD_CLICKED':
      console.log('User clicked on card:', event.data);
      break;
    case 'DATA_UPDATED':
      console.log('Card data updated:', event.data);
      break;
    case 'FILTER_APPLIED':
      console.log('Filter was applied:', event.data);
      break;
  }
});

๐Ÿ”’ Security Considerations

Origin Validation

// Always validate message origins in production
if (event.origin !== 'https://your-domo-instance.domo.com') return;

Iframe Sandbox

<iframe
  sandbox="allow-scripts allow-same-origin allow-forms allow-presentation"
  src="{embedUrl}"
/>

Content Security Policy

Configure CSP headers to allow embedding from trusted Domo domains.

๐Ÿ“‹ Message Types Reference

Standard Messages (sent to cards)

  • FILTER_CHANGE: Apply filters to card data
  • REFRESH_DATA: Trigger data refresh
  • EXPORT_DATA: Request data export
  • CARD_INIT: Initial card configuration
  • CUSTOM_EVENT: Custom application events

Standard Messages (received from cards)

  • CARD_LOADED: Card finished loading
  • CARD_CLICKED: User interaction with card
  • DATA_UPDATED: Data source updated
  • FILTER_APPLIED: Filter successfully applied
  • ERROR: Error occurred in card

๐Ÿ”ง Customization

Adding New Message Types

  1. Update the message type constants
  2. Add handlers in the main App component
  3. Update the embedded card component if needed
  4. Add corresponding UI controls

Styling Customization

  • Modify SCSS files in each component directory
  • Update global styles in src/styles/
  • Customize color scheme in src/styles/variables.scss

๐Ÿ› Troubleshooting

Common Issues

  1. Card not loading

    • Verify card ID is correct
    • Check Domo instance URL
    • Ensure embedding is enabled for the card
  2. Messages not being received

    • Verify origin validation settings
    • Check browser console for errors
    • Ensure postMessage syntax is correct
  3. CORS errors

    • Configure proper CORS headers on Domo instance
    • Verify iframe sandbox permissions

๐Ÿ“š Additional Resources

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

Available Scripts

In the project directory, you can run:

yarn generate

Allows you to generate components or reducers.

Components

The command yarn generate component will generate a new component and add it to the components folder of your project. There are 3 parameters to the component generator that you will be prompted for if you do not provide them inline:

  1. Component Name
  2. Whether or not you would like to include a test file (y/n)
  3. Whether or not you would like to include a storybook file (y/n)

You can provide these parameters inline if you want: yarn generate component myComponent y n or in part yarn generate component myComponent. Any parameter that you do not provide will cause the plop generator to prompt you for an answer.

Reducers

The reducer generator only has one parameter, its name. You can generate a reducer using the command yarn generate reducer myReducer. If you do not provide a name you will be prompted for one. Generating a reducer will produce the following modifications to your project:

  1. A new folder will be created in the actions directory of your project and an index.ts file will be added to it with some boiler plate examples of creating actions using Redux Toolkit.
  2. A new folder will be created in the reducers directory of your project and an index.ts file will be added to it with a basic reducer wired up with a default case. Handle new cases by adding `.addCase()` to the builder object provided. More info can be found in the Redux Toolkit documentation.
  3. The index.ts file in the base of the reducer folder will be modified to import your new reducer and wire it up. As long as you always create reducers using the generator command, you should never need to touch this file.

yarn start

Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.

The page will reload if you make edits.
You will also see any lint errors in the console.

yarn test

Launches the test runner in the interactive watch mode.
See the section about running tests for more information.

yarn build

Builds the app for production to the build folder.
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.
Your app is ready to be deployed!

See the section about deployment for more information.

yarn storybook

Starts up a storybook server to host any components that have been generated with a storybook file.

yarn eject

Note: this is a one-way operation. Once you eject, you canโ€™t go back!

If you arenโ€™t satisfied with the build tool and configuration choices, you can eject at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except eject will still work, but they will point to the copied scripts so you can tweak them. At this point youโ€™re on your own.

You donโ€™t have to ever use eject. The curated feature set is suitable for small and middle deployments, and you shouldnโ€™t feel obligated to use this feature. However we understand that this tool wouldnโ€™t be useful if you couldnโ€™t customize it when you are ready for it.

Learn More

You can learn more in the Create React App documentation.

To learn React, check out the React documentation.

About

Example of how to use postMessages with JS API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published