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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pavelkv94/microfi

v0.0.8

Published

Microfrontend on iframes

Downloads

456

Readme

Microfrontend Communication Library

This library provides two React hooks, useHostBus and useMicrofrontendBus, to facilitate communication between a host application and microfrontend applications using iframes.

Overview

  • useHostBus: Used in the host application to manage communication with registered iframes.
  • useMicrofrontendBus: Used in microfrontend applications to communicate with the host.

Installation

To install the library, add it to your project via npm or yarn:

npm install @pavelkv94/microfi

or

yarn add @pavelkv94/microfi

For React, it is advisable to remove strict mode

useHostBus

Description

useHostBus is a React hook that helps the host application register iframes and send actions to them.

Usage

  1. Initialize the Hook

import { useHostBus } from "@pavelkv94/microfi";

const originsWhiteList = ["http://localhost:5001", "http://localhost:5002"];
const { sendToRemote, registerIframe } = useHostBus(originsWhiteList);` 
  1. Register Iframes

    In the host app, you can register iframes and handle incoming messages using the registerIframe method.

    useEffect(() => {
    const messageHandler = (event: MessageEvent) => {
      if (originsWhiteList.includes(event.origin)) {
        // Handle incoming messages from iframes
        try {
          const action: Action = JSON.parse(event.data);

          switch (action.type) {
            case "IFRAME-LOADED":
              registerIframe(event); // Register the iframe if it exist
              break;

            //handle incoming messages from remote microfrontend
            case "CUSTOM_ACTION":
              setDataFromRemote(action.payload.data); // process data from remote microfrontend
              break;

            default:
              console.warn("Unhandled action type:", action.type);
          }
        } catch (error) {
          console.error("Failed to handle message:", error);
        }
      } else {
        console.warn("Blocked message from untrusted origin:", event.origin);
      }
    };

    window.addEventListener("message", messageHandler);

    return () => {
      window.removeEventListener("message", messageHandler);
    };
  }, [registerIframe, sendToRemote]);
  1. Send Actions to Iframes

    You can use the sendToRemote method to send actions to all registered iframes.

    
    const sendToken = () => {
      const action: Action = {
        type: "TOKEN_CREATED",
        payload: { token: "XXX-YYYY" },
      };
    
      sendToRemote(action);
    };

Example

import { useHostBus } from "@pavelkv94/microfi";
import { useEffect, useState } from "react";

interface Action {
  type: string;
  payload?: any;
}

function App() {
  const originsWhiteList = ["http://localhost:5001", "http://localhost:5002"]; // trusted remote microfrontend addresses
  const { sendToRemote, registerIframe } = useHostBus(originsWhiteList);

  const [dataFromRemote, setDataFromRemote] = useState(); // save data from remote microfrontend

  useEffect(() => {
    const messageHandler = (event: MessageEvent) => {
      if (originsWhiteList.includes(event.origin)) {
        // Handle incoming messages from iframes
        try {
          const action: Action = JSON.parse(event.data);

          switch (action.type) {
            case "IFRAME-LOADED":
              registerIframe(event); // Register the iframe if it exist
              break;

            //your custom action from remote microfrontend
            case "CUSTOM_ACTION":
              setDataFromRemote(action.payload.data); // process data from remote microfrontend
              break;

            default:
              console.warn("Unhandled action type:", action.type);
          }
        } catch (error) {
          console.error("Failed to handle message:", error);
        }
      } else {
        console.warn("Blocked message from untrusted origin:", event.origin);
      }
    };

    window.addEventListener("message", messageHandler);

    return () => {
      window.removeEventListener("message", messageHandler);
    };
  }, [registerIframe, sendToRemote]);

  const sendToken = () => {
    //custom action
    const action: Action = {
      type: "TOKEN_CREATED",
      payload: { token: "XXX-YYYY" },
    };

    sendToRemote(action); // Send the action to all registered iframes
  };

  return (
    <div>
      HOST APP
      <p>Data from remote: {dataFromRemote}</p>
      <button onClick={() => sendToken()}>Send Token all registered iframes</button>
      <iframe src="http://localhost:5001/" style={{ width: "300px", height: "300px", border: "1px solid red" }}></iframe>
    </div>
  );
}

export default App;

useMicrofrontendBus

Description

useMicrofrontendBus is a React hook used in microfrontend applications to communicate with the host.

Usage

  1. Initialize the Hook

import { useMicrofrontendBus } from "@pavelkv94/microfi";

const { registerMeInHost, sendToHost, subscribe } = useMicrofrontendBus("http://localhost:5000");

  1. Register the Iframe

    Call registerMeInHost to notify the host that the iframe is loaded.

useEffect(() => {
  registerMeInHost();
}, [registerMeInHost]); 
  1. Subscribe to Actions

    Use the subscribe method to listen for specific actions from the host.

useEffect(() => {
  const unsubscribe = subscribe("TOKEN_CREATED", (action: Action) => {
    console.log("Received token:", action.payload.token);
  });
    
  return () => {
    unsubscribe(); // Clean up subscription
  };
}, [subscribe]); 
  1. Send Actions to the Host

    Use the sendToHost method to send actions to the host application.

const notifyHost = () => {
  const action: Action = {
    type: "CUSTOM_ACTION",
    payload: { data: "some data" },
  };
    
  sendToHost(action);
};

Example

import React, { useEffect, useState } from "react";
import { useMicrofrontendBus } from "@pavelkv94/microfi";

interface Action {
  type: string;
  payload?: any;
}

function App() {
  const [token, setToken] = useState<string | undefined>(undefined); // process data from host

  const { registerMeInHost, sendToHost, subscribe } = useMicrofrontendBus("http://localhost:5000"); //trusted host address

  useEffect(() => {
    registerMeInHost();
    // subscribe to action(events) from host
    const unsubscribe = subscribe("TOKEN_CREATED", (action: Action) => {
      setToken(action.payload.token);
    });

    return () => {
      unsubscribe(); // Clean up subscription
    };
  }, [registerMeInHost, subscribe]);

  const notifyHost = () => {
    const action: Action = {
      type: "CUSTOM_ACTION",
      payload: { data: "some data" },
    };

    sendToHost(action);
  };

  return (
    <>
      <p>Token from host is {token}</p>
      <button onClick={notifyHost}>notify Host</button>
    </>
  );
}

export default App; 

Summary

  • Use useHostBus in the host application to manage and communicate with iframes.
  • Use useMicrofrontendBus in microfrontend applications to communicate with the host.

Feel free to adjust the types and implementations based on your specific use case and payload structure.