> ## 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.

# Read messages

> Receive a topic’s messages by subscribing to <topic ID>/out over MQTT or MQTTS.

Messages are read over MQTT. Every topic has a read channel: subscribe to **`<topic ID>/out`** and the platform delivers the topic’s messages to your client as they arrive.

| Direction | MQTT topic | Who |
| - | - | - |
| Publish | `<topic ID>` | Producer credential, producer subaccount, or account token |
| Read | `<topic ID>/out` | Consumer credential, consumer subaccount, or account token |

The topic ID is on the topic card, for example `u0123456789abcdef-orders`, so the read channel is `u0123456789abcdef-orders/out`.

> [!NOTE]
>
> HTTP and HTTPS connections only publish: send a `POST` to the connection URL. Reading always goes
> through MQTT.

## Connect

Use the MQTT or MQTTS broker URL from the topic card and a client ID that is unique for every session. Then authenticate in one of two ways.

> [tab] Access token
>
> Works with an account token or a consumer [subaccount](https://docs.link4sync.com/en/manage/subaccounts.md) token.
>
> ```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
> Subscribe  <topic ID>/out, QoS 1
> ```
>
> The token is checked when the client connects. Get a fresh token for every new connection.

> [tab] SCRAM credential
>
> Reveal the **Consumer** credential for MQTT on the topic card and connect with MQTT 5 enhanced
> authentication (SCRAM-SHA-512), exactly as in [Publish over MQTT](https://docs.link4sync.com/en/publish/mqtt.md), but with
> the consumer username and password.
>
> ```text theme={null}
> Broker     mqtts://mqtt.link4sync.com:8883   (or wss://mqtt.link4sync.com/mqtt)
> Protocol   MQTT 5 with SCRAM-SHA-512
> Username   consumer credential from the topic card
> Client ID  unique for every session
> Subscribe  <topic ID>/out, QoS 1
> ```

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

## Example with MQTT.js

```js read.mjs theme={null}
import mqtt from 'mqtt';
import { randomUUID } from 'node:crypto';

const topicId = process.env.LINK4SYNC_TOPIC_ID;
const client = mqtt.connect(process.env.LINK4SYNC_MQTT_URL, {
  protocolVersion: 5,
  clientId: `reader-${randomUUID()}`,
  username: topicId,
  password: process.env.LINK4SYNC_TOKEN, // an account or consumer subaccount token
});

client.on('connect', () => client.subscribe(`${topicId}/out`, { qos: 1 }));
client.on('message', (_topic, payload) => console.log(JSON.parse(payload.toString())));
```

## How delivery works

* Delivery to `/out` runs while at least one client is subscribed. The first subscription can take a little while to start it.
* When nobody is subscribed, the topic keeps the messages. Delivery resumes from where it stopped when a client subscribes again, as long as the topic still keeps those messages (seven days by default, see [Plans and limits](https://docs.link4sync.com/en/limits.md)).
* Messages are delivered to the clients subscribed at that moment.

## Read more than one topic

Consumers can also read the topics you allowed in [Read access](https://docs.link4sync.com/en/manage/read-access.md):

* **SCRAM consumer credential:** one connection can subscribe to `<topic ID>/out` of every allowed topic.
* **Access token:** the username names one topic, so open one connection per topic, each with that topic’s ID as the username. A consumer subaccount reaches the topics you ticked under **Also read from**.

> [!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.