{"id":15493259,"url":"https://github.com/roccomuso/mqtt-connect","last_synced_at":"2025-04-22T19:49:39.778Z","repository":{"id":57303700,"uuid":"89137420","full_name":"roccomuso/mqtt-connect","owner":"roccomuso","description":"MQTT high level framework to glue together various \"middleware\" to handle incoming messages.","archived":false,"fork":false,"pushed_at":"2017-04-23T22:34:39.000Z","size":14,"stargazers_count":15,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-10T18:56:47.263Z","etag":null,"topics":["broker","connect","framework","iot","middlewares","mqtt","mqttjs","nodejs","plugins"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/roccomuso.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-23T11:39:17.000Z","updated_at":"2018-05-04T00:32:46.000Z","dependencies_parsed_at":"2022-08-24T17:10:22.184Z","dependency_job_id":null,"html_url":"https://github.com/roccomuso/mqtt-connect","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roccomuso%2Fmqtt-connect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roccomuso%2Fmqtt-connect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roccomuso%2Fmqtt-connect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roccomuso%2Fmqtt-connect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/roccomuso","download_url":"https://codeload.github.com/roccomuso/mqtt-connect/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250313255,"owners_count":21410190,"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":["broker","connect","framework","iot","middlewares","mqtt","mqttjs","nodejs","plugins"],"created_at":"2024-10-02T08:05:12.218Z","updated_at":"2025-04-22T19:49:39.751Z","avatar_url":"https://github.com/roccomuso.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mqtt-connect [![NPM Version](https://img.shields.io/npm/v/mqtt-connect.svg)](https://www.npmjs.com/package/mqtt-connect) [![Build Status](https://travis-ci.org/roccomuso/mqtt-connect.svg?branch=master)](https://travis-ci.org/roccomuso/mqtt-connect) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\nMQTT high level framework to glue together various \"middleware\" to handle incoming messages.\n\n**It works for both MQTT Broker and Client.**\n\nHeavily inspired by the HTTP [connect](https://github.com/senchalabs/connect) module form-factor.\n\n## Broker Example\n\n```javascript\nvar mosca = require('mosca')\nvar MQTTConnect = require('mqtt-connect')\n\nvar settings = {\n  port: 1883,\n  backend: {\n    type: 'mongo',\n    url: 'mongodb://localhost:27017/mqtt',\n    pubsubCollection: 'ascoltatori',\n    mongo: {}\n  }\n}\n\nvar server = new mosca.Server(settings);\nvar app = new MQTTConnect.broker(server)\n\nserver.on('clientConnected', function(client) {\n    console.log('client connected', client.id)\n})\n\napp.use('/topic', function(broker, client, packet, next){\n  // middleware\n  console.log(packet.payload)\n  next()\n})\n\n// fired when a message is received\nserver.on('published', app.stack) // fn(packet, client)\n\n```\n\n## Client Example\n\n```javascript\nvar mqtt = require('mqtt')\nvar MQTTConnect = require('mqtt-connect')\n\nvar client  = mqtt.connect('mqtt://test.mosquitto.org')\n\nclient.on('connect', function () {\n  client.subscribe('presence')\n  client.publish('presence', 'Hello mqtt')\n})\n\nvar app = new MQTTConnect.Client(client)\n\napp.use(function middleware1(client, msg, next) {\n  // middleware 1\n  next()\n})\n\napp.use(function (client, msg, next) {\n  // middleware 2\n  console.log(msg.topic)\n  console.log(msg.data) // buffer\n  client.publish('boop/', 'boop')\n})\n\nclient.on('message', app.stack) // fn(topic, msg)\n\n```\n\n\n### Mount middleware\n\nThe `.use()` method also takes an optional path string that is matched against the beginning of the incoming request topic. This allows for basic msg routing:\n\n```javascript\napp.use('/topic1', function fooMiddleware(client, msg, next) {\n  // your middleware logic\n  next()\n})\n```\n\n## API\n\n### app.use(fn)\n\nUse a function on the app, where the function represents a middleware. The function\nwill be invoked for every request in the order that `app.use` is called. The function\nis called with three arguments for the client and 4 arguments for the Broker:\n\n```js\n// Client\napp.use(function (client, msg, next) {\n  // client is an MQTT client instance\n  // msg is an object with 2 property: topic \u003cString\u003e, data \u003cBuffer\u003e\n  // next is a function to call to invoke the next middleware\n})\n\n// Broker\napp.use(function(broker, client, packet, next){\n  // broker is a reference to our broker.\n  // client is the sender mqtt client.\n  // packet is the incoming packet, containes: .topic, .payload etc.\n  // next is a function to call to invoke the next middleware\n})\n```\n\n### app.use(topic, fn)\n\nUse a function on the app, where the function represents a middleware. The function\nwill be invoked for every packet received in which the TOPIC match with\nthe given `topic` string in the order that `app.use` is called.\n\n```js\napp.use('/foo', function (client, msg, next) {\n  // client is an MQTT client instance\n  // msg is an object with 2 property: topic \u003cString\u003e, data \u003cBuffer\u003e\n  // next is a function to call to invoke the next middleware\n})\n\napp.use('/bar/+', brokerCb)\n```\n\nThe `topic` could be terminated with a path separator (`/`) or an MQTT wildcard character (`+` or `#`).\nThis means the given topic `/foo/` and `/foo` are NOT the same and both will not match the same messages.\n\nMoreover the `topic` is matched in a case-sensitive manor!\n\nCheck the [mqtt-match](https://github.com/ralphtheninja/mqtt-match) module or the MQTT protocol documentation to better understand topic matching.\n\n### app.stack\n\nExpose the function that will iterate through the added middlewares instances.\n\n- For the `Client` could be called with 2 params (`topic`,`msg`).\n- For the `Broker` could be called with 2 params (`packet`,`client`).\n\nCommon usage:\n\n```js\n// Client\nclient.on('message', app.stack) // fn(topic, msg)\n\n// Broker\nserver.on('published', app.stack) // fn(packet, client)\n```\n\nIt's built to be compatible with the [MQTT.js](https://github.com/mqttjs/MQTT.js) Module and the [Mosca](https://github.com/mcollina/mosca) Broker. But it could also be used together with an `EventEmitter` class that emits messages following the same function signature.\n\n### app.getCount()\n\nReturns the number of middlewares currently inside our `MQTTConnect` `Client` or `Broker` instance.\n\n### app.reset()\n\nRemoves all the middlewares from our `app`.\n\n## Author\n\nRocco Musolino ([@roccomuso](https://twitter.com/roccomuso))\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froccomuso%2Fmqtt-connect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Froccomuso%2Fmqtt-connect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froccomuso%2Fmqtt-connect/lists"}