> ## Documentation Index
> Fetch the complete documentation index at: https://ngrok.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# MQTT

> Learn how to expose your MQTT server to the internet using ngrok TCP tunnels and connect remotely with MQTT Explorer.

This guide shows you how to expose your MQTT server to the internet using ngrok.
It covers setting up a local MQTT server, creating an ngrok TCP tunnel, and connecting to it using MQTT Explorer.

## What you'll need

* An [ngrok account](https://ngrok.com/signup).
* The [ngrok agent](https://download.ngrok.com) installed.
* [MQTT Explorer](https://mqtt-explorer.com/) installed.
* The [Mosquitto binary](https://mosquitto.org/download/) installed.

## 1. Start an MQTT server

For this tutorial, use the [Eclipse Mosquitto quickstart](https://github.com/eclipse-mosquitto/mosquitto).

Start the server by running this command in your terminal:

```bash theme={null}
mosquitto
```

The server runs by default on port `1883`.
You'll see output like this:

```bash theme={null}
1734408888: mosquitto version 2.0.20 starting
1734408888: Using default config.
1734408888: Starting in local only mode. Connections will only be possible from clients running on this machine.
1734408888: Create a configuration file which defines a listener to allow remote access.
1734408888: For more details see https://mosquitto.org/documentation/authentication-methods/
1734408888: Opening ipv4 listen socket on port 1883.
1734408888: Opening ipv6 listen socket on port 1883.
1734408888: mosquitto version 2.0.20 running
```

## 2. Launch ngrok

Once your MQTT server is running successfully on `localhost`, you can put it online securely using ngrok.

Go to the [ngrok dashboard](https://dashboard.ngrok.com) and copy your authtoken.

Start ngrok by running the following command:

```bash theme={null}
ngrok tcp 1883
```

ngrok will display a URL where your localhost application is exposed to the internet.
Copy this URL for use with MQTT Explorer.

## 3. Connect with MQTT Explorer

Open [MQTT Explorer](https://mqtt-explorer.com/) and click the plus sign to create a new MQTT connection.
Enter the TCP address from the last step and click **Connect**.

You'll see a few new lines in your MQTT server logs:

```bash theme={null}
1734410153: New connection on port 1883.
1734410153: New client connected as mqtt-explorer-ba9b56f9 (p2, c1, k60).
```

MQTT Explorer is now connected to your server over the public internet.
To get messages from the client, subscribe to a topic.
In your terminal, open a tab and enter:

```bash theme={null}
mosquitto_sub -t 'example/topic' -v
```

Back in MQTT Explorer, publish a message to `example/topic`.

You'll see the message in the terminal tab where you subscribed to the topic:

```bash theme={null}
example/topic hello world
```
