Skip to content

MQTT Driver

The MQTT Driver is a signaling driver that uses MQTT as the transport layer to exchange signaling messages between peers via a broker such as Mosquitto. It is a good choice when you want lightweight pub/sub signaling, broad protocol support, and compatibility with existing IoT or messaging infrastructure.

Peerix uses this driver to publish and subscribe to signaling messages in namespaces mapped to MQTT topics. Peers connected to the same broker and namespace can discover each other and establish direct peer-to-peer connections.

Usage

To use the MQTT Driver, import it from Peerix, create an MQTT client, connect the client to your broker over WebSocket, and pass the client to the driver.

import { MqttDriver } from 'peerix';
import { connect } from 'mqtt';

// connect to an MQTT broker over WebSocket
const client = connect('wss://broker.emqx.io:8084/mqtt');

// create an MQTT driver instance
const driver = new MqttDriver({
  // the MQTT client instance
  client,
  // optional prefix for MQTT topics
  prefix: 'peerix/',
});

In this setup, the MQTT client manages broker connectivity while the Peerix driver handles signaling semantics. In production, prefer using wss:// with authentication and access control enabled on the broker.

Broker Setup

You need an MQTT broker with WebSocket support enabled for browser clients. For local development, Mosquitto is a common choice.

For a self-hosted setup, see the Mosquitto self-hosting guide. For a managed option, see the cloud hosting guide for recommended providers and free public endpoints.

Installation

Install the mqtt package in your project to use the MQTT Driver in the browser.

npm install mqtt