{"name":"mqtt-packet","version":"4.0.2","keywords":["MQTT","packet","parse","publish","subscribe","pubsub"],"license":"MIT","_id":"mqtt-packet@4.0.2","maintainers":[{"name":"matteo.collina","email":"hello@matteocollina.com"}],"contributors":[{"url":"https://github.com/mcollina","name":"Matteo Collina","email":"matteo.collina@gmail.com"},{"name":"Adam Rudd","email":"damvrr@gmail.com"}],"homepage":"https://github.com/mqttjs/mqtt-packet","bugs":{"url":"https://github.com/mqttjs/mqtt-packet/issues"},"dist":{"shasum":"f323046b161da00b138b67d67df7089ee14be6b8","tarball":"http://registry-npm.ywwl.com/mqtt-packet/download/mqtt-packet-4.0.2.tgz","integrity":"sha512-ogxbmMdCCmxDyW1JrYk9Ne17qsHXfc/A9q7rrXC5JlrpF3ONIX0iCSki04NbqNbRmxOyVNfUhgVlbQ9uekkC3Q==","signatures":[{"sig":"MEUCIBePCj3CxwvqqwlIy/eqiyLy3ESE/UuRL6ZNCRf0tziIAiEAxJNKOSCnH47VKylfw64dpVy4HmobLrYIJ7BsqoE3lRc=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"size":14079,"noattachment":false,"key":"/mqtt-packet/-/mqtt-packet-4.0.2.tgz"},"main":"mqtt.js","_from":".","_shasum":"f323046b161da00b138b67d67df7089ee14be6b8","gitHead":"acb47487c41707420e338ccd269fbe3a36af8486","scripts":{"test":"tape test.js | faucet"},"_npmUser":{"name":"matteo.collina","email":"matteo.collina@gmail.com"},"deprecated":"critical bug fixed in version 4.0.5","pre-commit":"test","repository":{"url":"https://github.com/mqttjs/mqtt-packet.git","type":"git"},"_npmVersion":"1.4.28","description":"Parse and generate MQTT packets like a breeze","directories":{},"dependencies":{"bl":"^1.0.0","inherits":"^2.0.1"},"devDependencies":{"tape":"^4.2.0","faucet":"0.0.1","dev-null":"^0.1.1","pre-commit":"^1.1.1","readable-stream":"^2.0.2"},"readme":"mqtt-packet\n===========\n\nEncode and Decode MQTT 3.1.1, 5.0 packets the node way.\n\n[![JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)\n\n  * <a href=\"#installation\">Installation</a>\n  * <a href=\"#examples\">Examples</a>\n  * <a href=\"#packets\">Packets</a>\n  * <a href=\"#api\">API</a>\n  * <a href=\"#contributing\">Contributing</a>\n  * <a href=\"#license\">License &amp; copyright</a>\n\nThis library is tested with node v6, v8, v10, v12 and v14. The last version to support\nolder versions of node was mqtt-packet@4.1.2.\n\nInstallation\n------------\n\n```bash\nnpm install mqtt-packet --save\n```\n\nExamples\n--------\n\n### Generating\n\n```js\nconst mqtt = require('mqtt-packet');\nconst object = {\n  cmd: 'publish',\n  retain: false,\n  qos: 0,\n  dup: false,\n  length: 10,\n  topic: 'test',\n  payload: 'test' // Can also be a Buffer\n};\nconst opts = { protocolVersion: 4 }; // default is 4. Usually, opts is a connect packet\n\nconsole.log(mqtt.generate(object))\n// Prints:\n//\n// <Buffer 30 0a 00 04 74 65 73 74 74 65 73 74>\n//\n// Which is the same as:\n//\n// Buffer.from([\n//   48, 10, // Header (publish)\n//   0, 4, // Topic length\n//   116, 101, 115, 116, // Topic (test)\n//   116, 101, 115, 116 // Payload (test)\n// ])\n```\n\n### Parsing\n\n```js\nconst mqtt = require('mqtt-packet');\nconst opts = { protocolVersion: 4 }; // default is 4. Usually, opts is a connect packet\nconst parser = mqtt.parser(opts);\n\n// Synchronously emits all the parsed packets\nparser.on('packet', packet => {\n  console.log(packet)\n  // Prints:\n  //\n  // {\n  //   cmd: 'publish',\n  //   retain: false,\n  //   qos: 0,\n  //   dup: false,\n  //   length: 10,\n  //   topic: 'test',\n  //   payload: <Buffer 74 65 73 74>\n  // }\n})\n\nparser.parse(Buffer.from([\n  48, 10, // Header (publish)\n  0, 4, // Topic length\n  116, 101, 115, 116, // Topic (test)\n  116, 101, 115, 116 // Payload (test)\n]))\n// Returns the number of bytes left in the parser\n```\n\nAPI\n---\n\n  * <a href=\"#generate\"><code>mqtt#<b>generate()</b></code></a>\n  * <a href=\"#writeToStream\"><code>mqtt#<b>writeToStream()</b></code></a>\n  * <a href=\"#parser\"><code>mqtt#<b>parser()</b></code></a>\n\n<a name=\"generate\">\n\n### mqtt.generate(object, [opts])\n\nGenerates a `Buffer` containing an MQTT packet.\nThe object must be one of the ones specified by the [packets](#packets)\nsection. Throws an `Error` if a packet cannot be generated.\n\n<a name=\"writeToStream\">\n\n### mqtt.writeToStream(object, stream, [opts])\n\nWrites the mqtt packet defined by `object` to the given stream.\nThe object must be one of the ones specified by the [packets](#packets)\nsection. Emits an `Error` on the stream if a packet cannot be generated.\nOn node >= 0.12, this function automatically calls `cork()` on your stream,\nand then it calls `uncork()` on the next tick.\nBy default cache for number buffers is enabled.\nIt creates a list of buffers for faster write. To disable cache set `mqtt.writeToStream.cacheNumbers = false`.\nShould be set before any `writeToStream` calls.\n\n<a name=\"parser\">\n\n### mqtt.parser([opts])\n\nReturns a new `Parser` object. `Parser` inherits from `EventEmitter` and\nwill emit:\n\n  * `packet`, when a new packet is parsed, according to\n    [packets](#packets)\n  * `error`, if an error happens\n\n<a name=\"parse\">\n\n#### Parser.parse(buffer)\n\nParses a given `Buffer` and emits synchronously all the MQTT packets that\nare included. Returns the number of bytes left to parse.\n\nIf an error happens, an `error` event will be emitted, but no `packet` events\nwill be emitted after that. Calling `parse()` again clears the error and\nprevious buffer, as if you created a new `Parser`.\n\nPackets\n-------\n\nThis section describes the format of all packets emitted by the `Parser`\nand that you can input to `generate`.\n\n### Connect\n\n```js\n{\n  cmd: 'connect',\n  protocolId: 'MQTT', // Or 'MQIsdp' in MQTT 3.1 and 5.0\n  protocolVersion: 4, // Or 3 in MQTT 3.1, or 5 in MQTT 5.0\n  clean: true, // Can also be false\n  clientId: 'my-device',\n  keepalive: 0, // Seconds which can be any positive number, with 0 as the default setting\n  username: 'matteo',\n  password: Buffer.from('collina'), // Passwords are buffers\n  will: {\n    topic: 'mydevice/status',\n    payload: Buffer.from('dead'), // Payloads are buffers\n    properties: { // MQTT 5.0\n      willDelayInterval: 1234,\n      payloadFormatIndicator: false,\n      messageExpiryInterval: 4321,\n      contentType: 'test',\n      responseTopic: 'topic',\n      correlationData: Buffer.from([1, 2, 3, 4]),\n      userProperties: {\n        'test': 'test'\n      }\n    }\n  },\n  properties: { // MQTT 5.0 properties\n      sessionExpiryInterval: 1234,\n      receiveMaximum: 432,\n      maximumPacketSize: 100,\n      topicAliasMaximum: 456,\n      requestResponseInformation: true,\n      requestProblemInformation: true,\n      userProperties: {\n        'test': 'test'\n      },\n      authenticationMethod: 'test',\n      authenticationData: Buffer.from([1, 2, 3, 4])\n  }\n}\n```\n\nIf `protocolVersion` is 3, `clientId` is mandatory and `generate` will throw if\nmissing.\n\nIf `password` or `will.payload` are passed as strings, they will\nautomatically be converted into a `Buffer`.\n\n### Connack\n\n```js\n{\n  cmd: 'connack',\n  returnCode: 0, // Or whatever else you see fit MQTT < 5.0\n  sessionPresent: false, // Can also be true.\n  reasonCode: 0, // reason code MQTT 5.0\n  properties: { // MQTT 5.0 properties\n      sessionExpiryInterval: 1234,\n      receiveMaximum: 432,\n      maximumQoS: 1,\n      retainAvailable: true,\n      maximumPacketSize: 100,\n      assignedClientIdentifier: 'test',\n      topicAliasMaximum: 456,\n      reasonString: 'test',\n      userProperties: {\n        'test': 'test'\n      },\n      wildcardSubscriptionAvailable: true,\n      subscriptionIdentifiersAvailable: true,\n      sharedSubscriptionAvailable: false,\n      serverKeepAlive: 1234,\n      responseInformation: 'test',\n      serverReference: 'test',\n      authenticationMethod: 'test',\n      authenticationData: Buffer.from([1, 2, 3, 4])\n  }\n}\n```\n\nThe only mandatory argument is `returnCode`, as `generate` will throw if\nmissing.\n\n### Subscribe\n\n```js\n{\n  cmd: 'subscribe',\n  messageId: 42,\n  properties: { // MQTT 5.0 properties\n    subscriptionIdentifier: 145,\n    userProperties: {\n      test: 'test'\n    }\n  }\n  subscriptions: [{\n    topic: 'test',\n    qos: 0,\n    nl: false, // no Local MQTT 5.0 flag\n    rap: true, // Retain as Published MQTT 5.0 flag\n    rh: 1 // Retain Handling MQTT 5.0\n  }]\n}\n```\n\nAll properties are mandatory.\n\n### Suback\n\n```js\n{\n  cmd: 'suback',\n  messageId: 42,\n  properties: { // MQTT 5.0 properties\n    reasonString: 'test',\n    userProperties: {\n      'test': 'test'\n    }\n  }\n  granted: [0, 1, 2, 128]\n}\n```\n\nAll the granted qos __must__ be < 256, as they are encoded as UInt8.\nAll properties are mandatory.\n\n### Unsubscribe\n\n```js\n{\n  cmd: 'unsubscribe',\n  messageId: 42,\n  properties: { // MQTT 5.0 properties\n    userProperties: {\n      'test': 'test'\n    }\n  }\n  unsubscriptions: [\n    'test',\n    'a/topic'\n  ]\n}\n```\n\nAll properties are mandatory.\n\n### Unsuback\n\n```js\n{\n  cmd: 'unsuback',\n  messageId: 42,\n  properties: { // MQTT 5.0 properties\n    reasonString: 'test',\n    userProperties: {\n      'test': 'test'\n    }\n  }\n}\n```\n\nAll properties are mandatory.\n\n### Publish\n\n```js\n{\n  cmd: 'publish',\n  messageId: 42,\n  qos: 2,\n  dup: false,\n  topic: 'test',\n  payload: Buffer.from('test'),\n  retain: false,\n  properties: { // optional properties MQTT 5.0\n      payloadFormatIndicator: true,\n      messageExpiryInterval: 4321,\n      topicAlias: 100,\n      responseTopic: 'topic',\n      correlationData: Buffer.from([1, 2, 3, 4]),\n      userProperties: {\n        'test': 'test'\n      },\n      subscriptionIdentifier: 120, // can be an Array in message from broker, if message included in few another subscriptions\n      contentType: 'test'\n   }\n}\n```\n\nOnly the `topic` property is mandatory.\nBoth `topic` and `payload` can be `Buffer` objects instead of strings.\n`messageId` is mandatory for `qos > 0`.\n\n### Puback\n\n```js\n{\n  cmd: 'puback',\n  messageId: 42,\n  reasonCode: 16, // only for MQTT 5.0\n  properties: { // MQTT 5.0 properties\n      reasonString: 'test',\n      userProperties: {\n        'test': 'test'\n      }\n  }\n}\n```\n\nThe only mandatory property is `messageId`, as `generate` will throw if\nmissing.\n\n### Pubrec\n\n```js\n{\n  cmd: 'pubrec',\n  messageId: 42,\n  reasonCode: 16, // only for MQTT 5.0\n  properties: { // properties MQTT 5.0\n    reasonString: 'test',\n    userProperties: {\n      'test': 'test'\n    }\n  }\n}\n```\n\nThe only mandatory property is `messageId`, as `generate` will throw if\nmissing.\n\n### Pubrel\n\n```js\n{\n  cmd: 'pubrel',\n  messageId: 42,\n  reasonCode: 16, // only for MQTT 5.0\n  properties: { // properties MQTT 5.0\n     reasonString: 'test',\n     userProperties: {\n       'test': 'test'\n     }\n  }\n}\n```\n\nThe only mandatory property is `messageId`, as `generate` will throw if\nmissing.\n\n### Pubcomp\n\n```js\n{\n  cmd: 'pubcomp',\n  messageId: 42,\n  reasonCode: 16, // only for MQTT 5.0\n  properties: { // properties MQTT 5.0\n    reasonString: 'test',\n    userProperties: {\n       'test': 'test'\n    }\n  }\n}\n```\n\nThe only mandatory property is `messageId`, as `generate` will throw if\nmissing.\n\n### Pingreq\n\n```js\n{\n  cmd: 'pingreq'\n}\n```\n\n### Pingresp\n\n```js\n{\n  cmd: 'pingresp'\n}\n```\n\n### Disconnect\n\n```js\n{\n  cmd: 'disconnect',\n  reasonCode: 0, // MQTT 5.0 code\n  properties: { // properties MQTT 5.0\n     sessionExpiryInterval: 145,\n     reasonString: 'test',\n     userProperties: {\n       'test': 'test'\n     },\n     serverReference: 'test'\n  }\n}\n```\n\n### Auth\n\n```js\n{\n  cmd: 'auth',\n  reasonCode: 0, // MQTT 5.0 code\n  properties: { // properties MQTT 5.0\n     authenticationMethod: 'test',\n     authenticationData: Buffer.from([0, 1, 2, 3]),\n     reasonString: 'test',\n     userProperties: {\n       'test': 'test'\n     }\n  }\n}\n```\n\n<a name=\"contributing\"></a>\n\nContributing\n------------\n\nmqtt-packet is an **OPEN Open Source Project**. This means that:\n\n> Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.\n\nSee the [CONTRIBUTING.md](https://github.com/mqttjs/mqtt-packet/blob/master/CONTRIBUTING.md) file for more details.\n\n### Contributors\n\nmqtt-packet is only possible due to the excellent work of the following contributors:\n\n<table><tbody>\n<tr><th align=\"left\">Matteo Collina</th><td><a href=\"https://github.com/mcollina\">GitHub/mcollina</a></td><td><a href=\"http://twitter.com/matteocollina\">Twitter/@matteocollina</a></td></tr>\n<tr><th align=\"left\">Adam Rudd</th><td><a href=\"https://github.com/adamvr\">GitHub/adamvr</a></td><td><a href=\"http://twitter.com/adam_vr\">Twitter/@adam_vr</a></td></tr>\n<tr><th align=\"left\">Peter Sorowka</th><td><a href=\"https://github.com/psorowka\">GitHub/psorowka</a></td><td><a href=\"http://twitter.com/psorowka\">Twitter/@psorowka</a></td></tr>\n<tr><th align=\"left\">Siarhei Buntsevich</th><td><a href=\"https://github.com/scarry1992\">GitHub/scarry1992</a></td></tr>\n</tbody></table>\n\nLicense\n-------\n\nMIT\n","publish_time":1445500973461,"_cnpm_publish_time":1445500973461,"dist-tags":{"latest":"9.0.2","legacy-v4":"4.1.3","next":"8.0.0","v3-stable":"3.5.1"}}