hivemq / hivemq-mqtt-client

HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
https://hivemq.github.io/hivemq-mqtt-client/
Apache License 2.0
860 stars 159 forks source link

Subscription working over LAN but not internet #529

Closed CJure closed 2 years ago

CJure commented 2 years ago

Expected behavior

I am trying to connect and subscribe to our broker over internet.

Actual behavior

Connection is successful but when new value is sent to broker from another device, subscription callback is not being called. But this works normally if I am in same LAN as broker. I tried different devices and different networks, even cellphone hotspot but if client and broker are not in same LAN, subscription doesn't work. If use other client like Paho it works even outside LAN, so it's not problem in the broker.

Reproducer code

private void openConnection()
    {
        logger.info("openConnection() start");
        try
        {
            client.connectWith()
                    .simpleAuth()
                    .username(ServerConfig.getUser())
                    .password(ServerConfig.getPassword().getBytes())
                    .applySimpleAuth()
                    .send()
                    .whenComplete((connAck, throwable) ->
                    {
                        logger.info("connAck={}" ,connAck);
                        if (throwable != null)
                        {
                            logger.error("connect error");
                            logger.error("couldn't connect to broker={}", throwable);
                        } else
                        {
                            logger.info("mqtt client connected");
                            subscribe();
                        }
                    });
        }
        catch (Exception e)
        {
            logger.error("Exception on openConnection={}", e);
        }
        logger.info("openConnection() end");
    }

private void subscribeToTopic(String topic)
    {
        client.subscribeWith()
                .topicFilter(topic)
                .qos(MqttQos.AT_LEAST_ONCE)
                .noLocal(true)
                .callback(publish ->
                {
                    logger.info("callback messageArrived topic={}", publish.getTopic());
                    float floatValue = Float.parseFloat(new String(publish.getPayloadAsBytes(), StandardCharsets.UTF_8));
                    logger.info("messageArrived topic: {}, value: {}", publish.getTopic(), floatValue);
                    main.onCommandFromScada(topic, floatValue);
                })
                .send()
                .whenComplete((subAck, throwable) ->
                {
                    if (throwable != null)
                    {
                        logger.error("error on subscribe to topic={} error={}", topic, throwable);
                    }
                    else
                    {
                        logger.info("subscribed to topic={}", topic);
                        if(topic.equals(lastSubTopic))
                        {
                            isConnected = true;
                        }
                    }
                });
    }

Details

CJure commented 2 years ago

It was problem in my logic. Variables weren't created yet at subscription step, so it didn't make any subscriptions.