{"_id":"@eduardorothdev/fastify-rxjs-mqtt","name":"@eduardorothdev/fastify-rxjs-mqtt","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@eduardorothdev/fastify-rxjs-mqtt","version":"1.0.0","description":"Fastify wrapper for rxjs-mqtt","main":"dist/index.js","types":"dist/index.d.ts","scripts":{"build":"tsc -p tsconfig.json"},"repository":{"type":"git","url":"git+https://github.com/eduardoRoth/fastify-rxjs-mqtt.git"},"keywords":["mqtt","fastify","async","async-mqtt"],"author":{"name":"Eduardo Roth"},"license":"MIT","bugs":{"url":"https://github.com/eduardoRoth/fastify-rxjs-mqtt/issues"},"homepage":"https://github.com/eduardoRoth/fastify-rxjs-mqtt#readme","devDependencies":{"@types/node":"^22.5.4","typescript":"^5.6.2"},"dependencies":{"@eduardorothdev/rxjs-mqtt":"^1.0.6","fastify":"^4.28.1","fastify-plugin":"^4.5.1"},"_id":"@eduardorothdev/fastify-rxjs-mqtt@1.0.0","gitHead":"31aac4263b3f2a9a3c6cabd5a3fd49226d7f0865","_nodeVersion":"22.1.0","_npmVersion":"10.8.3","dist":{"integrity":"sha512-7C5GOt1aYGnxqJY83TuJqi+wGd2JTR41OrbdBHfb2TPf/O/CTtcOVOXAwEG8Z2aigabR1HLdmXSj9nA7Ycy6+Q==","shasum":"c9e6363bb9e49a96b567bba2e4bac9815d24b044","tarball":"https://registry.npmjs.org/@eduardorothdev/fastify-rxjs-mqtt/-/fastify-rxjs-mqtt-1.0.0.tgz","fileCount":8,"unpackedSize":9304,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC4LK/jImx4I4Ina/xACZkeNIZ0xXiMi55GodD43nVWsgIgIm4e7I8ek/7IRHguPNcodod8WrE95PCUEd2emekKD6g="}]},"_npmUser":{"name":"eduardoroth","email":"earoth88@gmail.com"},"directories":{},"maintainers":[{"name":"eduardoroth","email":"earoth88@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/fastify-rxjs-mqtt_1.0.0_1726525491263_0.3712195794480484"},"_hasShrinkwrap":false}},"time":{"created":"2024-09-16T22:24:51.117Z","1.0.0":"2024-09-16T22:24:51.449Z","modified":"2024-09-16T22:24:51.776Z"},"maintainers":[{"name":"eduardoroth","email":"earoth88@gmail.com"}],"description":"Fastify wrapper for rxjs-mqtt","homepage":"https://github.com/eduardoRoth/fastify-rxjs-mqtt#readme","keywords":["mqtt","fastify","async","async-mqtt"],"repository":{"type":"git","url":"git+https://github.com/eduardoRoth/fastify-rxjs-mqtt.git"},"author":{"name":"Eduardo Roth"},"bugs":{"url":"https://github.com/eduardoRoth/fastify-rxjs-mqtt/issues"},"license":"MIT","readme":"# fastify-rxjs-mqtt\n\nWrapper for [rxjs-mqtt](https://www.npmjs.com/package/@eduardorothdev/rxjs-mqtt) with TS support.\n\n## Install\n\n```\nnpm i @eduardorothdev/fastify-rxjs-mqtt\n```\n\n## Using it\n\nAdd it to your fastify project with the `register` method.\n\n```ts\nimport fastify from \"fastify\";\nimport { fastifyRxjsMqtt } from \"@eduardorothdev/fastify-rxjs-mqtt\";\n\nconst host = process.env.HOST ?? \"localhost\";\nconst port = process.env.PORT ? Number(process.env.PORT) : 3000;\n\nconst server = fastify();\n\n// register plugin\nserver.register(fastifyRxjsMqtt, { url: \"mqtt://localhost:1883\" });\n\nserver.listen({ port, host }, (err) => {\n  if (err) throw err;\n});\n```\n\nThen you can use it in your methods\n\n```ts\nimport { FastifyInstance } from \"fastify\";\n\nexport default async function (fastify: FastifyInstance) {\n  fastify.get(\"/mqtt/ping\", async function (req, reply) {\n    await this.mqttClient.publish(\"your/topic/#\", \"Async Hi Mosquitto!\");\n    reply.send({ mqtt: \"message sent!\" });\n  });\n}\n```\n\nListening for events\n\n```ts\nimport { FastifyInstance } from \"fastify\";\n\nexport default async function (fastify: FastifyInstance) {\n  try {\n    // subscribe to a topic\n    await fastify.mqttClient.subscribe(\"some/topic/#\");\n    const sub = fastify.mqttClient\n      .onJsonMessage<{\n        some: string;\n        property: string;\n        mapping: boolean;\n      }>()\n      .pipe(\n        catchError((err) => {\n          // we have to catch the error so the\n          // observable pipe doesn't stop sending messages\n          return of(null);\n        }),\n      )\n      .subscribe((jsonMessage) => {\n        // { some: 'hello', property: 'from mqtt', mapping: true }\n        console.log(jsonMessage);\n      });\n\n    // later you can unsubscribe when needed.\n    // this will unsubscribe from the onJsonMessage observable pipe\n    // not from the topic subscription\n    // sub.unsubscribe();\n\n    // Unsubscribe from the topic subscription\n    // await fastify.mqttClient.unsubscribe('some/topic/#');\n  } catch (err) {\n    // connection/subscription-to-topic errors\n    console.log(err);\n  }\n}\n```\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","readmeFilename":"README.md"}