@amadeus-it-group/microfrontends

中文文档
0.0.11发布于 2026/06/08MITTypeScriptESM + CJS零依赖近 30 天下载 1883

Amadeus Micro Frontend Toolkit

原始使用文档
文档目录11 节
  1. Messaging
  2. Features
  3. Common use-cases
  4. Creating a Message Peer
  5. Connecting to another peer
  6. Listening for connections
  7. Declaring Message types
  8. Sending and receiving messages
  9. Service messages
  10. Logging
  11. Information about the network

Amadeus Toolkit for Micro Frontends

Messaging

The Amadeus Toolkit for Micro Frontends provides a messaging system that allows micro frontends to communicate with each other. The messaging system is based on the Channel Messaging API and works across iFrames. It can also be used in the same document for talk to MFE packaged as a Web Component for example.

Features

schema.svg

  • typed and versioned message exchange between MessagePeers
  • broadcasting messages across all connected micro frontends (ex. MF1 to everybody)
  • sending messages between two specific micro frontends (ex. MF4 to MF3)
  • lifecycle messages (ex. MF5 disconnected, MF3 connected)
  • message validation before sending and upon reception

Common use-cases

Creating a Message Peer

You can create several message peers and connect them to each other in any way avoiding loops. You need to provide some options when creating a peer. Only id is technically required.

import { Message, MessagePeer } from '@amadeus-it-group/microfrontends';

// 'one' is unique identifier for this peer in the network
const peer = new MessagePeer({ id: 'one' });

Connecting to another peer

A peer can either wait for incoming connections from another peer via .listen() or initiate a connection itself via .connect().

import { MessagePeer } from '@amadeus-it-group/microfrontends';

// Create two peers.
// First peer waits for any incoming connection
const one = new MessagePeer({ id: 'one' });
one.listen();

// Second peer connects to the first one
const two = new MessagePeer({ id: 'two' });
const disconnect = two.connect('one');

// if connection crosses iFrames, you might need to provide
// expected window and origin for `connect` and `listen`  methods
two.connect('one', {
  window: oneWindow,
  origin: 'https://example.com',
});

// Disconnecting
disconnect(); // 'two' disconnects from 'one'
one.disconnect('two'); // 'one' disconnects from 'two'
one.disconnect(); // 'one' disconnects from all connected peers

Listening for connections

A peer can decide which incoming connections to accept. Simplest usage is to listen for all connections without any filtering:

const stop = peer.listen(); // start listening
stop(); // stop listening; can restart anytime

More advanced usage allows you to filter incoming connections based on the peer id, source window, origin or a custom logic.

// 1. Using simple peer ids
peer.listen('two');
peer.listen(['two', 'three']);

// 2. Using matching objects
// ex. from peer 'two' from a specific iframe of the expected origin
peer.listen({
  id: 'two',
  source: iframe.contentWindow,
  origin: 'https://example.com',
});

// ex. any connection from this particular iframe
peer.listen({
  source: iframe.contentWindow,
});

// ex. any connection from this origin
peer.listen({
  origin: 'https://example.com',
});

// 3. Using a combination of ids and objects
peer.listen(['one', 'two', { id: 'three', origin: 'https://example.com' }]);

// 4. Using a custom function
// ex. filter connections based on the handshake message and where it comes from
peer.listen((message, source, origin) => {
  return true; // you decide whether to accept the connection
});

Declaring Message types

This is optional, but allows for type checking during development when sending and receiving messages.

import { Message } from '@amadeus-it-group/microfrontends';

interface HelloMessage_1_0 extends Message {
  type: 'hello';
  version: '1.0';
  greeting: string;
}

interface HelloMessage_2_0 extends Message {
  type: 'hello';
  version: '2.0';
  greetings: string[];
}

export type MyMessage = HelloMessage_1_0 | HelloMessage_2_0;

Sending and receiving messages

import { MessagePeer } from '@amadeus-it-group/microfrontends';

// Receiving messages
const one = new MessagePeer<MyMessage>({ id: 'one' });

// An observable-like interface consuming messages
one.messages.subscribe(({ payload }: MyMessage) => {
  if (payload.type === 'hello') {
    switch (payload.version) {
      case '1.0':
        console.log(payload.greeting); // string
        break;
      case '2.0':
        console.log(payload.greetings); // string[]
        break;
    }
  }
});

// Broadcast a message. Message will be type checked.
two.send({
  type: 'hello',
  version: '1.0',
  greeting: 'Hello, world!',
});

// Send a message to a specific peer. Other peers will not receive it.
two.send(
  {
    type: 'hello',
    version: '2.0',
    greetings: ['Hello', 'world!'],
  },
  {
    to: 'one',
  },
);

Service messages

There are some lifecycle messages (ServiceMessage) that are automatically sent by the library. You can listen to them using the .serviceMessages stream to avoid polluting your own messages in .message stream.

import { MessagePeer, ServiceMessage } from '@amadeus-it-group/microfrontends';

const peer = new MessagePeer({ id: 'one' });

peer.serviceMessages.subscribe(({ payload }: ServiceMessage) => {
  switch (payload.type) {
    case 'handshake':
      // instance of `HandshakeMessage`
      break;
    case 'connect':
      // instance of `ConnectMessage`
      break;
    case 'disconnect':
      // instance of `DisconnectMessage`
      break;
    case 'error':
      // instance of `ErrorMessage`
      break;
  }
});

Logging

Simple logging can be enabled via enableLogging() method. It will log all messages sent and received for debugging purposes.

import { enableLogging } from '@amadeus-it-group/microfrontends';

enableLogging();

Information about the network

// List all known peers and their accepted messages
one.knownPeers; // a map of known peers and messages they accept
one.knownPeers.get('two'); // a list of message types peer 'two' accepts

版本与发布记录

@amadeus-it-group/microfrontends 共发布 11 个版本,首次发布于 2025/02/19;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.0.11latest2026/06/084 个月前
0.0.102025/11/2611 个月前
0.0.92025/09/031.2 年前
0.0.82025/07/011.4 年前
0.0.72025/04/111.6 年前
0.0.62025/04/101.6 年前

关于 @amadeus-it-group/microfrontends 的常见问题

@amadeus-it-group/microfrontends 是什么?

Amadeus Micro Frontend Toolkit

如何安装 @amadeus-it-group/microfrontends?

在项目目录运行 npm install @amadeus-it-group/microfrontends,或使用 pnpm add / yarn add / bun add。

@amadeus-it-group/microfrontends 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@amadeus-it-group/microfrontends 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@amadeus-it-group/microfrontends 可以商用吗?

@amadeus-it-group/microfrontends 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@amadeus-it-group/microfrontends 的下载量有多少?

根据 npm 官方下载统计,@amadeus-it-group/microfrontends 在 2026/10/06 – 2026/11/04 这 30 天内被下载 1,883 次,日均约 63 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@amadeus-it-group/microfrontends 还在维护吗?

最新版本 0.0.11 发布于 2026/06/08(4 个月前);至今共发布 11 个版本,其中过去 12 个月发布了 2 个;首次发布于 2025/02/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。