npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mqtt-request

v1.0.0

Published

make mqtt to be like http

Downloads

1

Readme

mqtt-request

Latest Version: 1.0.0

mqtt-request is a HTTP like library, you can use it request anything like http.

Install

npm install mqtt mqtt-request

you need install mqtt

Usage

It is easy to use. There is only two instance methods request() and response()

response() is for server and request() is for client.

Example

const mqtt = require('mqtt')
const MqttRequest = require('mqtt-request').default

const client = new mqtt("mqtt://mqtt.example.com")
const r = new MqttRequest(client);

// for server
r.response("hello/world", (payload) => {
  return "hello " + payload.toString();
})

mqtt.on('connect', () => {
  // for client
  r.request("hello/world", 
  (payload) => {
    console.log("Get response:" + payload.toString());
  }, 
  "Robot" // <-- this is payload
  )
})

How to work

When client send request, the request() method will add extra message to raw topic

e.g.

Raw topic hello/world

Transform to hello/world/@request/{uuid} the {uudi} is generate with nanoid and @request is sign it is a request messsage.

After send the request message, the request will be push to request queue and add expires property to this request. When expires is larger than now the request will be remove from request queue and THIS request is unusable.

Server get the request and generate response topic hello/world/@response/{uuid}, then send the topic to client with payload or nothing.

Methods

MqttRequest(mqtt)

mqtt the mqtt instance

MqttRequest#request(topic, callback, payload)

topic [string] request topic, you can use anyhing word expect @request or @response

callback [Function] callback function, function callback(payload) the payload is raw mqtt payload

payload [any] anything you want to send server

MqttRequest#response(topic, callback)

topic [string] response topic

callback [Function] callback function, function callback(payload) the payload is sent to server, and if you want send payload to client, then you must return data at callback method, the data can be anything.

Example

r.response("hello/world", (payload) => {return payload.toString()})

if you return nothing, there will no payload send to client, but the client always received the response!