class MessagePort extends Object with StObject with _TransferListItem
Instances of the worker.MessagePort
class represent one end of an
asynchronous, two-way communications channel. It can be used to transfer
structured data, memory regions and other MessagePort
s between different Worker
s.
This implementation matches [browser MessagePort
](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort) s.
- Annotations
- @JSType() @JSImport("worker_threads", "MessagePort") @native()
- Since
v10.5.0
- Alphabetic
- By Inheritance
- MessagePort
- _TransferListItem
- StObject
- Object
- Any
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Instance Constructors
- new MessagePort()
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def addListener(event: Symbol, listener: Function1[Any, Unit]): MessagePort.this.type
- def addListener(event: String, listener: Function1[Any, Unit]): MessagePort.this.type
- def addListener_close(event: close, listener: Function0[Unit]): MessagePort.this.type
- Annotations
- @JSName("addListener")
- def addListener_message(event: message, listener: Function1[Any, Unit]): MessagePort.this.type
- Annotations
- @JSName("addListener")
- def addListener_messageerror(event: messageerror, listener: Function1[Error, Unit]): MessagePort.this.type
- Annotations
- @JSName("addListener")
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
- def close(): Unit
Disables further sending of messages on either side of the connection.
Disables further sending of messages on either side of the connection. This method can be called when no further communication will happen over this
MessagePort
.The
'close' event
is emitted on bothMessagePort
instances that are part of the channel.- Since
v10.5.0
- def emit(event: Symbol, args: Any*): Boolean
- def emit(event: String, args: Any*): Boolean
- def emit_close(event: close): Boolean
- Annotations
- @JSName("emit")
- def emit_message(event: message, value: Any): Boolean
- Annotations
- @JSName("emit")
- def emit_messageerror(event: messageerror, error: Error): Boolean
- Annotations
- @JSName("emit")
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def hasOwnProperty(v: String): Boolean
- Definition Classes
- Object
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def isPrototypeOf(v: Object): Boolean
- Definition Classes
- Object
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def off(event: Symbol, listener: Function1[Any, Unit]): MessagePort.this.type
- def off(event: String, listener: Function1[Any, Unit]): MessagePort.this.type
- def off_close(event: close, listener: Function0[Unit]): MessagePort.this.type
- Annotations
- @JSName("off")
- def off_message(event: message, listener: Function1[Any, Unit]): MessagePort.this.type
- Annotations
- @JSName("off")
- def off_messageerror(event: messageerror, listener: Function1[Error, Unit]): MessagePort.this.type
- Annotations
- @JSName("off")
- def on(event: Symbol, listener: Function1[Any, Unit]): MessagePort.this.type
- def on(event: String, listener: Function1[Any, Unit]): MessagePort.this.type
- def on_close(event: close, listener: Function0[Unit]): MessagePort.this.type
- Annotations
- @JSName("on")
- def on_message(event: message, listener: Function1[Any, Unit]): MessagePort.this.type
- Annotations
- @JSName("on")
- def on_messageerror(event: messageerror, listener: Function1[Error, Unit]): MessagePort.this.type
- Annotations
- @JSName("on")
- def once(event: Symbol, listener: Function1[Any, Unit]): MessagePort.this.type
- def once(event: String, listener: Function1[Any, Unit]): MessagePort.this.type
- def once_close(event: close, listener: Function0[Unit]): MessagePort.this.type
- Annotations
- @JSName("once")
- def once_message(event: message, listener: Function1[Any, Unit]): MessagePort.this.type
- Annotations
- @JSName("once")
- def once_messageerror(event: messageerror, listener: Function1[Error, Unit]): MessagePort.this.type
- Annotations
- @JSName("once")
- def postMessage(value: Any, transferList: Array[TransferListItem]): Unit
- def postMessage(value: Any): Unit
Sends a JavaScript value to the receiving side of this channel.
value
is transferred in a way which is compatible with the [HTML structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm).Sends a JavaScript value to the receiving side of this channel.
value
is transferred in a way which is compatible with the [HTML structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm).In particular, the significant differences to
JSON
are:*
value
may contain circular references. *value
may contain instances of builtin JS types such asRegExp
s,BigInt
s,Map
s,Set
s, etc. *value
may contain typed arrays, both usingArrayBuffer
s andSharedArrayBuffer
s. *value
may contain [WebAssembly.Module
](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module) instances. *value
may not contain native (C++-backed) objects other than:js const { MessageChannel } = require('worker_threads'); const { port1, port2 } = new MessageChannel();
port1.on('message', (message) => console.log(message));
const circularData = {}; circularData.foo = circularData; // Prints: { foo: [Circular] } port2.postMessage(circularData);
transferList
may be a list of [ArrayBuffer
](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer),MessagePort
andFileHandle
objects. After transferring, they are not usable on the sending side of the channel anymore (even if they are not contained invalue
). Unlike withchild processes
, transferring handles such as network sockets is currently not supported.If
value
contains [SharedArrayBuffer
](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer) instances, those are accessible from either thread. They cannot be listed intransferList
.value
may still containArrayBuffer
instances that are not intransferList
; in that case, the underlying memory is copied rather than moved.js const { MessageChannel } = require('worker_threads'); const { port1, port2 } = new MessageChannel();
port1.on('message', (message) => console.log(message));
const uint8Array = new Uint8Array([ 1, 2, 3, 4 ]); // This posts a copy of
uint8Array
: port2.postMessage(uint8Array); // This does not copy data, but rendersuint8Array
unusable: port2.postMessage(uint8Array, [ uint8Array.buffer ]);// The memory for the
sharedUint8Array
is accessible from both the // original and the copy received by.on('message')
: const sharedUint8Array = new Uint8Array(new SharedArrayBuffer(4)); port2.postMessage(sharedUint8Array);// This transfers a freshly created message port to the receiver. // This can be used, for example, to create communication channels between // multiple
Worker
threads that are children of the same parent thread. const otherChannel = new MessageChannel(); port2.postMessage({ port: otherChannel.port1 }, [ otherChannel.port1 ]);The message object is cloned immediately, and can be modified after posting without having side effects.
For more information on the serialization and deserialization mechanisms behind this API, see the
serialization API of the v8 module
.- Since
v10.5.0
- def prependListener(event: Symbol, listener: Function1[Any, Unit]): MessagePort.this.type
- def prependListener(event: String, listener: Function1[Any, Unit]): MessagePort.this.type
- def prependListener_close(event: close, listener: Function0[Unit]): MessagePort.this.type
- Annotations
- @JSName("prependListener")
- def prependListener_message(event: message, listener: Function1[Any, Unit]): MessagePort.this.type
- Annotations
- @JSName("prependListener")
- def prependListener_messageerror(event: messageerror, listener: Function1[Error, Unit]): MessagePort.this.type
- Annotations
- @JSName("prependListener")
- def prependOnceListener(event: Symbol, listener: Function1[Any, Unit]): MessagePort.this.type
- def prependOnceListener(event: String, listener: Function1[Any, Unit]): MessagePort.this.type
- def prependOnceListener_close(event: close, listener: Function0[Unit]): MessagePort.this.type
- Annotations
- @JSName("prependOnceListener")
- def prependOnceListener_message(event: message, listener: Function1[Any, Unit]): MessagePort.this.type
- Annotations
- @JSName("prependOnceListener")
- def prependOnceListener_messageerror(event: messageerror, listener: Function1[Error, Unit]): MessagePort.this.type
- Annotations
- @JSName("prependOnceListener")
- def propertyIsEnumerable(v: String): Boolean
- Definition Classes
- Object
- def ref(): Unit
Opposite of
unref()
.Opposite of
unref()
. Callingref()
on a previouslyunref()
ed port does_not_ let the program exit if it's the only active handle left (the default behavior). If the port isref()
ed, callingref()
again has no effect.If listeners are attached or removed using
.on('message')
, the port isref()
ed andunref()
ed automatically depending on whether listeners for the event exist.- Since
v10.5.0
- def removeListener(event: Symbol, listener: Function1[Any, Unit]): MessagePort.this.type
- def removeListener(event: String, listener: Function1[Any, Unit]): MessagePort.this.type
- def removeListener_close(event: close, listener: Function0[Unit]): MessagePort.this.type
- Annotations
- @JSName("removeListener")
- def removeListener_message(event: message, listener: Function1[Any, Unit]): MessagePort.this.type
- Annotations
- @JSName("removeListener")
- def removeListener_messageerror(event: messageerror, listener: Function1[Error, Unit]): MessagePort.this.type
- Annotations
- @JSName("removeListener")
- def start(): Unit
Starts receiving messages on this
MessagePort
.Starts receiving messages on this
MessagePort
. When using this port as an event emitter, this is called automatically once'message'
listeners are attached.This method exists for parity with the Web
MessagePort
API. In Node.js, it is only useful for ignoring messages when no event listener is present. Node.js also diverges in its handling of.onmessage
. Setting it automatically calls.start()
, but unsetting it lets messages queue up until a new handler is set or the port is discarded.- Since
v10.5.0
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toLocaleString(): String
- Definition Classes
- Object
- def toString(): String
- Definition Classes
- AnyRef → Any
- def unref(): Unit
Calling
unref()
on a port allows the thread to exit if this is the only active handle in the event system.Calling
unref()
on a port allows the thread to exit if this is the only active handle in the event system. If the port is alreadyunref()
ed callingunref()
again has no effect.If listeners are attached or removed using
.on('message')
, the port isref()
ed andunref()
ed automatically depending on whether listeners for the event exist.- Since
v10.5.0
- def valueOf(): Any
- Definition Classes
- Object
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
Deprecated Value Members
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated
- Deprecated