Custom DOM 이벤트로 통신하기
브라우저의 네이티브 CustomEvent API를 가볍고 프레임워크에 종속되지 않는 마이크로 프런트엔드 간 통신 채널로 사용하는 방법을 배웁니다.
Custom DOM 이벤트로 통신하기은(는) CoddyKit의 무료 Micro Frontends Architecture with Module Federation 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Micro Frontends Architecture with Module Federation 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Micro Frontends Architecture with Module Federation 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Why Native Events?
The browser already has a built-in pub/sub system: the DOM event model. Using it for MFE communication needs no library and works across any framework.
The CustomEvent API
CustomEvent lets you create your own named events carrying a detail payload, dispatched on any DOM element including window.
const evt = new CustomEvent("cart:add", {
detail: { productId: 42, qty: 1 }
});Dispatching an Event
Any micro frontend can broadcast an event on a shared target such as window. Others do not need a direct reference to the sender.
window.dispatchEvent(new CustomEvent("cart:add", {
detail: { productId: 42 }
}));Listening for Events
A consuming MFE subscribes with addEventListener and reads the payload from event.detail.
window.addEventListener("cart:add", (e) => {
updateCart(e.detail.productId);
});Naming Conventions
Namespace event names to avoid collisions, for example cart:add or auth:login. A shared naming convention is part of your communication contract.
Cleaning Up Listeners
When a micro frontend unmounts, remove its listeners to prevent memory leaks and duplicate handling.
const handler = (e) => updateCart(e.detail);
window.addEventListener("cart:add", handler);
// on unmount:
window.removeEventListener("cart:add", handler);Two-Way Communication
For request/response style flows, one MFE dispatches a request event and another replies with a separate response event, keeping each direction explicit.
window.dispatchEvent(new CustomEvent("user:request"));
window.addEventListener("user:response", e => show(e.detail));Strengths of DOM Events
Native events are appealing because they are:
- Framework-agnostic
- Zero-dependency
- Familiar to all web developers
- Naturally decoupled (no shared references)
Limitations to Know
They also have downsides:
- No type safety on
detail - Easy to misspell event names
- Hard to trace who listens to what
- Not ideal for large or frequent payloads
Wrapping Events in a Typed Helper
To reduce mistakes, wrap dispatch and listen in a small typed module shared across MFEs, centralizing event names and payload shapes.
export function emit(name, detail) {
window.dispatchEvent(new CustomEvent(name, { detail }));
}When to Choose This Pattern
Custom DOM events shine for simple, occasional notifications between loosely coupled MFEs. For complex shared data, prefer a dedicated event bus or shared store.
Quick Check
Test your custom-event knowledge.
Recap
You learned native event communication:
CustomEventcarries data indetail- Dispatch on
window; listen withaddEventListener - Namespace names and clean up listeners
- Great for simple, decoupled notifications
- Wrap in a typed helper to avoid mistakes
DOM events are a zero-dependency MFE channel.
자주 묻는 질문
“Custom DOM 이벤트로 통신하기” 강의는 무료인가요?
네 — “Custom DOM 이벤트로 통신하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Micro Frontends Architecture with Module Federation 강의 전체를 잠금 해제할 수 있습니다. Micro Frontends Architecture with Module Federation 강의에는 총 4개의 강의가 포함되어 있습니다.
“Custom DOM 이벤트로 통신하기”에서 뭘 배우나요?
브라우저의 네이티브 CustomEvent API를 가볍고 프레임워크에 종속되지 않는 마이크로 프런트엔드 간 통신 채널로 사용하는 방법을 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Micro Frontends Architecture with Module Federation을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Micro Frontends Architecture with Module Federation을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Micro Frontends Architecture with Module Federation은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.
“Custom DOM 이벤트로 통신하기” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Micro Frontends Architecture with Module Federation 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Micro Frontends Architecture with Module Federation 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 앱 간 통신을 위한 이벤트 버스
- 공유 상태 관리
- 맞춤형 통신 솔루션
- Custom DOM 이벤트로 통신하기