edgeport - v1.0.3
    Preparing search index...

    Function connect

    • Connects to an MQTT broker over TCP, performs the handshake, and returns a live session.

      Dials the core transport (implicit TLS when tls: 'implicit', a STARTTLS-style upgrade when tls: 'starttls', otherwise plaintext), sends CONNECT, and waits for CONNACK so an auth failure surfaces here. When transport: 'ws' is set, this delegates to connectWebSocket using opts.url. A background pump then routes inbound messages to subscriptions.

      Parameters

      Returns Promise<MqttSession>

      The live session.

      If the broker rejects the credentials.

      If the connection cannot be established.

      If the broker speaks the protocol incorrectly.

      1.0.0

      import { connect } from 'edgeport/mqtt';

      await using mqtt = await connect({ hostname: 'broker.example.com', username: 'u', password: 'p' });
      await using sub = mqtt.subscribe('sensors/+/temp', { qos: 1 });
      await mqtt.publish('sensors/1/temp', '21.5', { qos: 1 });
      for await (const msg of sub) {
      console.log(msg.topic, new TextDecoder().decode(msg.payload));
      break;
      }