simpleiot-js
v1.1.0
Published
SimpleIOT JavaScript API using NATS / WebSockets
Downloads
5
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 callsgetNodeChildren
instead (see below); however, we strongly recommend usinggetNodeChildren
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 IDtype
- the node typehash
parent
- 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 objecttype
key
value
- 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
parentID
is "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 achildren
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 whenrecursive
is truthy,type
restricts which nodes are recursively searched. If you need to search descendants that do not match thetype
, consider removing thetype
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 ifrecursive
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 achildren
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*
orall
.subscribeEdgePoints(nodeID)
Subscribes to
p.<nodeID>.<parentID>
and returns an async iterable for an array of Point objects.nodeID
orparentID
can be*
orall
.subscribeUpstreamPoints(upstreamID, nodeID)
Subscribes to
up.<upstreamID>.<nodeID>
and returns an async iterable for an array of Point objects.nodeID
can be*
orall
.subscribeUpstreamEdgePoints(upstreamID, nodeID, parentID)
Subscribes to
up.<upstreamID>.<nodeID>.<parentID>
and returns an async iterable for an array of Point objects.nodeID
orparentID
can 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
points
for a givennodeID
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 betweennodeID
andparentID
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 objectssubscribeNotifications(nodeID)
subscribeNotifications subscribes to
node.<nodeID>.not
and returns an async iterable for Notification objects