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

# Publish over MQTT

> Connect devices and browsers over MQTT or MQTTS and publish to the topic ID, with an access token or a SCRAM-SHA-512 credential.

Publish to the MQTT topic **`<topic ID>`**. To receive the topic’s messages, subscribe to `<topic ID>/out` instead, see [Read messages](https://docs.link4sync.com/en/read-messages.md).

You can authenticate with an access token or with the topic’s producer SCRAM credential.

## With an access token

Use an account token or a producer [subaccount](https://docs.link4sync.com/en/manage/subaccounts.md) token as the password:

```text theme={null}
Broker     mqtts://mqtt.link4sync.com:8883   (or wss://mqtt.link4sync.com/mqtt)
Username   <topic ID>
Password   <access token>
Client ID  unique for every session
Topic      <topic ID>
QoS        1
```

The token is checked when the client connects. Get a fresh token for every new connection.

## With SCRAM

The producer credential uses MQTT 5 enhanced authentication with SCRAM-SHA-512. Use a client library that supports the MQTT 5 `AUTH` packet, for example the `handleAuth` hook in MQTT.js.

### Before you start

Reveal the **Producer** credential for MQTT on the topic card and note:

| Value | Where to find it |
| - | - |
| Broker URL | Topic card, MQTT or MQTTS |
| Topic ID | Topic card, for example `u0123456789abcdef-orders` |
| Producer username | Topic card, Producer credential |
| Producer password | Topic card, Producer credential |

### Connection settings

```text theme={null}
Broker     mqtts://mqtt.link4sync.com:8883   (or wss://mqtt.link4sync.com/mqtt)
Protocol   MQTT 5 with SCRAM-SHA-512
Username   producer credential from the topic card
Topic      <topic-id>
Client ID  unique for every session
QoS        1
```

Premium endpoints use their own hostnames. Copy the exact URL from the topic card.

### The exchange

Authentication data is the raw UTF-8 SCRAM message, not base64.

**1. CONNECT**

Protocol version 5, a client ID that is unique for every session, username = producer SCRAM username, **no password**.

Properties: *Authentication Method* = `SCRAM-SHA-512`, *Authentication Data* = `n,,n=<username>,r=<client-nonce>`.

**2. AUTH from the broker**

Reason code `0x18` (continue). *Authentication Data* = server-first (`r=…,s=…,i=…`).

**3. AUTH from the client**

Reason code `0x18`, *Authentication Method* = `SCRAM-SHA-512`, *Authentication Data* = client-final (`c=biws,r=<server-nonce>,p=<proof>`).

Compute the proof as in the [SCRAM-SHA-256 reference](https://docs.link4sync.com/en/publish/http-scram.md), but with SHA-512 and a 64-byte PBKDF2 key.

**4. CONNACK**

The broker replies with reason code `0x00`. If it includes a server signature, verify it.

**5. PUBLISH**

Publish a JSON payload with QoS 1 to the topic ID and wait for **PUBACK**.

> [!WARNING]
>
> Two sessions with the same client ID disconnect each other. Give every session its own client ID.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.