Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Web APIs in Node.js

Node.js CloseEvent and MessageEvent

Reference for CloseEvent and MessageEvent in Node.js: the event objects delivered by WebSocket, BroadcastChannel and MessagePort, and how to build your own.

MessageEvent and CloseEvent are event classes from the web platform. A MessageEvent carries a received message in its data property: WebSocket, BroadcastChannel, MessagePort and Worker all deliver messages this way. A CloseEvent describes why a WebSocket connection ended, with a numeric code, a text reason and a wasClean flag.

Both are globals in current Node.js and extend Event, so they work with EventTarget. You rarely construct them yourself, except in tests or when you build your own event-emitting class on top of EventTarget. CloseEvent became a global in Node 23 and was backported to Node 22.

NODE
const event = new MessageEvent('message', { data: { hello: 'world' } });
console.log(event.type, event.data); // message { hello: 'world' }

Try it

Node.js Building and dispatching events

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Events from real sources

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
new MessageEvent(type[, init])
new CloseEvent(type[, init])
MessageEvent init Default Description
data null The message payload (any structured-cloneable value).
origin '' Origin of the sender (empty for most Node sources).
lastEventId '' Used by server-sent events; empty otherwise.
source null Sender object (a window in browsers; always null in Node).
ports [] Array of MessagePort objects transferred with the message.
CloseEvent init Default Description
wasClean false true if the connection closed through the normal closing handshake.
code 0 Status code from the close frame.
reason '' Short text from the peer.

Both also take the standard Event options (bubbles, cancelable, composed), which have no effect for targets that are not part of a tree.

Read-only properties: MessageEvent has data, origin, lastEventId, source, ports. CloseEvent has code, reason, wasClean. Both have type, target, timeStamp and the other Event members.

Errors: constructing either without a type throws TypeError.

Usage notes

  • event.data is a copy. Messages cross realms and threads by the structured clone algorithm: plain objects, arrays, Map, Set, typed arrays, Date and errors work; functions and class instances lose their methods.
  • The close code values you will meet: 1000 normal, 1001 going away, 1005 no status in the frame, 1006 abnormal (connection lost, wasClean false), 1008 policy violation, 1009 message too big, 1011 server error.
  • Do not confuse with the EventEmitter style. In worker_threads, port.on('message', value) hands you the data directly, while port.onmessage and addEventListener('message') hand you a MessageEvent.
  • Listener errors are reported, not thrown. An exception inside a listener becomes an uncaught exception (an uncaughtException on the process) and does not stop other listeners.
  • Use Event subclasses for custom protocols. Subclassing Event with extra fields (as in the example) is the web-standard way to type your events.
  • Source of truth for failure details: a WebSocket error event has no message; the close event's code is the better clue.