jsrosetta

Object-oriented

Event Emitter

Node.js's EventEmitter compared to NotificationCenter (Swift), a hand-rolled callback map (Rust, Java), and channels with select in Go.

Minimum versions
Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.58Swift ≥ 3.0Java ≥ 25
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

Node.js has a built-in EventEmitter: register listeners with .on(), fire events with .emit(). Swift has NotificationCenter in Foundation — the same idea of event names plus observers, and the closest built-in match to EventEmitter among all five languages. Rust and Java have nothing built in, but hand-rolling a map from event name to a list of callbacks is a few lines of code. Go has no EventEmitter — the closest thing is a channel combined with a goroutine and select to listen on several "event channels" at once.

EventEmitter (Node.js) vs channel + select (Go)

import { EventEmitter } from 'node:events'
 
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter()
 
myEmitter.on('my-event', msg => {
  console.log(msg)
})
 
myEmitter.on('my-other-event', msg => {
  console.log(msg)
})
 
myEmitter.emit('my-event', 'hello world')
myEmitter.emit('my-other-event', 'hello other world')
// hello world
// hello other world

Reference: github.com/miguelmota/golang-for-nodejs-developers#event-emitter