This library encapsulates the API from ioBroker backend to frontend.
There are 2 connection types in it:
Connection=> for all Web Frontends;AdminConnection=> for Admin UI Connections, these have access to more commands.
npm run build for one-time builds.
npm run watch for continuous builds.
npm test runs the unit tests of the frontend package (test/) and of the backend package (backend/test/) with the test runner of Node.js.
The frontend tests replace the socket with a fake one (test/lib/FakeSocket.ts), the backend tests talk to a real WebSocket server.
The documentation describes all functions with examples: how to connect in the browser and in Node.js, how to read, write and subscribe states, objects and files, and how errors, timeouts and the cache work.
Include the socket library from Admin or Web adapter:
<script src="../lib/js/socket.io.js"></script>Instantiate the connection:
const adminConnection = new AdminConnection({
protocol: 'ws',
host: '192.168.1.2',
port: 8081,
admin5only: false,
autoSubscribes: [],
// optional: other options
});
await adminConnection.startSocket();
await adminConnection.waitForFirstConnection();
// and use it
console.log(await adminConnection.getHosts());- (@GermanBluefox) Added the command
getObjectsCountto get the number of objects in the system
- (@GermanBluefox) Fixed a connection in the ioBroker cloud getting stuck if it was opened while the ioBroker of the user was not connected to the cloud, e.g. during its restart: every later connect got the same failed answer to
getVersionfrom the cache and never authenticated. While the cloud reportsioBroker is not connected, the version is asked again every few seconds now, and a failed or interrupted request is not kept in the cache anymore - (@joltcoke) Requests that are still waiting for an answer are rejected with
notConnectedErrorwhen the connection drops ordestroy()is called. Until now they waited forever, e.g. the upload of a large file over a slow line. Please note: a caller that does not handle errors gets an unhandled rejection now, and the server may have executed a request whose answer was lost - (@krobipd, @GermanBluefox) Fixed the admin staying on its start screen when the connection dropped while the data was loading at start (ioBroker.admin#3641, analysed and reproduced by @krobipd): the ws client reports every later connection as reconnect, which did not load the data, so the loading gave up when the reconnect took longer than 10 seconds. Now a reconnect before the data is loaded goes through the connect sequence again, the loading waits for the connection instead of using up its attempts, and the lost connection is not reported to
onError. The timeout of a request is stopped as soon as it is answered - (@GermanBluefox) Added unit tests for the frontend and the backend package
- (@GermanBluefox) Added the documentation of all functions with examples, and corrected the description of
autoSubscribes: it subscribes objects, not states, and their changes are passed toonObjectChange(#32) - (@GermanBluefox) A failed request is not kept in the cache anymore, the next call asks the server again. Until now e.g.
getEnums,getCompactSystemConfig,checkFeatureSupported,getGroupsorgetHostInforeturned the same error untilupdatewas requested. Successful answers are cached as before - (@GermanBluefox)
doNotLoadAllObjects: falsereally loads all objects and passes them toonReady; until nowonReadygot an empty list. If the objects cannot be loaded, the error goes toonErrorandonReadyis called anyway.getObjects()withoutupdatestill answers from the cache - (@GermanBluefox) Fixed subscriptions:
unsubscribeStateunsubscribed at the server also ids that still had other handlers, object subscriptions were sent twice after every reconnect, the state to ignore was subscribed at the server,unsubscribeFromInstancewithout a type sent an empty type, andsubscribeOnInstancedid not settle when the instance answered without a result (it resolvesnullnow). An exception of one handler does not stop the other handlers anymore - (@GermanBluefox)
destroy()also works before the socket is created and stops the version request, the data loading, the token checks, the token refresh and the reloads of the page. A rejection of the access token during a running token refresh does not reload the page anymore, and broken stored tokens are ignored - (@GermanBluefox) Fixed small things:
readMetaItemswithout rows,zh-CNas browser language, the detection of the cloud (iobroker.internalis no cloud), socket errors withouttoString, errors thrown inside a request keep their message (timeoutinstead ofError: timeout), the data is loaded only once when the server answers slowly - (@GermanBluefox) AdminConnection:
getInstalledResetCacheandgetRepositoryResetCacheaccept a host name,getRepositoryshares the cache of a host name and its object id,getHostByIpanswers for an unknown IP and rejects on a permission error, short PEM certificates and EC or encrypted private keys are recognized,upgradeControllerandrestartControllerreject on a permission error - (@GermanBluefox) AdminConnection: the host queries
getHostInfo,getHostInfoShort,getRepository,getInstalled,getCompactInstalled,getCompactRepository,readBaseSettingsandwriteBaseSettingsreject on the permission error of socket-classes 2.x ({ error: 'permissionError' }) and on other errors that came instead of the data. Until now they resolved with the error object as result - (@GermanBluefox) Fixed the use in Node.js (reported by @oweitman, #37): the replacement of
locationhasport,searchandhashnow. The redirect to the login page threw "Cannot read properties of undefined (reading 'includes')", and without the optionportthe URL had no port.protocolaccepts'http','https','ws'and'wss'without colon, too - (@GermanBluefox) Tokens in Node.js (reported by @Scrounger, #54): when a new login is required, the connection calls
onErrorwithoperation: 'authenticate'instead of opening a login page, which does not exist in Node.js. The access token is renewed at the URL of the connection (protocol,host,port), because Node.js cannot use the relative URL of the browser. SotokenTimeoutHandlerand the renewal work in Node.js, if the tokens are saved withConnection.saveTokensStatic(), see the documentation - (@GermanBluefox) Backend: fixed the query parameters of the URL, an answer without arguments (e.g. of
logout) does not crash the process anymore, a secondauthenticatedoes not close the connection anymore, the authenticate timeout and errors of the WebSocket constructor reach the error handlers, the late close of a replaced socket does not close the new connection anymore, "too many attempts" is reported once, no double slash for the URL/, invalid messages are ignored, no debug output anymore. New optioncallbackTimeout: a request without answer getstimeoutafter this time (off by default, as before) - (@GermanBluefox) Removed the unused dependency
@iobroker/wsfrom the backend package (#69): the package has its own client (SocketClient), whosedestroy()andclose(true)end a connection for good, without a reconnect
- (@GermanBluefox) When the server rejects the access token (
reauthenticate), the connection first tries to get a new one with the refresh token and only goes to the login page if that fails. Until now everyreauthenticateled to the login page, although the user had asked to stay logged in - (@GermanBluefox) A failed token refresh no longer throws the tokens away when another tab has renewed them in the meantime (a refresh token can be used only once); the new access token is announced to the server instead. Tokens are only deleted when the server has really rejected the refresh token, a server that cannot be reached leads to a retry
- (@GermanBluefox) Only one refresh request runs at a time, and waiting for the lock of another tab no longer spins synchronously
- (@GermanBluefox) Added support for web-socket-only (socket.io) communication
- (@SimonFischer04) Added socketPath to allow for (web) running behind reverse proxy
- (@GermanBluefox) Extended cmdExec with files
- (@GermanBluefox) Migrated to TS 6
- (@GermanBluefox) Allowed to call getCompactSystemConfig in web too
- (@GermanBluefox) Updated packages
- (@bloop16) Better error handling implemented
- (@bloop16) Added destroy method to close the connection
- (@GermanBluefox) Updated packages
- (@GermanBluefox) Improved typing
- (@GermanBluefox) Updated packages, e.g. TypeScript 5.9
- (@GermanBluefox) Added new method: getObjectViewSystemCached
- (@GermanBluefox) Allowed using of this library in Node.js
- (@GermanBluefox) Added debug information
- (@GermanBluefox) Corrected redirect by login
- (@GermanBluefox) Updated packages. TypeScript 5.8
- (@GermanBluefox) Added support for OAuth2 authentication
- (@GermanBluefox) Updated js-controller 7 packages
- (@GermanBluefox) Prevented small possible error by subscribeStates
- (@GermanBluefox) Added the log message type
- (@GermanBluefox) Changed behavior by timeout: do not cache such responses
- (@GermanBluefox) Added new
socket.ionamespaceiob
- (@GermanBluefox) Migrated to eslint@9
- (@GermanBluefox) Breaking change: all thrown errors are now instances of
Errorclass
- (@GermanBluefox) made protocol and host optional
- (@GermanBluefox) Corrected typing of
CompactInstanceInfo
- (@GermanBluefox) Corrected typing of cmdExec
- (@GermanBluefox) Corrected upgradeController
- (@GermanBluefox) Added admin functions: upgradeAdapterWithWebserver, upgradeController, upgradeOsPackages, updateLicenses
- (@GermanBluefox) Better typing for subscribeOnInstance
- (@GermanBluefox) Added source files for TypeScript
- (@GermanBluefox) Replaced the SystemConfig type with ioBroker.SystemConfigObject
- (@GermanBluefox) Allowed calling getObjectView, getObjectViewSystem and getObjectViewCustom without options
- (@GermanBluefox) Improved getNotifications command
- (@GermanBluefox) Corrected the object subscribing
- (@GermanBluefox) Corrected types
- (@GermanBluefox) Allowed subscribing and unsubscribing on arrays of IDs
- (@GermanBluefox) Changed systemLang to writable, as it can be changed on the fly
- (foxriver76) fix
cjstypes export - (@GermanBluefox) Better typing for getLogs
- (@GermanBluefox) Better typing for getNotifications
- (@GermanBluefox) updated packages
- (foxriver76) port to
@iobroker/types
- (foxriver76) improve performance on
subscribeStatewithout wildcard
- (@GermanBluefox) Added return value for
subscribeOnInstance
- (foxriver76) Corrected import of modules
- (@GermanBluefox) Added implicit export of AdminConnection
- (jogibear9988) Updated Connection api documentation
- (@GermanBluefox) Added
subscribeStateAsyncmethod for legacy compatibility
- (@GermanBluefox) Added the subscribing on the specific instance messages
- (@GermanBluefox) Update packages
- (@GermanBluefox) added new method -
getObjectsById
- (rovo89) Typescript types tuning
- (@GermanBluefox) The path was removed from
socket.ioURL
- (@GermanBluefox) better detection of chained certificates
- (@GermanBluefox) packages updated
- (@GermanBluefox) Added
renameandrenameFilemethods
- (@GermanBluefox) Made the fix for
materialandecharts
- (@GermanBluefox) Caught errors on state/object changes
- (@GermanBluefox) Special changes for vis and "nothing_selected" ID
- (@GermanBluefox) Added
logcommand
- (jogibear9988) Added getObjectViewSystem and getObjectViewCustom and deprecated getObjectView
- (@GermanBluefox) Added support of authentication token
- (@GermanBluefox) Working on cloud connection
- (@GermanBluefox) Added method getCompactSystemRepositories
- (@GermanBluefox) Added ack parameter to
setStatemethod.
- (@GermanBluefox) Allowed call of getStates with a pattern
- (@GermanBluefox) Errors on connection are handled now
- (@GermanBluefox) Added preparations for iobroker cloud
- (@GermanBluefox) Added functions to reset cache
- (@GermanBluefox) Allowed connections behind reverse proxy
- (@GermanBluefox) Added functions to reset cache
- (@GermanBluefox) Corrected the cache problem by
getInstalledandgetRepositorycommands
- (@GermanBluefox) Allowed connections behind reverse proxy
- (@GermanBluefox) Added methods: subscribeFiles, unsubscribeFiles
- (@GermanBluefox) Extended
getVersioncommand with update
- (AlCalzone) corrected: reload on websocket error instead of alert()-ing
- (@GermanBluefox) Added
logoutcommand - (@GermanBluefox) Move
getGroupsto web connection
- (jogibear998) Fix connection with web adapter
- (jogibear998 & AlCalzone) Convert package to a CommonJS/ESM hybrid
- (@GermanBluefox) Fixed
getInstalledcommand
- (@GermanBluefox) Improved the vendor support
- (AlCalzone) setSystemConfig simplified
- (AlCalzone) The package was completely rewritten to make proper use of TypeScript
- (@GermanBluefox) Fix the renaming of groups
- (jogibear9988) Test release
- (@GermanBluefox) Update methods
- (UncleSamSwiss) Add release script and release workflow
- (jogibear9988) Create the Repository from the Code in https://github.com/ioBroker/adapter-react
The MIT License (MIT)
Copyright (c) 2021-2026 Jochen Kühner