darrickc
darrickc

Reputation: 1932

Does the Azure iot device sdk support multiple devices connecting at the same time?

I'm trying to write a little application that simulates many devices connecting and sending messages via MQTT protocol to an iot hub using the azure iot-device-client sdk version: 1.3.31. Eventually this will be multi-threaded and it will have thousands of simulated devices connecting simultaneously. This doesn't seem possible with the device client sdk. Can the device sdk be used this way?

IotHubClientProtocol protocol = IotHubClientProtocol.MQTT;

List<String> conns = new ArrayList<>();
//conns.add() 10 times with correct connection strings    

conns.forEach((newConnString) -> {
    try {
        DeviceClient client = new DeviceClient(newConnString, protocol);

        client.open();
        System.out.println("connected - "+newConnString);

    } catch (Exception e) {
        e.printStackTrace();
    }
});

When I run this I get every other device connecting with the rest getting forcably disconnected with this error:

java.io.IOException: Unable to subscribe to topic :devices/sim_3/messages/devicebound/# because java.io.EOFExceptionConnection lost at com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection.open(MqttIotHubConnection.java:140) at com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport.open(MqttTransport.java:83) at com.microsoft.azure.sdk.iot.device.DeviceIO.open(DeviceIO.java:212) at com.microsoft.azure.sdk.iot.device.DeviceClient.open(DeviceClient.java:197) at com.company.Main.lambda$main$0(Main.java:49) at java.util.ArrayList.forEach(ArrayList.java:1249) at com.company.Main.main(Main.java:45)

Upvotes: 0

Views: 1444

Answers (2)

Roman Kiss
Roman Kiss

Reputation: 8235

In addition to Rita's answer, I do recommend to use for each simulated MQTT device own AppDomain and lightweight (raw) MQTT Client Library (e.g. M2Mqtt). This concept has been used in the Azure IoT Hub Tester, where each MQTT Device is isolated, hosted, subscribed and published in own AppDomain. The following screen snippet shows 30 virtual MQTT Devices connected to the Azure IoT Hub:

enter image description here

Upvotes: 2

Rita Han
Rita Han

Reputation: 9700

For MQTT protocol, it is not supported connecting multiple devices over the same TLS connection concurrently.

IoT Hub only supports one active MQTT connection per device. Any new MQTT connection on behalf of the same device ID causes IoT Hub to drop the existing connection.

For your use case, you can choose AMQP. It supports connection multiplexing across devices.

Ref: choose a communication protocol.

Upvotes: 1

Related Questions