Publish/Subscribe to IoT device messages via Java SDK?

0

I'm using the aws Java SDK to build a Java server application that works with IOT services.

My server application is designed to be a supervisor that can communicate with all of the IOT devices in my network.

So far I've been able to instantiate an instance of IotClient using an AWS access key/secret key pair. I can call functions like listThings() or describeEndpoint() which return valid results.

I'm confused as to how to proceed from here. I need a simple way to publish and subscribe to messages, but there doesn't seem to be any such functionality available in the IotClient class.

I found some documentation pointing me to an "AWS Iot Device Service" SDK, but I don't think this is what I want; I think it's probably intended for devices.

What's the simplest way for my Java server application to publish and subscribe to messages?

Thanks, Frank

  • So far I've been able to retrieve and update shadows using IotDataPlaneClient, and I can also publish messages. What I'm struggling with is that although I can publish, I can't subscribe. It's essential that I be able to subscribe. How is this accomplished?

Frank
asked 2 years ago1219 views
1 Answer
0

The Java SDK doesn't support subscribing. The Java SDK is a relatively thin wrapper around the AWS services API (HTTPS). There is no API for subscribing: https://docs.aws.amazon.com/iot/latest/apireference/API_Operations_AWS_IoT_Data_Plane.html

To subscribe, you would need to connect to the AWS IoT Core MQTT broker. As Ryan_B has indicated elsewhere, one solution is to use the device SDK: https://github.com/aws/aws-iot-device-sdk-java-v2/tree/main/samples#basicpubsub

However, do you have need of receiving messages in real-time? It's not so common for web applications to connect directly to IoT Core. Solutions are often architected such that incoming device messages would typically flow through IoT Core to Rules Engine and then to another AWS service. Perhaps Kinesis, SQS or a storage service. Decoupling the application from the flow of incoming messages. The application would then retrieve the data from these other services, often through API gateway. Something like this: https://docs.aws.amazon.com/solutions/latest/smart-product-solution/architecture.html .

Amplify is also a popular choice: https://docs.amplify.aws/lib/pubsub/getting-started/q/platform/js/

profile pictureAWS
EXPERT
Greg_B
answered 2 years ago
  • Thanks for the response. I have installed the package you suggested, and gotten the publish and subscribe functions to work.

    My use case is fairly simple and most of the messages will be flowing from my application to the devices. I plan to use the device "shadows" for this communication. As an FYI, my application is not a web application, rather, it is a mobile application with a server that is hosted on EC2 (and it is the EC2 instance that connects to IoT core on behalf of the app user, not the app itself).

    I do have one infrequent use case where a device needs to signal back an alarm condition, which needs to be acted on quickly. My thinking is that I'll have all of the devices send that alarm to a common message queue, which I will monitor on my server. Does this sound feasible? I haven't tried it yet, but I don't think there's anything that would prevent me from having all of my devices publish messages to some standard queue such as "alarm".

    As far as the other Amazon services you mention, I haven't looked at them yet but I'm not sure I'll really need them given the simplicity of what I'm doing. I'll certainly keep them in mind if more complex interactions are needed later.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions