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

pubsubr

v0.1.5

Published

pub and sub to loopback components

Downloads

9

Readme

pubsubr

Build Status Coverage Status

pub and sub to loopback

Installation

$ npm install --save pubsubr

Example Usage

server

const pubsubr = require('pubsubr');
const server = new pubsubr.server('mqtt://test.mosquitto.org', app.models.EventLog);

client

const pubsubr = require('pubsubr');
const client = new pubsubr.client('mqtt://test.mosquitto.org', 'http://0.0.0.0:3000/api/eventLog');

create model

创建EventLog模型,用于客户端重连或重新运行获取丢失的publish

⚠️注意:模型字段会解析topic和字段topicmessagecreatedAtupdatedAt组成对象存进数据库,所以topic配置需和model字段相匹配

loopback

  • 从模块中使用已定义好的模型,配置model-config.json:
{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models",
      "pubsubr/models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "loopback/server/mixins",
      "../common/mixins",
      "./mixins"
    ]
  },
  "EventLog": {
    "dataSource": "db",
    "public": true
  }
}

other

配置好模型,在创建server对象时传入

Topic

tipic的处理和格式,key/value形式,+为级别任意通配符,#为层级任意通配符

  • publish时,会解析topic成为object加上字段topicmessagecreatedAtupdatedAt存入数据库事件日志表,例如:uid/123456789/model/Account/oid/abcdefg/action/create解析为{uid: '123456789', model: 'Account', oid: 'abcdefg', action: 'create'}
  • subscribe时,可通过通配符监听,同样会把topic解析放入payload中传入handler

API

pubsubr

pubsubr.server(options, model)

create pubsubr server

  • options connect to mosquitto ro mosca, String: mqtt://test.mosquitto.org , Object: {host: 'localhost', port: '1883'}
  • model - EventLog,用于客户端重连或重新运行获取丢失的publish,会使用模型的create创建一条数据,为null不创建数据

pubsubr.client(options, api)

create pubsubr client

  • options connect to mosquitto ro mosca, String: mqtt://test.mosquitto.org , Object:
    • host default localhost
    • port default 1883
  • api 获取事件日志数据的api

server

server.publish(topic, message, [options], [callback])

Publish a message to a topic

  • topic is the topic to publish to, String
  • message is the message to publish,Object or String
  • options is the options to publish with, including:
    • qos QoS level, Number, default 0
    • retain retain flag, Boolean, default false
    • dup mark as duplicate flag, Boolean, default false
  • callback - function (err), fired when the QoS handling completes, or at the next tick if QoS 0. An error occurs if client is disconnecting.

client

client.subscribe(topic, [options], handler)

Subscribe to a topic or topics

  • topic is a String topic of subscribe to topics to subscribe to. It can also be an object, it has as object keys the topic name and as value the QoS, like {'test1': 0, 'test2': 1}. MQTT topic wildcard characters are supported (+ - for single level and # - for multi level)
  • options is the options to subscribe with, including:
    • qos qos subscription level, default 0
    • time 最后一次收到事件的时间,用于重新获取漏处理的事件,发生于客户端重起和mqtt服务断线重连的时候,重连暂时只支持loopbackREST API风格
  • callback - function (err, granted) callback fired on suback where:
    • err a subscription error or an error that occurs when client is disconnecting
    • granted is an array of {topic, qos} where:
      • topic is a subscribed to topic

      • qos is the granted qos level on it

unsubscribe(topic, [callback])

Unsubscribe from a topic or topics

  • topic is a String topic or an array of topics to unsubscribe from
  • callback - function (err), fired on unsuback. An error occurs if client is disconnecting.