บัสเหตุการณ์สำหรับการสื่อสารข้ามแอป
เรียนรู้การสร้างบัสเหตุการณ์สำหรับการสื่อสารระหว่างไมโครฟรอนต์เอนด์ต่าง ๆ โดยลดการพึ่งพาซึ่งกันและกัน
บัสเหตุการณ์สำหรับการสื่อสารข้ามแอป เป็นบทเรียน Micro Frontends Architecture with Module Federation ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Micro Frontends Architecture with Module Federation และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Micro Frontends Architecture with Module Federation มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
MFE Communication Challenges
In a Micro Frontend (MFE) architecture, different applications run independently. But what happens when one MFE needs to tell another MFE something?
Direct communication can create tight dependencies, making your system harder to maintain and scale. We need a way for MFEs to talk without knowing too much about each other.
Meet the Event Bus
An Event Bus is a design pattern that provides a central hub for communication between different parts of an application, or in our case, different Micro Frontends.
Think of it like a community bulletin board where anyone can post a message, and anyone interested can read it.
How it Works: Publish/Subscribe
The Event Bus operates on a Publish/Subscribe (or Pub/Sub) model:
- Publishers (MFEs)
emitevents to the bus, often with some data. They don't care who listens. - Subscribers (other MFEs)
listen(oron) for specific events. They react when an event they're interested in is published.
This keeps communication decoupled.
Designing Our Event Bus
A simple Event Bus needs a few core methods:
on(eventName, callback): To subscribe to an event.emit(eventName, data): To publish an event with optional data.off(eventName, callback): To unsubscribe from an event (important for cleanup).
Let's build a basic JavaScript version.
Building the Bus Core
Here's a basic JavaScript class for an EventBus. It uses an internal object, listeners, to store all registered callbacks for each event name.
Try running this example:
class EventBus {
constructor() {
this.listeners = {};
}
on(eventName, callback) {
if (!this.listeners[eventName]) {
this.listeners[eventName] = [];
}
this.listeners[eventName].push(callback);
}
emit(eventName, data) {
if (this.listeners[eventName]) {
this.listeners[eventName].forEach(callback => {
callback(data);
});
}
}
off(eventName, callback) {
if (!this.listeners[eventName]) return;
this.listeners[eventName] = this.listeners[eventName].filter(
listener => listener !== callback
);
}
}
const bus = new EventBus();
console.log("EventBus initialized!");Sending Messages (Publish)
To send a message, one Micro Frontend will call the emit method on the shared Event Bus instance. It provides the event name and any relevant data.
The Event Bus then broadcasts this event to all registered listeners.
Publishing in Action
Here, we simulate an MFE publishing a 'user:loggedIn' event. Notice how the mfeAPublisher function uses bus.emit().
Run the code and see the output:
class EventBus {
constructor() {
this.listeners = {};
}
on(eventName, callback) {
if (!this.listeners[eventName]) this.listeners[eventName] = [];
this.listeners[eventName].push(callback);
}
emit(eventName, data) {
if (this.listeners[eventName]) {
this.listeners[eventName].forEach(callback => callback(data));
}
}
off(eventName, callback) {
if (!this.listeners[eventName]) return;
this.listeners[eventName] = this.listeners[eventName].filter(
listener => listener !== callback
);
}
}
const bus = new EventBus();
// MFE A publishes an event
function mfeAPublisher() {
const data = { user: "Alice", action: "loggedIn" };
bus.emit("user:loggedIn", data);
console.log("MFE A published 'user:loggedIn'");
}
mfeAPublisher();Receiving Messages (Subscribe)
Another Micro Frontend that wants to react to this event will use the on method to subscribe. It provides the event name it's interested in and a callback function to execute when that event occurs.
The callback function receives the data published with the event.
Subscribing and Reacting
Now let's put it all together. One MFE subscribes to the user:loggedIn event, and another MFE publishes it. When you run this, you'll see both actions logged.
Observe the flow of communication:
class EventBus {
constructor() {
this.listeners = {};
}
on(eventName, callback) {
if (!this.listeners[eventName]) this.listeners[eventName] = [];
this.listeners[eventName].push(callback);
}
emit(eventName, data) {
if (this.listeners[eventName]) {
this.listeners[eventName].forEach(callback => callback(data));
}
}
off(eventName, callback) {
if (!this.listeners[eventName]) return;
this.listeners[eventName] = this.listeners[eventName].filter(
listener => listener !== callback
);
}
}
const bus = new EventBus();
// MFE B subscribes to an event
function mfeBSubscriber(data) {
console.log("MFE B received 'user:loggedIn' event:");
console.log(data);
}
bus.on("user:loggedIn", mfeBSubscriber);
console.log("MFE B subscribed to 'user:loggedIn'");
// MFE A publishes an event
function mfeAPublisher() {
const data = { user: "Alice", action: "loggedIn" };
bus.emit("user:loggedIn", data);
console.log("MFE A published 'user:loggedIn'");
}
mfeAPublisher();When to Use an Event Bus
Pros:
- Decoupling: MFEs don't need to know about each other.
- Simplicity: Easy to implement for basic broadcast needs.
- Flexibility: New subscribers can be added without changing publishers.
Cons:
- Debugging: Hard to trace event flow (event spaghetti).
- No direct response: Not suitable for request/response patterns.
- Global state: The bus itself can become a single point of failure or a source of implicit dependencies.
Event Bus Quick Check
An Event Bus is a powerful tool for communication in Micro Frontends. Which of the following statements accurately describe its benefits?
Recap: Event Bus Essentials
You've learned about the Event Bus pattern for inter-Micro Frontend communication.
- It uses a Publish/Subscribe model.
- Publishers
emitevents, subscriberson(listen for) them. - It promotes decoupled communication, reducing direct dependencies.
- While simple and flexible, be aware of potential debugging challenges and ensure proper cleanup (
off).
Next, we'll explore shared state management!
คำถามที่พบบ่อย
บทเรียน “บัสเหตุการณ์สำหรับการสื่อสารข้ามแอป” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “บัสเหตุการณ์สำหรับการสื่อสารข้ามแอป” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Micro Frontends Architecture with Module Federation ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Micro Frontends Architecture with Module Federation มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “บัสเหตุการณ์สำหรับการสื่อสารข้ามแอป”
เรียนรู้การสร้างบัสเหตุการณ์สำหรับการสื่อสารระหว่างไมโครฟรอนต์เอนด์ต่าง ๆ โดยลดการพึ่งพาซึ่งกันและกัน คุณปฏิบัติ Micro Frontends Architecture with Module Federation ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Micro Frontends Architecture with Module Federation หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Micro Frontends Architecture with Module Federation บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “บัสเหตุการณ์สำหรับการสื่อสารข้ามแอป” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Micro Frontends Architecture with Module Federation นี้ได้ไหม
ได้ บทเรียน Micro Frontends Architecture with Module Federation ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บัสเหตุการณ์สำหรับการสื่อสารข้ามแอป
- การจัดการสถานะร่วม
- โซลูชันการสื่อสารแบบกำหนดเอง
- การสื่อสารด้วยเหตุการณ์ DOM แบบกำหนดเอง