Skip to content

HiveMQ

Install

1
npm install @testcontainers/hivemq --save-dev

Examples

These examples use the following libraries:

  • mqtt
    1
    npm install mqtt
    

Choose an image from the container registry and substitute IMAGE.

Produce/consume a message

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
await using container = await new HiveMQContainer(IMAGE).start();

const mqttClient = await mqtt.connectAsync(container.getConnectionString());

const firstMessagePromise = new Promise<{ topic: string; message: Buffer }>((resolve, reject) => {
  mqttClient.once("message", (topic, message) => resolve({ topic, message }));
  mqttClient.once("error", (err) => reject(err));
});

await mqttClient.subscribeAsync("test");
await mqttClient.publishAsync("test", "Test Message");

const { message } = await firstMessagePromise;
expect(message.toString()).toEqual("Test Message");

mqttClient.end();