private void mqttSetup()

in serverless-ui/jwt-stack/src/main/java/com/awssamples/client/shell/ShellPresenter.java [103:152]


    private void mqttSetup() {
        // Close the MQTT client if it exists already
        if (mqttClient != null) {
            mqttClient.end(true);
        }

        mqttClient = null;

        eventBus.fireEvent(new MqttConnectionAttempt.Event());

        // Use the user ID in their cookies, if there is one
        String userId = Option.of(Cookies.getCookie(USER_ID_COOKIE_NAME))
                // If there isn't then just send a NULL
                .getOrNull();

        JWT_SERVICE_ASYNC.getClientConfig(
                new AsyncCallback<ClientConfig>() {
                    @Override
                    public void onFailure(Throwable caught) {
                        eventBus.fireEvent(new MqttConnectionFailed.Event());
                        Window.alert("Couldn't get credentials for an MQTT connection [" + caught.getMessage() + "]");
                    }

                    @Override
                    public void onSuccess(ClientConfig clientConfig) {
                        eventBus.fireEvent(new RegionDetected.Event(clientConfig.region));
                        userIdOption = Option.of(clientConfig.userId);
                        clientIdOption = Option.of(clientConfig.clientId);

                        JSONObject jsonObject = new JSONObject();

                        jsonObject.put("region", new JSONString(clientConfig.region));
                        jsonObject.put("host", new JSONString(clientConfig.endpointAddress));
                        jsonObject.put("clientId", new JSONString(clientConfig.clientId));
                        jsonObject.put("protocol", new JSONString(WEBSOCKET_PROTOCOL));
                        jsonObject.put("maximumReconnectTimeMs", new JSONNumber(8000));
                        jsonObject.put("debug", JSONBoolean.getInstance(true));
                        jsonObject.put("accessKeyId", new JSONString(clientConfig.accessKeyId));
                        jsonObject.put("secretKey", new JSONString(clientConfig.secretAccessKey));
                        jsonObject.put("sessionToken", new JSONString(clientConfig.sessionToken));

                        // NOTE: Don't try to do anything fancy here with Try or Option. Native JavaScript types don't work well with them.
                        mqttClient = AWSIoTData.device(jsonObject.getJavaScriptObject());

                        mqttClient.subscribe(String.join("/", TOPIC_PREFIX, "+"));

                        eventBus.fireEvent(new MqttConnectionSuccessful.Event(mqttClient));
                    }
                });
    }