BeginnerCoder

Lesson 13 — Event emitter

Publish and subscribe

Node is event-driven: things happen ("a request arrived", "a file finished") and code reacts. An event emitter keeps a list of listener functions per event name. on(name, fn) adds a listener; emit(name, data) calls all of them.

b.on("hello", (d) => console.log("got", d));
b.emit("hello", "world");   // got world

Note: this playground runs JavaScript in your browser, not in real Node, so Node-only tools (like fs or http) are not available. The exercise is a small plain-JS simulation of the Node idea. Real Node has this built in as EventEmitter.

Why it matters: servers, streams and sockets in Node are all event emitters.

Want it another way?
Task

Finish emit() so it calls every listener for the event. Both listeners should log.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.