# MQTT Publish/Subscribe

Test Soracom Beam with an MQTT publish and subscribe action.

Use Beam's [MQTT entry point](https://docs.soracom.io/en/services/beam/mqtt) and Soracom's [MQTTS test server](https://docs.soracom.io/en/services/beam/test-servers) to verify how Beam forwards MQTT publish and subscribe operations.

![MQTT flow via Beam to MQTTS test server](https://docs.soracom.io/_astro/beam-mqtt.XbEWP4kr_SofDD.webp)

No special setup is required to use the MQTTS test server. In this tutorial, you will add an MQTT entry point configuration and perform MQTT publish and subscribe operations from your device.

> [!WARNING]
>
> The device connects to the MQTT entry point (`beam.soracom.io:1883`). The device does not need to know the MQTTS test server URL or credentials.

## Configuring MQTT Entry Point

Add an MQTT entry point configuration to your SIM group and assign your IoT SIM to the group.

> [!WARNING]
>
> Beam settings are configured at the group level. For more information about groups, see [Groups](https://docs.soracom.io/en/services/groups).

1. In the SIM group settings screen, choose a group, then select **Soracom Beam**.

   For detailed instructions on how to open the SIM Group settings screen, see [Group Settings](https://docs.soracom.io/en/services/groups/settings).

2. Click **+ Add Configuration** and select **MQTT entry point**.

   The Soracom Beam - MQTT configuration screen appears.

3. Configure the following settings:

   | Item | Value |
   | - | - |
   | **Configuration name** | Enter any name. Example: `MQTT over TLS` |
   | **Destination** > **Type** | Select `Standard`. |
   | **Destination** > **Protocol** | Select `MQTTS`. |
   | **Destination** > **Host name** | Enter `beamtest.soracom.io`. |
   | **Destination** > **Port number** | Enter `8883`. |
   | **Username** | Enter `beamuser`. |
   | **Password** | Enter `passwd`. |

   For more information about MQTT entry point settings, see [MQTT Entry Point](https://docs.soracom.io/en/services/beam/mqtt).

4. Click **Save**.

5. [Assign your IoT SIM to the group](https://docs.soracom.io/en/services/groups/usage#managing-devices).

   Beam configuration is now complete.

## Publishing Data to a Topic

In this example, your IoT device publishes a message through Beam to the MQTTS test server. An external client subscribes directly to the test server to receive the message.

**Roles in this example:**

- **Publisher**: IoT device with SIM → connects to Beam (`beam.soracom.io:1883`)
- **Subscriber**: External client → connects directly to MQTTS test server (`beamtest.soracom.io:8883`)
- **Beam**: Forwards published messages to the test server, handling TLS and authentication

> [!WARNING]
>
> Beam charges are incurred per request. A free tier is available. For more information, see [Soracom Beam Pricing](https://docs.soracom.io/en/pricing#soracom-beam).

1. On any device with an MQTT client, subscribe to a topic using the following settings:

   | Item | Value |
   | - | - |
   | **Host** | `beamtest.soracom.io:8883` |
   | **Topic** | `test` |
   | **Username** | `beamuser` |
   | **Password** | `passwd` |

   Example using `mosquitto_sub`:

   ```bash
   mosquitto_sub -h beamtest.soracom.io -p 8883 -t test -u beamuser -P passwd
   ```

2. From a device using an IoT SIM configured with the MQTT entry point, publish a message using the following settings:

   | Item | Value |
   | - | - |
   | **Host** | `beam.soracom.io` |
   | **Topic** | `test` |
   | **Message** | `Hello World` |

   Example using `mosquitto_pub`:

   ```bash
   mosquitto_pub -h beam.soracom.io -p 1883 -t test -m "Hello World"
   ```

   The external client displays:

   ```text
   Hello World
   ```

> [!WARNING]
>
> The IoT device using Beam does not need to specify the MQTTS test server username and password. Beam handles the authentication.

## Subscribing to a Topic

In this example, your IoT device subscribes through Beam to receive messages from the MQTTS test server. An external client publishes directly to the test server.

**Roles in this example:**

- **Subscriber**: IoT device with SIM → connects to Beam (`beam.soracom.io:1883`)
- **Publisher**: External client → connects directly to MQTTS test server (`beamtest.soracom.io:8883`)
- **Beam**: Maintains the subscription and delivers incoming messages, handling TLS and authentication

> [!WARNING]
>
> Beam charges are incurred per request. A free tier is available. For more information, see [Soracom Beam Pricing](https://docs.soracom.io/en/pricing#soracom-beam).

1. From a device using an IoT SIM configured with the MQTT entry point, subscribe to a topic using the following settings:

   | Item | Value |
   | - | - |
   | **Host** | `beam.soracom.io` |
   | **Topic** | `test` |

   Example using `mosquitto_sub`:

   ```bash
   mosquitto_sub -h beam.soracom.io -p 1883 -t test
   ```

2. On any device with an MQTT client, publish a message using the following settings:

   | Item | Value |
   | - | - |
   | **Host** | `beamtest.soracom.io:8883` |
   | **Topic** | `test` |
   | **Username** | `beamuser` |
   | **Password** | `passwd` |
   | **Message** | `Hello World` |

   Example using `mosquitto_pub`:

   ```bash
   mosquitto_pub -h beamtest.soracom.io -p 8883 -t test -u beamuser -P passwd -m "Hello World"
   ```

   The IoT device displays:

   ```text
   Hello World
   ```

> [!WARNING]
>
> The IoT device using Beam does not need to specify the MQTTS test server username and password. Beam handles the authentication.

## Disabling Beam Configuration

Beam charges are incurred per request. If you are not using Beam, disable the entry point configuration or remove the IoT SIM from the group.

- To stop forwarding requests via Beam, open your SIM group settings and disable or delete the MQTT entry point configuration.
- To remove an IoT SIM from a group, see [Removing a Device from a Group](https://docs.soracom.io/en/services/groups/usage#removing-a-device-from-a-group).
