Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

284
Vistas
Can't publish with aws-iot-device-sdk, but can subscribe

My mqtt client is unable to publish to messages to AWS, but is able to receive them. The following code works to subscribe to the mqtt Client, and print messages received to the console.

  if (Object.keys(cred).length !== 0) { //if temp creds exist, create mqtt client
    const mqttClient = AWSIoTData.device({
      region: awsConfig.region,
      host: awsConfig.mqttEndpoint,
      clientId: awsConfig.clientId,
      protocol: "wss",
      maximumReconnectTimeMs: 8000,
      debug: false,
      accessKeyId: String(cred.AccessKeyId),
      secretKey: String(cred.SecretAccessKey),
      sessionToken: String(cred.SessionToken),
    });

    mqttClient.on("connect", () => {
      console.log("mqttClient connected");
      mqttClient.subscribe("mg/autogenstart/fromDevice");
    });
    mqttClient.on("message", (topic, payload) => {
      console.log(JSON.parse(payload.toString()));
    });

However, the following code does not work to publish to the client on connect.

  if (Object.keys(cred).length !== 0) { //if temp creds exist, create mqtt client
    const mqttClient = AWSIoTData.device({
      region: awsConfig.region,
      host: awsConfig.mqttEndpoint,
      clientId: awsConfig.clientId,
      protocol: "wss",
      maximumReconnectTimeMs: 8000,
      debug: false,
      accessKeyId: String(cred.AccessKeyId),
      secretKey: String(cred.SecretAccessKey),
      sessionToken: String(cred.SessionToken),
    });

    mqttClient.on("connect", () => {
      console.log("mqttClient connected");
      mqttClient.publish(
        "mg/autogenstart/toDevice",
        JSON.stringify({ test_data: 1 })
      );
    });

Any ideas why? I've tried using the same topic, and creating an empty rule in IoT Core for mg/autogenstart/toDevice.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda