tauri-plugin-mqtt

Crates.iotauri-plugin-mqtt
lib.rstauri-plugin-mqtt
version0.1.0
sourcesrc
created_at2024-10-19 18:33:00.225334
updated_at2024-10-19 18:33:00.225334
descriptionMQTT Client for Tauri App
homepage
repositoryhttps://github.com/kuyoonjo/tauri-plugin-mqtt
max_upload_size
id1415635
size48,638
Yu Chen (kuyoonjo)

documentation

README

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
Commit count: 3

cargo fmt