@qualithm/mqtt-wire - v0.8.0
    Preparing search index...

    Class MqttWire

    MQTT protocol connection state machine (server-side).

    Handles incoming MQTT client connections, protocol parsing, encoding, QoS flows, keepalive, and connection lifecycle. Transport-agnostic: bytes in via receive(), bytes out via onSend hook.

    const wire = new MqttWire({
    onSend: (data) => socket.write(data),
    onConnect: (connect) => ({
    type: PacketType.CONNACK,
    sessionPresent: false,
    reasonCode: 0x00
    }),
    onPublish: (packet) => console.log(packet.topic, packet.payload),
    onSubscribe: (packet) => ({
    type: PacketType.SUBACK,
    packetId: packet.packetId,
    reasonCodes: packet.subscriptions.map(s => s.options.qos)
    }),
    onDisconnect: () => socket.end(),
    onError: (err) => console.error(err)
    })

    // Receive data from transport. A hook error ends the session through
    // onDisconnect; a rejection here is a bug in the wire.
    socket.on('data', (chunk) => {
    wire.receive(chunk).catch(() => socket.destroy())
    })

    // Publish to client
    await wire.publish('topic', payload, { qos: 1 })

    // Disconnect client
    await wire.disconnect()
    Index

    Constructors

    Accessors

    • get clientId(): string | null

      Get client ID (from CONNECT or assigned by server).

      Returns string | null

    • get isConnected(): boolean

      Check if connected.

      Returns boolean

    • get maximumPacketSize(): number

      Get client's maximum packet size (from CONNECT).

      Returns number

    • get receiveMaximum(): number

      Get client's receive maximum (from CONNECT).

      Returns number

    Methods

    • Disconnect the client.

      Sends DISCONNECT packet (MQTT 5.0), waiting at most closeTimeoutMs for onSend, and cleans up state. While an error is already closing the session, it returns at once and leaves the close to finish, so onDisconnect may run after it resolves.

      Parameters

      Returns Promise<void>

    • Publish a message to the client.

      Parameters

      • topic: string

        Topic name

      • payload: Uint8Array

        Message payload

      • options: { dup?: boolean; properties?: PublishProperties; qos?: QoS; retain?: boolean } = {}

        Publish options

      Returns Promise<number | undefined>

      Packet ID for QoS > 0, undefined for QoS 0

    • Process incoming data from transport.

      Call this method with chunks of data received from the transport (TCP socket, WebSocket, etc.). Complete packets will be parsed and appropriate hooks called.

      Resolves even when a hook throws: the error goes to onError, the session ends (DISCONNECT 0x80 on 5.0) and onDisconnect runs with it. Frames after the failing one in the same chunk are dropped.

      Parameters

      • data: Uint8Array

        Incoming data chunk

      Returns Promise<void>

    • Force cleanup of all state, ready for a new connection. Ends a live session first, so onDisconnect runs once for it; a session that has already ended doesn't fire it again.

      Returns void