An MQTT client with the topic and wildcard rules, as the core transport. One capability of pamoja, one memory-safe Rust core with bindings for TypeScript, Python, and C#.
npm install @pamoja/mqtt
This pulls in @pamoja/native, the compiled engine. npm install pamoja is the whole framework in one package.
The test that runs in CI, spliced here as it ran.
From bindings/node/guides/mqtt.ts:
import { MqttClient, Qos } from '@pamoja/mqtt'
// The broker on the site. The guide's CI runs one on localhost; point these at yours and
// nothing else changes.
const BROKER = '127.0.0.1'
const PORT = 1883
async function main(): Promise<{ topic: string; payload: Buffer }> {
// The gateway takes every temperature on the site. A `+` stands for exactly one level,
// so this matches every node's temperature and nothing deeper.
const gateway = new MqttClient({
clientId: 'site-gateway',
host: BROKER,
port: PORT,
qos: Qos.AtLeastOnce,
})
await gateway.connect()
await gateway.subscribe('sensors/+/temperature')
console.log('gateway subscribed to sensors/+/temperature')
// A node publishes under that pattern. At-least-once means the broker acknowledges the
// message, so a node knows its reading was taken rather than hoping.
const node = new MqttClient({
clientId: 'node-1',
host: BROKER,
port: PORT,
qos: Qos.AtLeastOnce,
})
await node.connect()
await node.publish('sensors/1/temperature', '21.5')
console.log('node published 21.5 to sensors/1/temperature')
// The gateway receives it with the topic attached, which is how it knows which node
// sent the reading without the payload having to repeat it.
const received = (await gateway.recv())!
console.log(`gateway got ${received.payload.toString()} on ${received.topic}`)
// Disconnecting leaves the client reusable, so a node that loses its link can reconnect
// the same object when the broker comes back.
await node.disconnect()
console.log(`node disconnected, still connected: ${await node.isConnected()}`)
await gateway.disconnect()
// A broker that is not there is reported rather than leaving a client that looks
// connected, so a retry loop has something to test.
const nowhere = new MqttClient({ clientId: 'node-2', host: BROKER, port: 1, keepAliveSecs: 1 })
try {
await nowhere.connect()
console.log('an unreachable broker accepted a connection, which should never happen')
} catch (error) {
console.log(`unreachable broker refused: ${(error as Error).message}`)
}
return received
}
main()
| Language | Package | Reference |
|---|---|---|
| Rust | pamoja-mqtt |
reference, docs.rs, install |
| TypeScript | @pamoja/mqtt |
reference, install |
| Python | pamoja-mqtt |
reference, install |
| C# | Pamoja.Mqtt |
reference, install |
@pamoja/mqtt reference, every class, function, and type this package exports.MIT
Ergonomic facade over the generated MQTT binding.
Adds rejected promises for errors, an async iterator over incoming messages, and string-or-bytes payloads, without adding behavior; all real work happens in the native core reached through the generated contract.