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

simpleiot-js

v1.1.0

Published

SimpleIOT JavaScript API using NATS / WebSockets

Downloads

9

Readme

simpleiot-js

SimpleIoT JavaScript API using NATS / WebSockets

This package allows JavaScript clients (especially web browsers) to connect to SimpleIoT using nats.ws.

Install

npm i simpleiot-js

Usage

import { connect } from "simpleiot-js"
;(async function siotConnect() {
  try {
    // Note: nats.ws has built-in reconnection logic by default
    const nc = await connect({
      servers: "localhost:9222",
      // Pass in options as documented in nats.ws package
    })
    // `getServer()` is a method documented by nats.ws
    console.log(`connected to ${nc.getServer()}`)
    // `closed()` is a nats.ws method that returns a promise
    // indicating the client closed
    const done = nc.closed()

    // Example: get root nodes from SimpleIoT tree
    const n = await nc.getNodeChildren("root")

    // close the connection
    await nc.close()
    // check if the close was OK
    const err = await done
    if (err) {
      console.log(`error closing:`, err)
    }
  } catch (err) {
    console.error("connection error:", err)
  }
})()

API

The SimpleIoT package is simply a wrapper of the nats.ws package. Any API documented in the nats.ws package will work. We have also added the following functions specific to SimpleIoT.

  • getNode(id, { parent, type, includeDel, opts } = {})

    getNode sends a request to nodes.<parent>.<id> to retrieve an array of NodeEdges for the specified Node ID.

    • If id is "all" or falsy, this calls getNodeChildren instead (see below); however, we strongly recommend using getNodeChildren directly
    • If parent is "all" or falsy, all instances of the specified node are returned
    • If parent is "root", only root nodes are returned
    • opts are options passed to the NATS request

    The returned node contains the following properties:

    • id - the node ID
    • type - the node type
    • hash
    • parent - the parent ID for this node edge
    • pointsList - the list of points for this node
    • edgepointsList - the list of edge points for the edge between this node and the specified parent

    Each point contains the following properties:

    • time - timestamp of the point converted to a JavaScript Date object
    • type
    • key
    • value - numeric value of the point
    • text - text value of the point
    • data - data contained within the point (encoded as base64 string)
    • tombstone - if tombstone value is even, the point is active; otherwise, if it is odd, the point is considered deleted
    • origin - the node ID of the user or other node that created this point.
  • getNodeChildren(parentID, { type, includeDel, recursive, opts } = {} )

    getNodeChildren sends a request to nodes.<parentID>.<id> to retrieve an array of child NodeEdges of the specified parent node.

    • If parentID is "root", all root nodes are retrieved

    • type - can be used to filter nodes of a specified type (defaults to "")

    • includeDel - set to true to include deleted nodes (defaults to false)

    • recursive - set to true to recursively retrieve all descendants matching the criteria. In this case, each returned NodeEdge will contain a children property, which is an array of that Node's descendant NodeEdges. Set to "flat" to return a single flattened array of NodeEdges.

      Note: If type is also set when recursive is truthy, type restricts which nodes are recursively searched. If you need to search descendants that do not match the type, consider removing the type filter and filter manually.

    • opts are options passed to the NATS request

  • getNodesForUser(userID, { type, includeDel, recursive, opts } = {})

    getNodesForUser returns the parent nodes for the given userID along with their descendants if recursive is truthy.

    • type - can be used to filter nodes of a specified type (defaults to "")
    • includeDel - set to true to include deleted nodes (defaults to false)
    • recursive - set to true to recursively retrieve all descendants matching the criteria. In this case, each returned NodeEdge will contain a children property, which is an array of that Node's descendant NodeEdges. Set to "flat" to return a single flattened array of NodeEdges.
    • opts are options passed to the NATS request
  • subscribePoints(nodeID)

    Subscribes to p.<nodeID> and returns an async iterable for an array of Point objects. nodeID can be * or all.

  • subscribeEdgePoints(nodeID)

    Subscribes to p.<nodeID>.<parentID> and returns an async iterable for an array of Point objects. nodeID or parentID can be * or all.

  • subscribeUpstreamPoints(upstreamID, nodeID)

    Subscribes to up.<upstreamID>.<nodeID> and returns an async iterable for an array of Point objects. nodeID can be * or all.

  • subscribeUpstreamEdgePoints(upstreamID, nodeID, parentID)

    Subscribes to up.<upstreamID>.<nodeID>.<parentID> and returns an async iterable for an array of Point objects. nodeID or parentID can be * or all.

  • setUserID(userID)

    setUserID sets the user ID for this connection; any points / edge points sent from this connection will have their origin set to the specified userID

  • sendNodePoints(nodeID, points, { ack, opts })

    sendNodePoints sends an array of points for a given nodeID

    • ack - true if function should block waiting for send acknowledgement (defaults to true)
    • opts are options passed to the NATS request
  • sendEdgePoints(nodeID, parentID, edgePoints, { ack, opts })

    sendEdgePoints sends an array of edgePoints for the edge between nodeID and parentID

    • ack - true if function should block waiting for send acknowledgement (defaults to true)
    • opts are options passed to the NATS request

Deprecated API functions

  • subscribeMessages(nodeID)

    subscribeMessages subscribes to node.<nodeID>.msg and returns an async iterable for Message objects

  • subscribeNotifications(nodeID)

    subscribeNotifications subscribes to node.<nodeID>.not and returns an async iterable for Notification objects