Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@eduardorothdev/fastify-rxjs-mqtt

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@eduardorothdev/fastify-rxjs-mqtt

Fastify wrapper for rxjs-mqtt

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
0
Weekly downloads
 
Created
Source

fastify-rxjs-mqtt

Wrapper for rxjs-mqtt with TS support.

Install

npm i @eduardorothdev/fastify-rxjs-mqtt

Using it

Add it to your fastify project with the register method.

import fastify from "fastify";
import { fastifyRxjsMqtt } from "@eduardorothdev/fastify-rxjs-mqtt";

const host = process.env.HOST ?? "localhost";
const port = process.env.PORT ? Number(process.env.PORT) : 3000;

const server = fastify();

// register plugin
server.register(fastifyRxjsMqtt, { url: "mqtt://localhost:1883" });

server.listen({ port, host }, (err) => {
  if (err) throw err;
});

Then you can use it in your methods

import { FastifyInstance } from "fastify";

export default async function (fastify: FastifyInstance) {
  fastify.get("/mqtt/ping", async function (req, reply) {
    await this.mqttClient.publish("your/topic/#", "Async Hi Mosquitto!");
    reply.send({ mqtt: "message sent!" });
  });
}

Listening for events

import { FastifyInstance } from "fastify";

export default async function (fastify: FastifyInstance) {
  try {
    // subscribe to a topic
    await fastify.mqttClient.subscribe("some/topic/#");
    const sub = fastify.mqttClient
      .onJsonMessage<{
        some: string;
        property: string;
        mapping: boolean;
      }>()
      .pipe(
        catchError((err) => {
          // we have to catch the error so the
          // observable pipe doesn't stop sending messages
          return of(null);
        }),
      )
      .subscribe((jsonMessage) => {
        // { some: 'hello', property: 'from mqtt', mapping: true }
        console.log(jsonMessage);
      });

    // later you can unsubscribe when needed.
    // this will unsubscribe from the onJsonMessage observable pipe
    // not from the topic subscription
    // sub.unsubscribe();

    // Unsubscribe from the topic subscription
    // await fastify.mqttClient.unsubscribe('some/topic/#');
  } catch (err) {
    // connection/subscription-to-topic errors
    console.log(err);
  }
}

License

Licensed under MIT.

Keywords

FAQs

Package last updated on 16 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc