#tauri-plugin #tauri #mqtt #plugin #mqtt-client

sys tauri-plugin-mqtt

MQTT Client for Tauri App

1 unstable release

0.1.0 Oct 19, 2024

#21 in #mqtt-client

Download history 154/week @ 2024-10-14 34/week @ 2024-10-21 7/week @ 2024-10-28

195 downloads per month

MIT license

24KB
529 lines

tauri-plugin-mqtt

This plugin only works with Tauri 2.x only.

Install

cargo add tauri-plugin-mqtt
npm i @kuyoonjo/tauri-plugin-mqtt

Usage

rust


tauri::Builder::default()
    .plugin(tauri_plugin_tcp::init())
    ...

javascript

import { connect, disconnect, publish, subscribe, unsubscribe } from "@kuyoonjo/tauri-plugin-mqtt";

// Server side
const id = 'unique-id';
await connect(id, 'mqtt://test.mosquitto.org');
await disconnect(id);
let topic = '/tauri-plugin-mqtt';
await subscribe(id, topic, 0);
await publish(id, topic, 0, false, 'hello');
await listen((x) => {
  console.log(x.payload);
});
await unsubscribe(id, topic);
await disconnect(id);

Functions

export async function publish(
  id: string,
  topic: String,
  qos: number,
  retain: boolean,
  payload: string | number[],
);

export async function subscribe(
  id: string,
  topic: String,
  qos: number,
);

export async function unsubscribe(
  id: string,
  topic: String,
);

export async function connect(id: string, uri: string, tlsOptions?: TlsOptions);

export async function disconnect(id: string);

export function listen(handler: EventCallback<Payload>, options?: Options);

Interfaces

export interface TlsOptions {
  skipVerification?: boolean;
  ca?: number[];
  alpn?: number[][];
  client_cert?: number[];
  client_key?: number[];
}

export interface Payload {
  id: string;
  event: {
    connect?: [];
    disconnect?: [];
    message?: {
      dup: boolean;
      qos: 0 | 1 | 2;
      retain: boolean;
      topic: string;
      pkid: number;
      payload: number[];
    };
  };
}

permissions

add "mqtt:default" into "permissions" list of src-tauri\capabilities\default.json

{
  "$schema": "../gen/schemas/desktop-schema.json",
  ...
  "permissions": [
    "core:default",
    ...
    "mqtt:default"
  ]
}

Support

MacOS Linux Windows

Dependencies

~19–61MB
~1M SLoC