simpleiot-js
v1.1.0
Published
SimpleIOT JavaScript API using NATS / WebSockets
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
idis "all" or falsy, this callsgetNodeChildreninstead (see below); however, we strongly recommend usinggetNodeChildrendirectly - If
parentis "all" or falsy, all instances of the specified node are returned - If
parentis "root", only root nodes are returned optsare options passed to the NATS request
The returned node contains the following properties:
id- the node IDtype- the node typehashparent- the parent ID for this node edgepointsList- the list of points for this nodeedgepointsList- 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 objecttypekeyvalue- numeric value of the pointtext- text value of the pointdata- 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 deletedorigin- the node ID of the user or other node that created this point.
- If
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
parentIDis "root", all root nodes are retrievedtype- 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 achildrenproperty, which is an array of that Node's descendant NodeEdges. Set to "flat" to return a single flattened array of NodeEdges.Note: If
typeis also set whenrecursiveis truthy,typerestricts which nodes are recursively searched. If you need to search descendants that do not match thetype, consider removing thetypefilter and filter manually.optsare options passed to the NATS request
getNodesForUser(userID, { type, includeDel, recursive, opts } = {})getNodesForUser returns the parent nodes for the given
userIDalong with their descendants ifrecursiveis 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 achildrenproperty, which is an array of that Node's descendant NodeEdges. Set to "flat" to return a single flattened array of NodeEdges.optsare options passed to the NATS request
subscribePoints(nodeID)Subscribes to
p.<nodeID>and returns an async iterable for an array of Point objects.nodeIDcan be*orall.subscribeEdgePoints(nodeID)Subscribes to
p.<nodeID>.<parentID>and returns an async iterable for an array of Point objects.nodeIDorparentIDcan be*orall.subscribeUpstreamPoints(upstreamID, nodeID)Subscribes to
up.<upstreamID>.<nodeID>and returns an async iterable for an array of Point objects.nodeIDcan be*orall.subscribeUpstreamEdgePoints(upstreamID, nodeID, parentID)Subscribes to
up.<upstreamID>.<nodeID>.<parentID>and returns an async iterable for an array of Point objects.nodeIDorparentIDcan be*orall.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
pointsfor a givennodeIDack- true if function should block waiting for send acknowledgement (defaults to true)optsare options passed to the NATS request
sendEdgePoints(nodeID, parentID, edgePoints, { ack, opts })sendEdgePoints sends an array of
edgePointsfor the edge betweennodeIDandparentIDack- true if function should block waiting for send acknowledgement (defaults to true)optsare options passed to the NATS request
Deprecated API functions
subscribeMessages(nodeID)subscribeMessages subscribes to
node.<nodeID>.msgand returns an async iterable for Message objectssubscribeNotifications(nodeID)subscribeNotifications subscribes to
node.<nodeID>.notand returns an async iterable for Notification objects
