<a id="cloud-mqtt-config"></a>

# Connect Self-Managed MQTT Proxy to Confluent Cloud

The Confluent Platform MQTT Proxy component enables MQTT clients to use the MQTT 3.1.1 protocol to
publish data directly to Apache Kafka®. You can configure the Confluent MQTT Proxy to produce
to a topic in a Kafka cluster in Confluent Cloud.

The following steps walk you through configuring the MQTT Proxy with Confluent Cloud.

**Prerequisites**

- Access to Confluent Cloud.
- An MQTT Client configured as described in the
  [MQTT Proxy Quick Start](/platform/current/kafka-mqtt/intro.html#install-mqtt-client).

## Set up the cluster

In the following steps, you install the Confluent CLI and use it to sign in to Confluent Cloud,
get the cluster endpoint, and create a topic and an API key that you will use to configure the MQTT proxy.

1. Install Confluent CLI as described in
   [the Confluent CLI installation guide](https://docs.confluent.io/confluent-cli/current/install.html).
   For a list of all of the Confluent CLI commands, see
   [Confluent CLI Command Reference](https://docs.confluent.io/confluent-cli/current/command-reference/index.html).
2. Sign in to your Confluent Cloud cluster.
   ```bash
   confluent login
   ```

   Your output should resemble:
   ```none
   Enter your Confluent credentials:
   Email: jdoe@myemail.io
   Password: ***********************

   Logged in as "jdoe@myemail.io"
   Using environment "t118" ("default")
   ```
3. Run the `confluent kafka cluster list command` to get the Kafka cluster ID.
   ```bash
   confluent kafka cluster list
   ```

   Your output should resemble:
   ```bash
        Id      |        Name         |   Type    | Cloud    |  Region  | Availability | Status
   -------------+---------------------+-----------+----------+----------+--------------+---------
     lkc-m1234  | Dev                 | BASIC     | gcp      | us-west4 | single-zone  | UP
     lkc-r1234  | Test                | BASIC     | gcp      | us-east4 | single-zone  | UP
     lkc-g1234  | Prod                | DEDICATED | gcp      | us-west4 | single-zone  | UP
   ```
4. Set the active Kafka cluster. In this example, the cluster ID is `lkc-m1234`.
   ```bash
   confluent kafka cluster use lkc-m1234
   ```
5. Run the `confluent kafka cluster describe` command to get the endpoint for
   your Confluent Cloud cluster.
   ```bash
   confluent kafka cluster describe
   ```

   Your output should resemble:
   ```text
    +--------------+--------------------------------------------------------+
    | Id           | lkc-m1234                                              |
    | Name         | mqtt-proxy-quickstart                                  |
    | Type         | BASIC                                                  |
    | Ingress      |                                                    100 |
    | Egress       |                                                    100 |
    | Storage      |                                                   5000 |
    | Cloud        | gcp                                                    |
    | Availability | single-zone                                            |
    | Region       | us-west2                                               |
    | Status       | UP                                                     |
    | Endpoint     | SASL_SSL://pkc-12345.us-west2.gcp.confluent.cloud:9092 |
    | ApiEndpoint  | https://pkac-12345.us-west2.gcp.confluent.cloud        |
    +--------------+--------------------------------------------------------+
   ```

   Save the `Endpoint` value, which you’ll use to configure the bootstrap server for the
   MQTT Proxy.
6. Create a Kafka topic that the MQTT proxy will produce to.
   Use the Confluent CLI to create a topic named “temperature”.
   ```bash
   confluent kafka topic create temperature
   ```

<a id="cloud-mqtt-create-api-key"></a>
1. Create a Kafka API key and secret that the MQTT proxy can use to access Confluent Cloud.
   You must specify the cluster with the `resource` flag for this step.
   ```bash
   confluent api-key create --resource lkc-m1234
   ```

   Your output should resemble:
   ```text
   It may take a couple of minutes for the API key to be ready.
   Save the API key and secret. The secret is not retrievable later.
   +---------+------------------------------------------------------------------+
   | API Key | ABCXQHYDZXMMUDEF                                                 |
   | Secret  | aBCde3s54+4Xv36YKPLDKy2aklGr6x/ShUrEX5D1Te4AzRlphFlr6eghmPX81HTF |
   +---------+------------------------------------------------------------------+
   ```

   #### IMPORTANT
   **Save the API key and secret.** You need this information to
   configure your applications that communicate with Confluent Cloud. This is the *only*
   time that you can access, view, and save the key and secret.

## Configure and start the MQTT Proxy

You can either configure and start the MQTT proxy by downloading Confluent Platform locally, or using a
Docker container.

### Local install

1. Install [Confluent Platform](/platform/current/installation/index.html).
2. Customize the `/etc/confluent-kafka-mqtt/kafka-mqtt-dev.properties` properties file,
   specifying:
   - The Confluent Cloud Endpoint that you saved earlier for the bootstrap server.
   - Security information including the Confluent Cloud API key and secret you created in the previous section.
   - Topic information.

   ```text
   # add bootstrap server
   bootstrap.servers=pkc-12345.us-west2.gcp.confluent.cloud:9092

   #configure connection to Confluent Cloud
   producer.security.protocol=SASL_SSL
   producer.sasl.mechanism=PLAIN
   producer.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="<api-key>" password="<secret>";

   # configure topic settings
   topic.regex.list=temperature:.*temperature
   confluent.topic.replication.factor=3
   ```
3. Start the MQTT proxy, specifying the properties file:
   ```bash
   bin/kafka-mqtt-start etc/confluent-kafka-mqtt/kafka-mqtt-dev.properties
   ```

### Docker container

Prerequisites:

- Docker version 1.11 or later is [installed and running](https://docs.docker.com/engine/install/).

To run the MQTT Proxy with Docker:

- Use the following command to install and start the MQTT Proxy in a Docker container.
  Set the appropriate environment variables specifying:
  - The Confluent Cloud Endpoint that you saved earlier for the bootstrap server.
  - Security information including the Confluent Cloud API key and secret you created in the previous section.
  - Topic information.
- You may need to substitute the [latest version](/platform/current/platform.html) of Confluent Platform for `7.2.1` in `confluentinc/cp-kafka-mqtt:7.2.1`.
  ```bash
  docker run -it --rm -p 1883:1883 --name mqtt-proxy \
  -e KAFKA_MQTT_BOOTSTRAP_SERVERS=pkc-12345.us-west2.gcp.confluent.cloud:9092 \
  -e KAFKA_MQTT_PRODUCER_SECURITY_PROTOCOL=SASL_SSL \
  -e KAFKA_MQTT_PRODUCER_SASL_MECHANISM=PLAIN \
  -e KAFKA_MQTT_PRODUCER_SASL_JAAS_CONFIG="org.apache.kafka.common.security.plain.PlainLoginModule required username='<api-key>' password='<secret>';" \
  -e KAFKA_MQTT_TOPIC_REGEX_LIST="temperature:.*temperature" \
  -e KAFKA_MQTT_CONFLUENT_TOPIC_REPLICATION_FACTOR=3 \
  confluentinc/cp-kafka-mqtt:7.2.1
  ```

## Send messages with the MQTT client

You can now send messages to the MQTT proxy using your client.

- Use the previously configured [MQTT client](/platform/current/kafka-mqtt/intro.html#install-mqtt-client)
  to publish messages to the MQTT proxy.
  ```bash
  mosquitto_pub -h 0.0.0.0 -p 1883 -t car/engine/temperature -q 2 -m "190F"
  mosquitto_pub -h 0.0.0.0 -p 1883 -t car/engine/temperature -q 2 -m "200F"
  mosquitto_pub -h 0.0.0.0 -p 1883 -t car/engine/temperature -q 2 -m "210F"
  ```

## View MQTT messages in Confluent Cloud

Sign in to the Cloud Console, and access the `temperatures` topic
to see the MQTT messages.

Click the **Messages** tab, and you should see the messages sent from your client to the
MQTT Proxy and then to Confluent Cloud.

![image](images/ccloud-mqtt-messages.png)
