#mizchi/js_web/message

    MessageChannel API for two-way communication between contexts.

    #Installation

    Add to your moon.mod:

    import { "mizchi/js_web@0.13.0", }

    ...and to the moon.pkg of the package that uses it:

    import { "mizchi/js_web/message", }

    #Overview

    Provides bindings for MessageChannel and MessagePort for creating communication channels.

    #Usage Example

    fn main {
    // Create a message channel
    let channel = @message.MessageChannel()

    // Get the two ports
    let port1 = channel.port1()
    let port2 = channel.port2()

    // Set up message handler on port1
    port1.set_onmessage(fn(event) {
    @console.log("Port1 received:", event.data())
    })

    // Send message from port2 to port1
    port2.post_message("Hello from port2!")

    // Start receiving messages
    port1.start()
    }

    #Use Cases

    • Communication between main thread and workers
    • Iframe communication
    • Creating isolated communication channels

    #Reference

    MessageChannel

    #external
    pub type MessageChannel

    MessageChannel creates a new message channel and returns it with two MessagePort objects https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel

    MessageChannel::MessageChannel

    fn MessageChannel::MessageChannel() -> MessageChannel

    Creates a new MessageChannel object https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel/MessageChannel

    MessageChannel::as_any

    MessageChannel::new

    #deprecated("use `MessageChannel(..)` instead")
    fn MessageChannel::new() -> MessageChannel

    Deprecated alias for the MessageChannel(..) constructor.

    MessageChannel::port1

    fn MessageChannel::port1(channel : MessageChannel) -> MessagePort

    Returns the first port of the channel https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel/port1

    MessageChannel::port2

    fn MessageChannel::port2(channel : MessageChannel) -> MessagePort

    Returns the second port of the channel https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel/port2

    MessageEvent

    pub(all) struct MessageEvent {
    data :
    Any

    origin : String
    lastEventId : String
    ports : Array[MessagePort]
    }

    MessageEvent represents a message received by a target object https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent

    MessageEvent::as_any

    MessageEvent::as_event_target

    MessagePort

    #external
    pub type MessagePort

    MessagePort represents one end of a message channel https://developer.mozilla.org/en-US/docs/Web/API/MessagePort

    MessagePort implements the EventTarget trait for event handling.
    impl Show for MessagePort

    MessagePort::as_any

    MessagePort::as_event_target

    MessagePort::close

    fn MessagePort::close(port : MessagePort) -> Unit

    Disconnects the port, so it is no longer active https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/close

    MessagePort::postMessage

    #alias(post_message)
    fn MessagePort::postMessage(port : MessagePort, message :
    Any
    ) -> Unit

    Sends a message through the port https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/postMessage

    MessagePort::postMessageWithTransfer

    #alias(post_message_with_transfer)
    fn MessagePort::postMessageWithTransfer(port : MessagePort, message :
    Any
    , transfer : Array[
    Any
    ]) -> Unit

    Sends a message with transferable objects through the port

    MessagePort::start

    fn MessagePort::start(port : MessagePort) -> Unit

    Starts the sending of messages queued on the port https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/start

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io