Installations
npm install obniz-app-sdk
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
>= 14.21.1
Node Version
16.15.1
NPM Version
8.11.0
Score
34.6
Supply Chain
61
Quality
71.4
Maintenance
50
Vulnerability
97.3
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (94.37%)
Lua (5.63%)
Developer
obniz
Download Statistics
Total Downloads
7,583
Last Day
2
Last Week
10
Last Month
74
Last Year
1,059
GitHub Statistics
1 Stars
242 Commits
4 Watching
10 Branches
5 Contributors
Package Meta Information
Latest Version
1.4.0
Package Id
obniz-app-sdk@1.4.0
Unpacked Size
639.38 kB
Size
170.94 kB
File Count
172
NPM Version
8.11.0
Node Version
16.15.1
Publised On
09 Feb 2023
Total Downloads
Cumulative downloads
Total Downloads
7,583
Last day
0%
2
Compared to previous day
Last week
-44.4%
10
Compared to previous week
Last month
51%
74
Compared to previous month
Last year
-63.2%
1,059
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
25
obniz-app-sdk
This is a framework for creating nodejs hosted apps using obniz.
You can use this SDK to run your program all the time and control the devices on which your app is installed with this program.
It will periodically synchronize with obnizCloud and automatically apply device additions, removals, and configuration changes.
This SDK can also operate a large number of devices by load balancing across multiple machine instances to support the long-term operation of a large number of devices.
Features
- Program operation in conjunction with obnizCloud
- Load balancing mode with multiple instances
- Multi-core load balancing support by pm2
- Master/Manager reduction by redis
System Architecture
How to introduce
Create Hosted App on obnizCloud
Create a hosted app on obnizCloud.
Next, install the created application on the device you wish to use.
Installing SDK
Create a nodejs project and install the sdk.
$ npm i obniz-app-sdk
Use SDK
Prepare a Nodejs program to control the device, and include obniz-app-sdk.
You will be creating a child class of the Worker class, which will be instantiated and executed for each device, and App is where you specify the app information and how to scale it.
The following example shows how to scan for bluetooth on a device and output to log.
1const { App, AppInstanceType, Worker } = require('obniz-app-sdk') 2const Obniz = require("obniz"); 3 4class MyWorker extends Worker { 5 6 // Called only once when a connection to an obniz is made. 7 // The obniz argument is the same as each obniz instance in obniz.js. 8 async onObnizConnect(obniz){ 9 await obniz.ble.initWait(); 10 } 11 12 // Runs repeatedly as long as it is connected to the device. 13 // Like obniz.onloop, it always pingsWait() with the device and loops. 14 // The obniz argument is the same as each obniz instance in obniz.js. 15 async onObnizLoop(obniz){ 16 const peripherals = await obniz.ble.scan.startAllWait(null, { 17 duration : 10 18 }); 19 console.log(`founded beacons by obniz ${obniz.id} length=${peripherals.length}`) 20 } 21 22} 23 24const app = new App({ 25 appToken: process.env.APPTOKEN, 26 workerClass: MyWorker, 27 instanceType: AppInstanceType.Master, 28 obnizClass: Obniz 29}) 30 31app.start();
Options
The following options are available for App.
key | mean |
---|---|
workerClass | Specify a MyWorker class that describes the process for each device. (Not necessary if instanceType is set to Manager. |
appToken | Specify the App Token listed in the App Settings page of the Developer Console. |
instanceType | Required for autoscaling. 1st unit must be Master or Manager , 2nd and later units must be Slave . |
obnizClass | Specify the obniz class to be used by the Worker. |
obnizOption | The second argument of new Obniz() . |
database | Specifies the coordination mode for multiple machines. See Multi-Machines for details. |
databaseConfig | Specify the DB connection method for multi-machine cooperation. See Multi-Machines for details. |
instanceName | Specify a string that identifies this process. This must be unique. By default, os.hostname() is used. |
For other optional parameters, please refer to the program (App.ts).
instanceType
There are three types of instanceType
, each of which works as follows
- Master (
AppInstanceType.Master
)- This type performs three types of operations: synchronization with obnizCloud, distribution of Workers, and operation as a Worker.
- It can work as a standalone unit.。
- Manager (
AppInstanceType.Manager
)- This type performs only two operations: synchronization with obnizCloud and worker allocation.
- At least one Slave must be running at the same time。
- Slave (
AppInstanceType.Slave
)- This type performs only one operation as a Worker.
- At least one Master or Manager must be running at the same time。
Deploy
Run this Nodejs project on a server. The following is an example of pm2.
1$ npm install pm2 -g 2$ pm2 startup ubuntu 3$ pm2 start index.js 4$ pm2 save
While running, it will always work with obnizCloud to monitor the addition and removal of installed devices and increase or decrease the number of workers.
Examples
Examples can be found Examples
Multi-Machines
This program can be run on multiple machines and work together to operate a large number of devices.
The mode is specified by database
.
Here are some of the features of load balancing.
- The master process also functions as a worker. Specify
Manager
to focus on managing without worker inside of it. - It distributes the load so that all loads are evenly distributed.
- Even if it detects the addition of a machine later, it will not stop -> move the running one.
database:'memory'
memory
is a single-instance mode.
Distributed on multiple cores, not distributed on multiple machines.
database:'redis'
It performs inter-process coordination and load balancing using a redis server.
Each machine must be able to access a common redis server.
- Example1
- 1x Master + 1x Slave pattern that manages workers and has its own worker
- Example2
- 1x Manager + 1x Slave pattern that only manages workers and has no worker itself
- Example3
- 2x Manager or 2x Master + 2x Slave pattern (Master and Manager can operate together)
1// Example 2{ 3 database: "redis", 4 databaseConfig: process.env.REDIS_URL || "redis://localhost:6379" 5}
database:'mqtt'
Unlike Redis load balancing, there is no need to set up a server.
1// Example 2{ 3 database: "mqtt", 4 databaseConfig: process.env.MQTT_SEED_URL || "mqtt://127.0.0.1", 5}
Multi-Core.
This function supports cluster function of pm2 for multi-core, so that CPU can be used optimally.
This feature works only when load balancing is enabled on multiple machines (it does not work with database:'memory'
).
There is no need to configure it, just start it as usual as in the example, and it will automatically identify the pm2 cluster and start it as multiple processes.
If you don't want to start multiple processes, please turn off cluster in pm2 or set the number of cluster instances to 1.
LifeCycle
The Worker class has LifeCycle.
1class MyWorker extends Worker { 2 3 /** 4 * Worker lifecycle 5 */ async 6 7 async onStart(){ 8 console.log("onStart"); 9 //You can use cloudLog.info to record logs that can be viewed on the device details page of obniz.com. 10 this.cloudLog.info("obniz Connect"); 11 } 12 13 async onLoop(){ 14 console.log("onLoop"); 15 } 16 17 async onEnd(){ 18 console.log("onEnd"); 19 } 20 21 async onRequest(key) { 22 return `response from ${this.obniz.id}` 23 } 24 25 /** 26 * obniz lifecycle 27 */ onObnizConnect(obniz.id) 28 29 async onObnizConnect(obniz){ 30 console.log(`connected to obniz ${obniz.id} ${obniz.metadata.description}`); 31 } 32 33 async onObnizLoop(obniz){ 34 console.log(`obniz loop ${obniz.id} ${obniz.metadata.description}`); 35 } 36 37 async onObnizClose(obniz){ 38 console.log(`obniz disconnected from ${obniz.id} ${obniz.metadata.description}`); 39 } 40 41 42}
The lifecycle diagram is shown below.
Performance
The number of obniz devices that can be operated on one machine depends on the program. It needs to be adjusted depending on the resource load. The following values are for reference.
On a machine with 1Ghz, 1Core, 1GB Memory
Recommended range of the number of obniz devices in one instance | Case |
---|---|
300-1,000 | In the case of BLE beacon detection and periodic API transmission of collected information to another server |
30-200 | if frequent communication and analysis is needed with connected BLE devices |
100-500 | For a program that converts AD voltage, detects voltage errors, and sends API to another server |
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
Found 3/4 approved changesets -- score normalized to 7
Reason
dependency not pinned by hash detected -- score normalized to 1
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/git-pr-release.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/obniz/obniz-app-sdk/git-pr-release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/git-pr-release.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/obniz/obniz-app-sdk/git-pr-release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.js.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/obniz/obniz-app-sdk/node.js.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.js.yml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/obniz/obniz-app-sdk/node.js.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.js.yml:32: update your workflow using https://app.stepsecurity.io/secureworkflow/obniz/obniz-app-sdk/node.js.yml/master?enable=pin
- Info: 0 out of 5 GitHub-owned GitHubAction dependencies pinned
- Info: 1 out of 1 npmCommand dependencies pinned
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/git-pr-release.yml:1
- Warn: no topLevel permission defined: .github/workflows/node.js.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Reason
42 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-wf5p-g6vw-rhxx
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-7gc6-qh9x-w6h8
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-qrpm-p2h7-hrv2
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-w7rc-rwvf-8q5r
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-44c6-4v22-4mhx
- Warn: Project is vulnerable to: GHSA-4x5v-gmq8-25ch
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-gx6r-qc2v-3p3v
- Warn: Project is vulnerable to: GHSA-cvv5-9h9w-qp2m
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3
- Warn: Project is vulnerable to: GHSA-7jxr-cg7f-gpgv
- Warn: Project is vulnerable to: GHSA-xj72-wvfv-8985
- Warn: Project is vulnerable to: GHSA-ch3r-j5x3-6q2m
- Warn: Project is vulnerable to: GHSA-p5gc-c584-jj6v
- Warn: Project is vulnerable to: GHSA-whpj-8f3w-67p5
- Warn: Project is vulnerable to: GHSA-cchq-frgv-rjh5
- Warn: Project is vulnerable to: GHSA-g644-9gfx-q4q4
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2.9
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More