Documentation
    Preparing search index...

    Class MessageChannel

    Hierarchy

    Index

    Constructors

    Properties

    channelId: string
    senderId: string

    Methods

    • Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.

      The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.

      When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.

      When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.

      When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.

      If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.

      The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.

      MDN Reference

      Type Parameters

      Parameters

      • type: K
      • listener: null | EventHandler<MessageChannelEvents[K]>
      • Optionaloptions: boolean | AddEventListenerOptions

      Returns void

    • Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.

      MDN Reference

      Parameters

      • event: Event

      Returns boolean

    • Check if there are pending repair requests that need to be sent. Useful for adaptive sync intervals - increase frequency when repairs pending.

      Parameters

      • currentTime: number = ...

      Returns boolean

    • Parameters

      • type: string

      Returns number

    • Processes all queued tasks sequentially to ensure proper message ordering.

      This method should be called periodically by the library consumer to execute queued send/receive operations in the correct sequence.

      Returns Promise<void>

      const channel = new MessageChannel("my-channel");

      // Queue some operations
      await channel.pushOutgoingMessage(payload, callback);
      channel.pushIncomingMessage(incomingMessage);

      // Process all queued operations
      await channel.processTasks();

      CustomEvent("taskError", { detail: { command, error, params } } if any task fails, but continues processing remaining tasks

    • Queues a received message for processing.

      The message will be processed when processTasks() is called, ensuring proper dependency resolution and causal ordering.

      Parameters

      • message: Message

        The message to receive and process

      • retrievalHint: undefined | Uint8Array<ArrayBufferLike>

        The retrieval hint for the message, provided by the transport layer

      Returns void

      const channel = new MessageChannel("chat-room");

      // Receive a message from the network
      channel.pushIncomingMessage(incomingMessage);

      // Process the received message
      await channel.processTasks();
    • Sends a short-lived message without synchronization or reliability requirements.

      Sends a message without a timestamp, causal history, or bloom filter. Ephemeral messages are not added to the outgoing buffer. Upon reception, ephemeral messages are delivered immediately without checking for causal dependencies or including in the local log.

      See https://rfc.vac.dev/vac/raw/sds/#ephemeral-messages

      Parameters

      • payload: Uint8Array

        The payload to send.

      • Optionalcallback: (processedMessage: Message) => Promise<boolean>

        A callback function that returns a boolean indicating whether the message was sent successfully.

      Returns Promise<void>

    • Queues a message to be sent on this channel.

      The message will be processed sequentially when processTasks() is called. This ensures proper lamport timestamp ordering and causal history tracking.

      Parameters

      • payload: Uint8Array

        The message content as a byte array

      • Optionalcallback: (
            processedMessage: ContentMessage,
        ) => Promise<
            { retrievalHint?: Uint8Array<ArrayBufferLike>; success: boolean },
        >

        callback function that should propagate the message on the routing layer; success should be false if sending irremediably fails, when set to true, the message is finalized into the channel locally.

      Returns void

      Promise that resolves when the message is queued (not sent)

      const channel = new MessageChannel("chat-room");
      const message = new TextEncoder().encode("Hello, world!");

      await channel.pushOutgoingMessage(message, async (processedMessage) => {
      console.log("Message processed:", processedMessage.messageId);
      return { success: true };
      });

      // Actually send the message
      await channel.processTasks();

      Error if the payload is empty

    • Removes the event listener in target's event listener list with the same type, callback, and options.

      MDN Reference

      Type Parameters

      Parameters

      • type: K
      • Optionallistener: null | EventHandler<MessageChannelEvents[K]>
      • Optionaloptions: boolean | EventListenerOptions

      Returns void

    • Type Parameters

      • Detail

      Parameters

      Returns boolean

    • Sweep repair incoming buffer and rebroadcast messages ready for repair. Per SDS-R spec: periodically check for repair responses that are due.

      Parameters

      • Optionalcallback: (message: Message) => Promise<boolean>

        callback to rebroadcast the message

      Returns Promise<Message[]>

      Promise that resolves when all ready repairs have been sent