Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
node-unifi
Advanced tools
Node-UniFi is a NodeJS module that allows to query/control UniFi devices via the official UniFi-Controller API. It is developed to be compatible to the UniFi-Controller API version starting with v4.x.x up to v8.x.x
async
/await
or then()
/catch()
.node-unifi can be installed using the following npm command:
npm install node-unifi
node-unifi has been designed to be used quite straight forward and without introducing ackward language constructs. The following example should give a brief introduction on how to use node-unifi in your own applications using its Promises-based API interface:
const Unifi = require('node-unifi');
const unifi = new Unifi.Controller({'<HOSTNAME>', '<PORT>', sslverify: false});
(async () => {
try {
// LOGIN
const loginData = await unifi.login('<USERNAME>', '<PASSWORD>');
console.log('login: ' + loginData);
// GET SITE STATS
const sites = await unifi.getSitesStats();
console.log('getSitesStats: ' + sites[0].name + ':' + sites.length);
console.log(JSON.stringify(sites));
// GET SITE SYSINFO
const sysinfo = await unifi.getSiteSysinfo();
console.log('getSiteSysinfo: ' + sysinfo.length);
console.log(JSON.stringify(sysinfo));
// GET CLIENT DEVICES
const clientData = await unifi.getClientDevices();
console.log('getClientDevices: ' + clientData.length);
console.log(JSON.stringify(clientData));
// GET ALL USERS EVER CONNECTED
const usersData = await unifi.getAllUsers();
console.log('getAllUsers: ' + usersData.length);
console.log(JSON.stringify(usersData));
// LOGOUT
const logoutData = await unifi.logout();
console.log('logout: ' + JSON.stringify(logoutData));
} catch (error) {
console.log('ERROR: ' + error);
}
})();
Please note that every unifi.XXXXX()
function returns a Promise
, thus async
/await
as well as .then()
/.catch()
can be used accordingly.
Since version 2.0.0 node-unifi supports (thanks to unifi-axios-events) the WebSocket interface
of a UniFi controller. This new interface allows to listen for events using unifi.listen()
and automatically receive events
as soon as the UniFi controller sends them out via its WebSocket functionality. For receiving these events in a nodejs-compatible
way node-unifi uses internally EventEmitter2 which allows to execute actions based
on event filters defined by unifi.on(...)
.
An example on how to use this EventEmitter-based functionality of node-unifi to immediately receive state changes rather than regularly having to poll a unifi controller for changes can be seen here:
const Unifi = require('node-unifi');
const unifi = new Unifi.Controller({'<HOSTNAME>', '<PORT>', sslverify: false});
(async () => {
try {
// LOGIN
const loginData = await unifi.login('<USERNAME>', '<PASSWORD>');
console.log('login: ' + loginData);
// LISTEN for WebSocket events
const listenData = await unifi.listen();
console.log('listen: ' + listenData);
// Listen for alert.client_connected
unifi.on('alert.client_connected', function (data) {
const ts = new Date(data[0].timestamp).toISOString();
console.log(`${ts} - ${this.event}: ${data[0].parameters.CLIENT.id} (${data[0].parameters.CLIENT.name})`);
});
// Listen for alert.client_disconnected
unifi.on('alert.client_disconnected', function (data) {
const ts = new Date(data[0].timestamp).toISOString();
console.log(`${ts} - ${this.event}: ${data[0].parameters.CLIENT.id} (${data[0].parameters.CLIENT.name})`);
});
// Listen for ctrl.* events
unifi.on('ctrl.*', function () {
console.log(`${this.event}`);
});
} catch (error) {
console.log('ERROR: ' + error);
}
})();
More examples can be found in the "examples" sub-directory of this GitHub repository.
If you are having an application still using the obsolete v1 version of node-unifi and you want to port it to using the new/revised v2 version, all you have to do is:
async
/await
or .then()
/.catch()
logic for synchronous processing of events (see Examples) rather than expecting callback functions, forcing you to nest them properly.site
function argument required when calling a node-unifi function. Now you can either use the { site: 'my site' }
argument when passing contructor options to node-unifi or you switch to a different site using unifi.opts.site='my site'
before calling a node-unifi API function.sslverify: false
in the constructor optionsThis nodejs package/class uses functionality/Know-How gathered from different third-party projects:
The following projects are known to use this nodejs class for query/control UniFi devices:
The MIT License (MIT)
Copyright (c) 2017-2023 Jens Maus <mail@jens-maus.de>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
NodeJS class for querying UniFi-Controller (www.ubnt.com)
We found that node-unifi demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.