{"id":15612485,"url":"https://github.com/euberdeveloper/mqtt-now","last_synced_at":"2025-04-28T13:05:34.915Z","repository":{"id":57303723,"uuid":"193378880","full_name":"euberdeveloper/mqtt-now","owner":"euberdeveloper","description":"A node.js module which provides you a publisher and a subscriber, allowing you to easily run repetitive tasks. With Typescript support.","archived":false,"fork":false,"pushed_at":"2023-03-14T15:48:13.000Z","size":443,"stargazers_count":3,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-28T13:04:27.456Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/euberdeveloper.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-23T17:51:38.000Z","updated_at":"2021-04-16T17:10:14.000Z","dependencies_parsed_at":"2024-10-22T18:04:30.543Z","dependency_job_id":null,"html_url":"https://github.com/euberdeveloper/mqtt-now","commit_stats":{"total_commits":23,"total_committers":4,"mean_commits":5.75,"dds":"0.26086956521739135","last_synced_commit":"21611a4c0091ae0ee8d0fc16c201650d52e67cfa"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euberdeveloper%2Fmqtt-now","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euberdeveloper%2Fmqtt-now/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euberdeveloper%2Fmqtt-now/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euberdeveloper%2Fmqtt-now/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/euberdeveloper","download_url":"https://codeload.github.com/euberdeveloper/mqtt-now/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251319611,"owners_count":21570426,"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":[],"created_at":"2024-10-03T06:43:12.589Z","updated_at":"2025-04-28T13:05:34.859Z","avatar_url":"https://github.com/euberdeveloper.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![GitHub license](https://img.shields.io/github/license/euberdeveloper/mqtt-now.svg?style=for-the-badge)](https://github.com/euberdeveloper/mqtt-now/blob/master/LICENSE)\n[![GitHub issues](https://img.shields.io/github/issues/euberdeveloper/mqtt-now.svg?style=for-the-badge)](https://github.com/euberdeveloper/mqtt-now/issues)\n![npm](https://img.shields.io/npm/v/mqtt-now.svg?style=for-the-badge)\n\n# mqtt-now\n\nA node.js module which provides you a publisher and a subscriber for the mqtt protocol, allowing you to easily run repetitive tasks. With Typescript support.\n\n## Install\n\nTo install mqtt-now:\n\n```bash\n$ npm install mqtt-now\n```\n\n## Usage\n\n### Publish data\n\nSimple:\n\n```js\nconst mqttNow = require('mqtt-now');\n\nconst options = {\n  host: 'localhost',\n  interval: 1000,\n  actions: [\n    {\n      topic: 'public',\n      message: 'my message'\n    },\n    {\n      topic: 'random',\n      message: () =\u003e ( 'random ' + Math.random() )\n    }\n  ]\n}\n\nmqttNow.publish(options);\n```\n\nWith Buffer:\n\n```js\nconst mqttNow = require('mqtt-now');\n\nconst options = {\n  host: 'localhost',\n  interval: 1000,\n  actions: [\n    {\n      topic: 'public',\n      message: Buffer.alloc(10, 'my message')\n    }\n  ]\n}\n\nmqttNow.publish(options);\n```\n\nWith Web Sockets:\n\n```js\nconst { publish, Protocol } = require('mqtt-now');\n\nconst options = {\n  type: Protocol.WS,\n  host: 'localhost',\n  interval: 1000,\n  actions: [{ topic: 'public', message: 'my message' }]\n}\n\npublish(options);\n```\n\nWith further configurations:\n\n```js\nconst mqttNow = require('mqtt-now');\n\nconst options = {\n  url: 'mqtt://localhost:3000',\n  actions: [\n    {\n      topic: 'public',\n      message: 'my message',\n      onError: error =\u003e console.error('Error in public')\n    },\n    {\n      topic: 'random',\n      message: () =\u003e ( 'random ' + Math.random() ),\n      interval: 100\n    }\n  ],\n  interval: 1000,\n  onError: error =\u003e console.error('Error in publishing', error)\n}\n\nmqttNow.publish(options);\n```\n\n### Subscribe and handle\n\nSimple:\n\n```js\nconst mqttNow = require('mqtt-now');\n\nconst options = {\n  host: 'localhost',\n  actions: [{ topic: 'public' }, { topic: 'random' }],\n  onMessage: message =\u003e console.log('Message received ', message),\n  messageType: mqttNow.MessageType.STRING\n}\n\nmqttNow.subscribe(options);\n```\n\nWith Web Sockets:\n\n```js\nconst { subscribe, Protocol, MessageType } = require('mqtt-now');\n\nconst options = {\n  type: Protocol.WS,\n  host: 'localhost',\n  actions: [{ topic: 'public' }, { topic: 'random' }],\n  onMessage: message =\u003e console.log('Message received ', message),\n  messageType: MessageType.STRING\n}\n\nsubscribe(options);\n```\n\nWith further configurations:\n\n```js\nconst mqttNow = require('mqtt-now');\n\nconst options = {\n  protocol: 'mqtt',\n  port: '1883',\n  host: 'localhost',\n  actions: [\n    {\n      topic: 'public',\n      onError: error =\u003e console.error('Error in subscribing to \"public\" ', error)\n    },\n    {\n      topic: 'random',\n      onMessage: message =\u003e console.log('Random message received ', message)\n    },\n    {\n      topic: 'buffer',\n      messageType: mqttNow.MessageType.BUFFER,\n      onMessage: message =\u003e console.log('This remains a buffer ', message)\n    }\n  ],\n  messageType: mqttNow.MessageType.STRING,\n  onMessage: message =\u003e console.log('Message received ', message),\n  onError: error =\u003e console.error('Error in subscribing ', error)\n}\n\nmqttNow.subscribe(options);\n```\n\n### Get a Publisher\n\nA Publisher is a function with two optional arguments: an array of topics and generic data.\nWhen called, all the specified topics will be published as specified in the options and, if topic's message is generated by a function, the generic data is passed to that function.\n\n```js\nconst mqttNow = require('mqtt-now');\n\nconst options = {\n  host: 'localhost',\n  actions: [\n    {\n      topic: 'public',\n      message: 'my message'\n    },\n    {\n      topic: 'random',\n      message: () =\u003e ( 'random ' + Math.random() )\n    },\n    {\n      topic: 'now',\n      message: data =\u003e ( data + 'ms are passed from 01/01/1970' )\n    }\n  ]\n}\n\nmqttNow.getPublisher(options)\n  .then(publisher =\u003e {\n    publisher('public');\n    publisher(['public', 'random']);\n    publisher('', Date.now());\n  });\n```\n\n## Result\n\nGiven this code:\n\n```js\nconst mqttNow = require('mqtt-now');\n\nconst publishOptions = {\n  host: 'localhost',\n  actions: [\n    {\n      topic: 'public',\n      message: 'my message',\n      interval: 500\n    },\n    {\n      topic: 'random',\n      message: () =\u003e ( 'random ' + Math.random() )\n    }\n  ],\n  interval: 1000\n}\n\nconst subscribeOptions = {\n  host: 'localhost',\n  actions: [\n    { topic: 'public' },\n    {\n      topic: 'random',\n      onMessage: message =\u003e console.log('Random message received ', message)\n    }\n  ],\n  messageType: mqttNow.MessageType.STRING,\n  onMessage: message =\u003e console.log('Message received ', message)\n}\n\nmqttNow.publish(publishOptions);\nmqttNow.subscribe(subscribeOptions);\n```\n\nSupposing that there is a mqtt instance running in localhost,\n\nThe console will result in this:\n\n```\nMessage received  my message\nRandom message received  random 0.48166328598140296\nMessage received  my message\nMessage received  my message\nRandom message received  random 0.8821357744971647\nMessage received  my message\nMessage received  my message\nRandom message received  random 0.8432069519144318\nMessage received  my message\nMessage received  my message\nRandom message received  random 0.13065012342886484\n..................................................\n```\n\n## API\n\n### publish\n\n**Syntax:**\n\n`mqttNow.publish(options)`\n\n**Description:**\n\nThis function publishes every a certain amount of time the data to the topics specified by the options. See __Usage__ to have an example.\n\n**Parameters:**\n\n* __options__: The options which specifies which topics and which data have to be sent and which url these things will be sent to.\n\n**Options parameters:**\n\n* __type__: Default value: `Protocol.MQTT`. If not overwritten, the protocol and the port of the url will be determined by this property.\n* __protocol__: Default value: `undefined`. Specifies the protocol to use in the url string. Note: overwrites the protocol specified by type property but is overwritten by url property.\n* __host__: Default value: `localhost`. Specifies the host to use in the url string. Note: overwritten by url property.\n* __port__: Default value: `undefined`. Specifies the port to use in the url string. Note: overwrites the port specified by type property but is overwritten by url property.\n* __url__: Default value: `undefined`. Specifies the url used by the connection. Note: overwrites all the other properties that specify the url.\n* __onError__: Default value: `error =\u003e {}`. Specifies what will be executed in case of an error. Note: overwritten by the more-specific onError option whether specified in a certain topic.\n* __actions__: Default value: `[]`. The actions (topic and message) that you want to publish. See __MqttPublishAction__ to further information.\n* __interval__: Default value: `1`. The time in milliseconds between every publication of a topic. Note: overwritten by the more-specific interval option whether specified in a certain topic.\n\n**MqttPublishAction**\n\n* __topic__: The topic that will be published.\n* __message__: The message that will be published, as a string or a Buffer.\n* __onError__: Optional. What will be executed in case of a publishing error. Note: overwrites the onError callback specified by the options.\n* __interval__: Optional. The time in milliseconds between every publication of this topic. Note: overwrites the interval property specified by the options.\n\n### subscribe\n\n**Syntax:**\n\n`mqttNow.subscribe(options)`\n\n**Description:**\n\nThis function subscribes to the topics specified by the options and handle the received messages in the way specified by the options. See __Usage__ to have an example.\n\n**Parameters:**\n\n* __options__: The options which specifies which topics are to be subscribed and which message handlers are to be executed and which url is to be listened.\n\n**Options parameters:**\n\n* __type__: Default value: `Protocol.MQTT`. If not overwritten, the protocol and the port of the url will be determined by this property.\n* __protocol__: Default value: `undefined`. Specifies the protocol to use in the url string. Note: overwrites the protocol specified by type property but is overwritten by url property.\n* __host__: Default value: `localhost`. Specifies the host to use in the url string. Note: overwritten by url property.\n* __port__: Default value: `undefined`. Specifies the port to use in the url string. Note: overwrites the port specified by type property but is overwritten by url property.\n* __url__: Default value: `undefined`. Specifies the url used by the connection. Note: overwrites all the other properties that specify the url.\n* __onError__: Default value: `error =\u003e {}`. Specifies what will be executed in case of an error. Note: overwritten by the more-specific onError option whether specified in a certain topic.\n* __actions__: Default value: `[]`. The actions that specifies which topics you want to subscribe to and what to do with the received messages. See __MqttSubscribeAction__ to further information.\n* __onMessage__: Default value: `message =\u003e {}`. What will be done with the received messages. Note: overwritten by the more-specific onMessage option whether specified in a certain topic.\n* __messageType__: Default value: `MessageType.BUFFER`. The type of the message given as argument of onMessage callback. Note: overwritten by the more-specific messageType option whether specified in a certain topic.\n\n**MqttSubscribeAction**\n\n* __topic__: The topic that will be subscribed.\n* __onMessage__: Optional. What will be done with the received message. Note: overwrites the onError callback specified by the options.\n* __messageType__: Default value: `MessageType.BUFFER`. The type of the message given as argument of onMessage callback. Note: overwrites the messageType callback specified by the options.\n* __onError__: Optional. What will be executed in case of a subscribing error. Note: overwrites the onError callback specified by the options.\n\n### getPublisher\n\n**Syntax:**\n\n`const promise = mqttNow.getPublisher(options)`\n\n**Description:**\n\nThis function returns a promise with a Publisher, which is a function that takes a list of topics and in case some data as arguments and when is called publishes everything is specified in the options given to this function. In the returned function: If no topic or an  empty string is passed, it publishes all the topics. Also, in case data is passed as second argument, it will passed as the argument of each function that generates the message for a topic. See __Usage__ to have an example.\n\n**Parameters:**\n\n* __options__: The options which specifies which topics and which data have to be sent and which url these things will be sent to.\n\n**Options parameters:**\n\n* __type__: Default value: `Protocol.MQTT`. If not overwritten, the protocol and the port of the url will be determined by this property.\n* __protocol__: Default value: `undefined`. Specifies the protocol to use in the url string. Note: overwrites the protocol specified by type property but is overwritten by url property.\n* __host__: Default value: `localhost`. Specifies the host to use in the url string. Note: overwritten by url property.\n* __port__: Default value: `undefined`. Specifies the port to use in the url string. Note: overwrites the port specified by type property but is overwritten by url property.\n* __url__: Default value: `undefined`. Specifies the url used by the connection. Note: overwrites all the other properties that specify the url.\n* __onError__: Default value: `error =\u003e {}`. Specifies what will be executed in case of an error. Note: overwritten by the more-specific onError option whether specified in a certain topic.\n* __actions__: Default value: `[]`. The actions (topic and message) that you want to publish. See __MqttPublisherAction__ to further information.\n\n**MqttPublisherAction**\n\n* __topic__: The topic that will be published.\n* __message__: The message that will be published, as a string or a Buffer. You can pass also a function that returns a message. The data passed as the second argument to the Publisher will be passed as argument to that function.\n* __onError__: Optional. What will be executed in case of a publishing error. Note: overwrites the onError callback specified by the options.\n\n## Note\n\nPlease notice that an mqtt instance have to run in your localhost in order to the above examples to work.\n\n## Build\n\nTo build the module make sure you have Typescript installed or install the dev dependencies. After this, run:\n\n```bash\n$ npm run transpile\n```\n\nThe `source` folder will be compiled in the `dist` folder.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feuberdeveloper%2Fmqtt-now","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feuberdeveloper%2Fmqtt-now","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feuberdeveloper%2Fmqtt-now/lists"}