github.com/simpleiot/simpleiot@v0.18.3/frontend/lib/README.md (about) 1 # simpleiot-js 2 3 SimpleIoT JavaScript API using NATS / WebSockets 4 5 This package allows JavaScript clients (especially web browsers) to connect to 6 SimpleIoT using [nats.ws](https://github.com/nats-io/nats.ws). 7 8 ## Install 9 10 `npm i simpleiot-js` 11 12 ## Usage 13 14 ```js 15 import { connect } from "simpleiot-js" 16 ;(async function siotConnect() { 17 try { 18 // Note: nats.ws has built-in reconnection logic by default 19 const nc = await connect({ 20 servers: "localhost:9222", 21 // Pass in options as documented in nats.ws package 22 }) 23 // `getServer()` is a method documented by nats.ws 24 console.log(`connected to ${nc.getServer()}`) 25 // `closed()` is a nats.ws method that returns a promise 26 // indicating the client closed 27 const done = nc.closed() 28 29 // Example: get root nodes from SimpleIoT tree 30 const n = await nc.getNodeChildren("root") 31 32 // close the connection 33 await nc.close() 34 // check if the close was OK 35 const err = await done 36 if (err) { 37 console.log(`error closing:`, err) 38 } 39 } catch (err) { 40 console.error("connection error:", err) 41 } 42 })() 43 ``` 44 45 ## API 46 47 The SimpleIoT package is simply a wrapper of the 48 [nats.ws](https://github.com/nats-io/nats.ws) package. Any API documented in the 49 nats.ws package will work. We have also added the following functions specific 50 to SimpleIoT. 51 52 - `getNode(id, { parent, type, includeDel, opts } = {})` 53 54 getNode sends a request to `nodes.<parent>.<id>` to retrieve an array of 55 NodeEdges for the specified Node ID. 56 57 - If `id` is "all" or falsy, this calls `getNodeChildren` instead (see below); 58 however, we strongly recommend using `getNodeChildren` directly 59 - If `parent` is "all" or falsy, all instances of the specified node are 60 returned 61 - If `parent` is "root", only root nodes are returned 62 - `opts` are options passed to the NATS request 63 64 The returned node contains the following properties: 65 66 - `id` - the node ID 67 - `type` - the node type 68 - `hash` 69 - `parent` - the parent ID for this node edge 70 - `pointsList` - the list of points for this node 71 - `edgepointsList` - the list of edge points for the edge between this node 72 and the specified parent 73 74 Each point contains the following properties: 75 76 - `time` - timestamp of the point converted to a JavaScript Date object 77 - `type` 78 - `key` 79 - `value` - numeric value of the point 80 - `text` - text value of the point 81 - `data` - data contained within the point (encoded as base64 string) 82 - `tombstone` - if tombstone value is even, the point is active; otherwise, if 83 it is odd, the point is considered deleted 84 - `origin` - the node ID of the user or other node that created this point. 85 86 - `getNodeChildren(parentID, { type, includeDel, recursive, opts } = {} )` 87 88 getNodeChildren sends a request to `nodes.<parentID>.<id>` to retrieve an 89 array of child NodeEdges of the specified parent node. 90 91 - If `parentID` is "root", all root nodes are retrieved 92 - `type` - can be used to filter nodes of a specified type (defaults to "") 93 - `includeDel` - set to true to include deleted nodes (defaults to false) 94 - `recursive` - set to true to recursively retrieve all descendants matching 95 the criteria. In this case, each returned NodeEdge will contain a `children` 96 property, which is an array of that Node's descendant NodeEdges. Set to 97 "flat" to return a single flattened array of NodeEdges. 98 99 Note: If `type` is also set when `recursive` is truthy, `type` restricts 100 which nodes are recursively searched. If you need to search descendants that 101 do _not_ match the `type`, consider removing the `type` filter and filter 102 manually. 103 104 - `opts` are options passed to the NATS request 105 106 - `getNodesForUser(userID, { type, includeDel, recursive, opts } = {})` 107 108 getNodesForUser returns the parent nodes for the given `userID` along with 109 their descendants if `recursive` is truthy. 110 111 - `type` - can be used to filter nodes of a specified type (defaults to "") 112 - `includeDel` - set to true to include deleted nodes (defaults to false) 113 - `recursive` - set to true to recursively retrieve all descendants matching 114 the criteria. In this case, each returned NodeEdge will contain a `children` 115 property, which is an array of that Node's descendant NodeEdges. Set to 116 "flat" to return a single flattened array of NodeEdges. 117 - `opts` are options passed to the NATS request 118 119 - `subscribePoints(nodeID)` 120 121 Subscribes to `p.<nodeID>` and returns an 122 [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) 123 for an array of Point objects. `nodeID` can be `*` or `all`. 124 125 - `subscribeEdgePoints(nodeID)` 126 127 Subscribes to `p.<nodeID>.<parentID>` and returns an 128 [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) 129 for an array of Point objects. `nodeID` or `parentID` can be `*` or `all`. 130 131 - `subscribeUpstreamPoints(upstreamID, nodeID)` 132 133 Subscribes to `up.<upstreamID>.<nodeID>` and returns an 134 [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) 135 for an array of Point objects. `nodeID` can be `*` or `all`. 136 137 - `subscribeUpstreamEdgePoints(upstreamID, nodeID, parentID)` 138 139 Subscribes to `up.<upstreamID>.<nodeID>.<parentID>` and returns an 140 [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) 141 for an array of Point objects. `nodeID` or `parentID` can be `*` or `all`. 142 143 - `setUserID(userID)` 144 145 setUserID sets the user ID for this connection; any points / edge points sent 146 from this connection will have their origin set to the specified userID 147 148 - `sendNodePoints(nodeID, points, { ack, opts })` 149 150 sendNodePoints sends an array of `points` for a given `nodeID` 151 152 - `ack` - true if function should block waiting for send acknowledgement 153 (defaults to true) 154 - `opts` are options passed to the NATS request 155 156 - `sendEdgePoints(nodeID, parentID, edgePoints, { ack, opts })` 157 158 sendEdgePoints sends an array of `edgePoints` for the edge between `nodeID` 159 and `parentID` 160 161 - `ack` - true if function should block waiting for send acknowledgement 162 (defaults to true) 163 - `opts` are options passed to the NATS request 164 165 ## Deprecated API functions 166 167 - `subscribeMessages(nodeID)` 168 169 subscribeMessages subscribes to `node.<nodeID>.msg` and returns an 170 [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) 171 for Message objects 172 173 - `subscribeNotifications(nodeID)` 174 175 subscribeNotifications subscribes to `node.<nodeID>.not` and returns an 176 [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) 177 for Notification objects