Packages

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 MessagePorts 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

Linear Supertypes
_TransferListItem, StObject, Object, Any, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. MessagePort
  2. _TransferListItem
  3. StObject
  4. Object
  5. Any
  6. AnyRef
  7. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Instance Constructors

  1. new MessagePort()

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. def addListener(event: Symbol, listener: Function1[Any, Unit]): MessagePort.this.type
  5. def addListener(event: String, listener: Function1[Any, Unit]): MessagePort.this.type
  6. def addListener_close(event: close, listener: Function0[Unit]): MessagePort.this.type
    Annotations
    @JSName("addListener")
  7. def addListener_message(event: message, listener: Function1[Any, Unit]): MessagePort.this.type
    Annotations
    @JSName("addListener")
  8. def addListener_messageerror(event: messageerror, listener: Function1[Error, Unit]): MessagePort.this.type
    Annotations
    @JSName("addListener")
  9. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  10. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
  11. 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 thisMessagePort.

    The 'close' event is emitted on both MessagePort instances that are part of the channel.

    Since

    v10.5.0

  12. def emit(event: Symbol, args: Any*): Boolean
  13. def emit(event: String, args: Any*): Boolean
  14. def emit_close(event: close): Boolean
    Annotations
    @JSName("emit")
  15. def emit_message(event: message, value: Any): Boolean
    Annotations
    @JSName("emit")
  16. def emit_messageerror(event: messageerror, error: Error): Boolean
    Annotations
    @JSName("emit")
  17. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  18. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  19. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  20. def hasOwnProperty(v: String): Boolean
    Definition Classes
    Object
  21. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  22. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  23. def isPrototypeOf(v: Object): Boolean
    Definition Classes
    Object
  24. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  25. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  26. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  27. def off(event: Symbol, listener: Function1[Any, Unit]): MessagePort.this.type
  28. def off(event: String, listener: Function1[Any, Unit]): MessagePort.this.type
  29. def off_close(event: close, listener: Function0[Unit]): MessagePort.this.type
    Annotations
    @JSName("off")
  30. def off_message(event: message, listener: Function1[Any, Unit]): MessagePort.this.type
    Annotations
    @JSName("off")
  31. def off_messageerror(event: messageerror, listener: Function1[Error, Unit]): MessagePort.this.type
    Annotations
    @JSName("off")
  32. def on(event: Symbol, listener: Function1[Any, Unit]): MessagePort.this.type
  33. def on(event: String, listener: Function1[Any, Unit]): MessagePort.this.type
  34. def on_close(event: close, listener: Function0[Unit]): MessagePort.this.type
    Annotations
    @JSName("on")
  35. def on_message(event: message, listener: Function1[Any, Unit]): MessagePort.this.type
    Annotations
    @JSName("on")
  36. def on_messageerror(event: messageerror, listener: Function1[Error, Unit]): MessagePort.this.type
    Annotations
    @JSName("on")
  37. def once(event: Symbol, listener: Function1[Any, Unit]): MessagePort.this.type
  38. def once(event: String, listener: Function1[Any, Unit]): MessagePort.this.type
  39. def once_close(event: close, listener: Function0[Unit]): MessagePort.this.type
    Annotations
    @JSName("once")
  40. def once_message(event: message, listener: Function1[Any, Unit]): MessagePort.this.type
    Annotations
    @JSName("once")
  41. def once_messageerror(event: messageerror, listener: Function1[Error, Unit]): MessagePort.this.type
    Annotations
    @JSName("once")
  42. def postMessage(value: Any, transferList: Array[TransferListItem]): Unit
  43. 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 as RegExps,BigInts, Maps, Sets, etc. * value may contain typed arrays, both using ArrayBuffers and SharedArrayBuffers. * 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 and FileHandle objects. After transferring, they are not usable on the sending side of the channel anymore (even if they are not contained in value). Unlike with child 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 in transferList.

    value may still contain ArrayBuffer 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 renders uint8Array 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

  44. def prependListener(event: Symbol, listener: Function1[Any, Unit]): MessagePort.this.type
  45. def prependListener(event: String, listener: Function1[Any, Unit]): MessagePort.this.type
  46. def prependListener_close(event: close, listener: Function0[Unit]): MessagePort.this.type
    Annotations
    @JSName("prependListener")
  47. def prependListener_message(event: message, listener: Function1[Any, Unit]): MessagePort.this.type
    Annotations
    @JSName("prependListener")
  48. def prependListener_messageerror(event: messageerror, listener: Function1[Error, Unit]): MessagePort.this.type
    Annotations
    @JSName("prependListener")
  49. def prependOnceListener(event: Symbol, listener: Function1[Any, Unit]): MessagePort.this.type
  50. def prependOnceListener(event: String, listener: Function1[Any, Unit]): MessagePort.this.type
  51. def prependOnceListener_close(event: close, listener: Function0[Unit]): MessagePort.this.type
    Annotations
    @JSName("prependOnceListener")
  52. def prependOnceListener_message(event: message, listener: Function1[Any, Unit]): MessagePort.this.type
    Annotations
    @JSName("prependOnceListener")
  53. def prependOnceListener_messageerror(event: messageerror, listener: Function1[Error, Unit]): MessagePort.this.type
    Annotations
    @JSName("prependOnceListener")
  54. def propertyIsEnumerable(v: String): Boolean
    Definition Classes
    Object
  55. def ref(): Unit

    Opposite of unref().

    Opposite of unref(). Calling ref() on a previously unref()ed port does_not_ let the program exit if it's the only active handle left (the default behavior). If the port is ref()ed, calling ref() again has no effect.

    If listeners are attached or removed using .on('message'), the port is ref()ed and unref()ed automatically depending on whether listeners for the event exist.

    Since

    v10.5.0

  56. def removeListener(event: Symbol, listener: Function1[Any, Unit]): MessagePort.this.type
  57. def removeListener(event: String, listener: Function1[Any, Unit]): MessagePort.this.type
  58. def removeListener_close(event: close, listener: Function0[Unit]): MessagePort.this.type
    Annotations
    @JSName("removeListener")
  59. def removeListener_message(event: message, listener: Function1[Any, Unit]): MessagePort.this.type
    Annotations
    @JSName("removeListener")
  60. def removeListener_messageerror(event: messageerror, listener: Function1[Error, Unit]): MessagePort.this.type
    Annotations
    @JSName("removeListener")
  61. 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

  62. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  63. def toLocaleString(): String
    Definition Classes
    Object
  64. def toString(): String
    Definition Classes
    AnyRef → Any
  65. 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 already unref()ed callingunref() again has no effect.

    If listeners are attached or removed using .on('message'), the port isref()ed and unref()ed automatically depending on whether listeners for the event exist.

    Since

    v10.5.0

  66. def valueOf(): Any
    Definition Classes
    Object
  67. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  68. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  69. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])

Deprecated Value Members

  1. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable]) @Deprecated
    Deprecated

Inherited from _TransferListItem

Inherited from StObject

Inherited from Object

Inherited from Any

Inherited from AnyRef

Inherited from Any

Ungrouped