AWS IoTが10月にBetaとして登場した時、MQTTの"Will"の機能はありませんでした。
参考: AWS IoT Message Brokerに一般のMessage Brokerにあるべき実装がない理由を考えてみた。
ところが、今のドキュメントを見ると、Device Shadows Data Flowには以下の記載があります。
If you want to determine if a device is currently connected, include a connected
setting in the thing shadow and use an MQTT Last Will and Testament (LWT) message that
will set the connected setting to false if a device is disconnected due to error.
このような機能は最初無かったと思うのですが、追加されたのでしょうか?
AWSのドキュメントが変更されたことをお知らせしてくれるサービスとかあったらいいのに・・
実際に試してみるのは簡単で、例えば thing-passthrough-example.js なら、以下のようにすれば"Will"メッセージを設定することができます。
https://github.com/aws/aws-iot-device-sdk-js/blob/master/examples/thing-passthrough-example.js
const thingShadows = thingShadow({
keyPath: args.privateKey,
certPath: args.clientCert,
caPath: args.caCert,
clientId: args.clientId,
region: args.region,
reconnectPeriod: args.reconnectPeriod
will: {
topic: '$aws/things/thingname/shadow/update',
payload: '{ "state" : { "reported" : { "connected" : "false" }}}'
}
});
上記の例だと、$aws/things/thingname/shadow/update を subscribe していれば、デバイスが切断した時にWillが送信されて来ます。