Skip to main content
Widget Example

Prerequisites

Before you begin, ensure you have the following:
  • Node.js (v14 or higher) installed. You can download it from Node.js Official Website.
  • npm (comes with Node.js) or Yarn as your package manager.
  • Basic knowledge of React and Next.js.
  • Access to the TakeProfit platform for widget integration and testing.

Set Up a Next.js Project with App Router

Next.js introduces the App Router, which provides a new way to organize your application’s routes and components.
  1. Create a New Next.js Project Open your terminal and run the following command to create a new Next.js project:
    Replace my-tp-widget with your desired project name.
  2. Navigate to Your Project Directory
  3. Verify App Router is Enabled Ensure that your project uses the App Router by checking the presence of the app directory.
    You should see an app directory among others.

Install the TakeProfit Widget SDK

Integrate the TakeProfit Widget SDK into your Next.js project to enable communication between your widget and the TakeProfit platform.
  1. Install the SDK via npm
    Alternatively, if you prefer using Yarn:
  2. Verify Installation Check your package.json to ensure takeprofit-widget-sdk is listed under dependencies:

Create the Widget Page

Create a dedicated page for your widget within the App Router structure.
  1. Create the Widget Directory and Page In the app directory, create a new folder named widget and add a page.jsx file:
  2. Implement the Widget Component Open app/widget/page.jsx and add the following code:
    Explanation:
    • Initialization: The SDK is connected using TPWidgetSDK.connect(), and the loader is hidden with TPWidgetSDK.hideLoader().
    • Subscriptions: The widget subscribes to state changes for both the widget itself and the associated channel.
    • Security Change: Provides buttons to change the displayed security to AAPL or META, demonstrating interaction with the platform.

Configure Next.js Headers for Security

To ensure that your widget can be embedded securely within the TakeProfit platform, you need to configure specific HTTP headers in your Next.js application.
  1. Create or Update next.config.js In the root of your project, create or modify the next.config.js file to include the following configuration:
    Explanation:
    • X-Frame-Options: This header controls whether your site can be embedded in an iframe. Setting it to ALLOW-FROM https://takeprofit.com allows the widget to be embedded only from the specified origin.
    • Content-Security-Policy: The frame-ancestors directive specifies valid parents that may embed a page using <frame>, <iframe>, <object>, <embed>, or <applet>. Setting it to 'self' https://takeprofit.com allows framing from the same origin and the specified TakeProfit domain.

Initialize the SDK and Handle Messages

Ensure that your widget properly handles incoming messages and maintains state accordingly.
  1. Understand the SDK’s Internal Mechanisms The SDK uses postMessage for communication between the widget (iframe) and the parent TakeProfit platform. It handles various message types to manage state and interactions.
  2. No Additional Configuration Needed The SDK automatically manages message listeners upon calling TPWidgetSDK.connect(). However, ensure that your widget’s domain is trusted by the TakeProfit platform to allow communication.

Add Menu Items

Enhance your widget by adding custom menu items to the widget header. Update your WidgetPage component to include functions for adding, updating, and removing menu items.
Explanation:
  • State Management: Uses React’s useState to keep track of the added menu item’s ID.
  • Add Menu Item: Adds a menu item labeled “Refresh Data” with a callback that logs to the console.
  • Update Menu Item: Updates the label of the existing menu item.
  • Remove Menu Item: Removes the existing menu item.

Test Your Widget Locally

Verify that your widget integrates correctly with the TakeProfit platform by testing it in a local environment.
  1. Start the Development Server
    By default, Next.js runs on http://localhost:3000.
  2. Configure Local Storage for Testing To simulate the widget’s presence on the TakeProfit platform, configure local storage in your browser:
    • Open the TakeProfit platform in your browser.
    • Open the developer console (F12 or Ctrl+Shift+I).
    • Execute the following command, replacing <your-iframe-url> and <icon-url> with your actual widget URL and an icon URL:
  3. Reload the TakeProfit Platform Refresh the TakeProfit platform page. Your widget should now appear in the widget panel, allowing you to add it to the dashboard and interact with its functionalities.
  4. Verify Functionality
    • Security Change: Click on the “Set Security” buttons and observe the console logs for successful changes.
    • Menu Items: Add, update, and remove menu items, ensuring that callbacks are executed as expected.

Deploy Your Widget

Once testing is successful, deploy your widget to a live environment.
  1. Build the Project
  2. Start the Production Server
    Ensure that your widget is accessible over HTTPS in a production environment for secure communication with the TakeProfit platform.
  3. Update Local Storage Configuration Replace the iframeUrl in local storage with your deployed widget’s URL:
  4. Final Testing Reload the TakeProfit platform and verify that your deployed widget functions correctly in the live environment.

Best Practices

  • Unique Identifiers: Ensure that requestId and menuItemId are unique to prevent conflicts.
  • Unsubscribe When Necessary: Clean up subscriptions using unsubscribeWidgetChange and unsubscribeChannelChange to prevent memory leaks.
  • Error Handling: Implement robust error handling for all asynchronous operations to enhance user experience.
  • Performance Optimization: Limit the number of active subscriptions and avoid unnecessary state updates to maintain optimal performance.
  • Documentation: Keep your code well-documented to facilitate maintenance and future enhancements.

Next Steps

Congratulations on creating your first widget with the TakeProfit Widget SDK! To further enhance your widget, consider the following:
  • Explore Advanced SDK Features: Dive deeper into the SDK Reference to utilize all available methods and message types.
  • Implement Real-Time Data: Integrate real-time data feeds to provide dynamic and up-to-date information within your widget.
  • Customize the UI: Enhance the user interface with additional React components and styling to improve user engagement.
  • Add More Interactions: Implement more interactive features like custom events, notifications, and user preferences.
  • Optimize: Ensure your widget is responsive and functions seamlessly across different devices and screen sizes.
For more detailed information, refer to the complete SDK Reference and other documentation resources provided by the TakeProfit Widget SDK.