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.