{"id":29331467,"url":"https://github.com/tommy44458/mqtt-vue-hook","last_synced_at":"2025-10-11T14:43:13.837Z","repository":{"id":47617766,"uuid":"485314514","full_name":"tommy44458/mqtt-vue-hook","owner":"tommy44458","description":"Mqtt-Vue-hook is a TypeScript library that makes it easy to connect to an MQTT broker (supports v5) and manages callback functions for various topics.","archived":false,"fork":false,"pushed_at":"2024-02-03T07:45:38.000Z","size":198,"stargazers_count":25,"open_issues_count":8,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-15T06:48:58.861Z","etag":null,"topics":["hook","mqtt","react","reactjs","typescript","vue","vue-hook","vue3","vue3-typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/mqtt-vue-hook","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tommy44458.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-04-25T09:56:32.000Z","updated_at":"2024-11-21T14:56:54.000Z","dependencies_parsed_at":"2024-02-03T09:00:19.235Z","dependency_job_id":null,"html_url":"https://github.com/tommy44458/mqtt-vue-hook","commit_stats":{"total_commits":83,"total_committers":2,"mean_commits":41.5,"dds":0.2530120481927711,"last_synced_commit":"1237d165e425e8a97b81e841674940df2c3e86fe"},"previous_names":["tommy44458/mqtt-ts-hooks"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tommy44458/mqtt-vue-hook","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tommy44458%2Fmqtt-vue-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tommy44458%2Fmqtt-vue-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tommy44458%2Fmqtt-vue-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tommy44458%2Fmqtt-vue-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tommy44458","download_url":"https://codeload.github.com/tommy44458/mqtt-vue-hook/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tommy44458%2Fmqtt-vue-hook/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262022052,"owners_count":23246245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hook","mqtt","react","reactjs","typescript","vue","vue-hook","vue3","vue3-typescript"],"created_at":"2025-07-08T01:01:28.815Z","updated_at":"2025-10-11T14:43:08.817Z","avatar_url":"https://github.com/tommy44458.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mqtt-Vue-Hook\n\n[![NPM version](https://img.shields.io/npm/v/mqtt-vue-hook.svg)](https://www.npmjs.com/package/mqtt-vue-hook)\n[![NPM download count](https://img.shields.io/npm/dt/mqtt-vue-hook)](https://www.npmjs.com/package/mqtt-vue-hook)\n[![languages](https://img.shields.io/github/languages/top/tommy44458/mqtt-vue-hook)](https://www.npmjs.com/package/mqtt-vue-hook)\n[![license](https://img.shields.io/npm/l/mqtt-vue-hook)](https://www.npmjs.com/package/mqtt-vue-hook)\n[![codacy](https://img.shields.io/codacy/grade/167baac7ff374d359dac9f885f566c0f)](https://www.npmjs.com/package/mqtt-vue-hook)\n\nMqtt-vue-hook is a TypeScript library that makes it easy to connect to an MQTT broker (supports v5) and manages callback functions for various topics.\n\nIt can be used with Vue, Vite, React, and other frameworks based on Typescript. This efficient and effective tool streamlines MQTT-related logic, making it valuable for IoT applications and other MQTT-based scenarios.\n\n## Example\n\n[A LIGHT vue3 starter support MQTT](https://github.com/tommy44458/light-vue3-starter)\n\n## Install\n\n```bash\nnpm install mqtt-vue-hook --save\n```\n\n```bash\nyarn add mqtt-vue-hook -D\n```\n\n## Usage\n\n### Vue or React or Typescript instance\n\n```ts\n// src/app.tsx\n// protocol = 'wss', 'ws', 'mqtt', ...\n// host = ip or domain\n// port = 8083, 1883, ...\nimport { useMQTT } from 'mqtt-vue-hook'\nconst mqttHook = useMQTT()\n\nmqttHook.connect(`${protocol}://${host}:${port}`, {\n    clean: false,\n    keepalive: 60,\n    clientId: `mqtt_client_${Math.random().toString(16).substring(2, 10)}`,\n    connectTimeout: 4000,\n})\n```\n\noptions: https://github.com/mqttjs/MQTT.js#client\n\n### Subscribe\n\n```vue\n\u003cscript setup lang=\"ts\"\u003e\nimport { useMQTT } from 'mqtt-vue-hook'\n\nonMounted(() =\u003e {\n    const mqttHook = useMQTT()\n    // mqttHook.subscribe([...topic], qos, opts?, callback?, clientID?)\n    // mqttHook.unSubscribe(topic, opts?, callback?, clientID?)\n    // '+' == /.+/\n    // '#' == /[A-Za-z0-9/]/\n    mqttHook.subscribe(\n        ['+/root/#'],\n        1,\n        {\n            properties: {\n                userProperties: {...}\n            },\n        },\n        () =\u003e {\n            console.log('subscribed!')\n        }\n    )\n})\n\u003c/script\u003e\n```\n\noptions: https://github.com/mqttjs/MQTT.js#subscribe\n\n### Publish\n\n```vue\n\u003cscript setup lang=\"ts\"\u003e\nimport { useMQTT } from 'mqtt-vue-hook'\n\nonMounted(() =\u003e {\n    const mqttHook = useMQTT()\n    // mqttHook.publish(topic, message, qos?, opts?, callback?, clientID?)\n    mqttHook.publish(\n        ['test/root/1'],\n        'my message',\n        1,\n        {},\n        () =\u003e {\n            console.log('published!')\n        }\n    )\n})\n\u003c/script\u003e\n```\n\noptions: https://github.com/mqttjs/MQTT.js#publish\n\n### Register Event\n\n```vue\n\u003cscript setup lang=\"ts\"\u003e\nimport { useMQTT } from 'mqtt-vue-hook'\n\nconst mqttHook = useMQTT()\n\nonMounted(() =\u003e {\n    // mqttHook.registerEvent(topic, callback function, vm = string, clientID?)\n    // mqttHook.unRegisterEvent(topic, vm)\n    mqttHook.registerEvent(\n        '+/root/#',\n        (topic: string, message: string) =\u003e {\n            Notification({\n                title: topic,\n                message: message.toString(),\n                type: 'info',\n            })\n        },\n        'string_key',\n    )\n    mqttHook.registerEvent(\n        'on-connect', // mqtt status: on-connect, on-reconnect, on-disconnect, on-connect-fail\n        (topic: string, message: string) =\u003e {\n            console.log('mqtt connected')\n        },\n        'string_key',\n    )\n})\n\nonUnmounted(() =\u003e {\n    // mqttHook.unRegisterEvent(topic, vm, clientID?))\n    mqttHook.unRegisterEvent('+/root/#', 'string_key')\n    mqttHook.unRegisterEvent('on-connect', 'string_key')\n})\n\u003c/script\u003e\n```\n\n### Typescript\n\n```ts\nimport { useMQTT } from 'mqtt-vue-hook'\nconst mqttHook = useMQTT()\n\nmqttHook.registerEvent('+/root/1', (topic: string, message: string) =\u003e {\n    console.log(topic, message.toString())\n})\nmqttHook.publish(['test/root/1'], 'my message', 1)\n\n// console log \"test/root/1 my message\"\n```\n\n### Multi-Client\n\n```ts\nimport { useMQTT } from 'mqtt-vue-hook'\nconst mqttHook = useMQTT()\n\nif (!mqttHook.isConnected(\n    'mqtt-client-2' // // clientID\n)) {\n    mqttHook.connect(\n        `${mqttProtocol}://${mqttHost}:${mqttPort}`,\n        {\n            clean: false,\n            keepalive: 60,\n            clientId: `mqtt_client_2_${Math.random().toString(16).substring(2, 10)}`,\n            path: '/mqtt',\n            connectTimeout: 4000,\n        },\n        'mqtt-client-2', // clientID\n    )\n\n    mqttHook.subscribe(\n        ['test/root/1'],\n        1,\n        null,\n        null,\n        'mqtt-client-2' // // clientID\n    )\n\n    mqttHook.registerEvent(\n        'test/root/1',\n        (_topic: string, message: string) =\u003e {\n            // callback\n        },\n        'string_key',\n        'mqtt-client-2', // clientID\n    )\n}\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftommy44458%2Fmqtt-vue-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftommy44458%2Fmqtt-vue-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftommy44458%2Fmqtt-vue-hook/lists"}