Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

280
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!