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.
const event = new MessageEvent('message', { data: { hello: 'world' } }); console.log(event.type, event.data); // message { hello: 'world' }
Try it
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.
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
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.datais a copy. Messages cross realms and threads by the structured clone algorithm: plain objects, arrays,Map,Set, typed arrays,Dateand errors work; functions and class instances lose their methods.- The close
codevalues you will meet:1000normal,1001going away,1005no status in the frame,1006abnormal (connection lost,wasCleanfalse),1008policy violation,1009message too big,1011server error. - Do not confuse with the
EventEmitterstyle. Inworker_threads,port.on('message', value)hands you the data directly, whileport.onmessageandaddEventListener('message')hand you aMessageEvent. - Listener errors are reported, not thrown. An exception inside a listener becomes an uncaught exception (an
uncaughtExceptionon the process) and does not stop other listeners. - Use
Eventsubclasses for custom protocols. SubclassingEventwith extra fields (as in the example) is the web-standard way to type your events. - Source of truth for failure details: a WebSocket
errorevent has no message; thecloseevent'scodeis the better clue.