dbus
The dbus module provides Linux-only D-Bus APIs for JSH applications.
It supports method calls, property access, introspection, signal subscription, and name-owner watching.
Module load
const dbus = require("dbus");
const conn = new dbus.Connection({ busType: dbus.BusType.Session });If the runtime OS is not Linux, creating a connection fails.
BusType
dbus.BusType.Sessiondbus.BusType.System
Connection
D-Bus connection object.
Creation
new dbus.Connection(options)Options
| Option | Type | Default | Description |
|---|---|---|---|
| busType | string | dbus.BusType.Session | D-Bus bus type |
Return value
Connection
Error behavior:
- Throws when
busTypeis invalid. - Throws when the platform is not Linux.
close()
Closes the current D-Bus connection.
Syntax
conn.close()Return value
undefined
This method is idempotent and can be called more than once.
object()
Creates an ObjectProxy bound to destination/path.
Syntax
conn.object(destination, path)Parameters
destination(string): Service name (for example,org.freedesktop.DBus)path(string): Object path (for example,/org/freedesktop/DBus)
Return value
ObjectProxy
call()
Calls a D-Bus method.
Syntax
conn.call(request)Parameters
request(object): CallRequest
Return value
object: CallResult
Error behavior:
- Throws when required fields are missing.
- Throws when the object path is invalid.
getProperty()
Reads a D-Bus property.
Syntax
conn.getProperty(request)Parameters
request(object): PropertyRequest
Return value
object: PropertyResult
setProperty()
Writes a D-Bus property.
Syntax
conn.setProperty(request)Parameters
request(object): SetPropertyRequest
Return value
undefined
introspect()
Gets introspection metadata for an object.
Syntax
conn.introspect(request)Parameters
request(object): IntrospectRequest
Return value
object: IntrospectionNode
subscribeSignal()
Subscribes to D-Bus signals matching criteria.
Syntax
conn.subscribeSignal(request)Parameters
request(object): SignalWatchRequest
Return value
Connection(for chaining)
Error behavior:
- Throws
missing signal match criteriawhen all match fields are empty.
unsubscribeSignal()
Unsubscribes a previously registered signal match.
Syntax
conn.unsubscribeSignal(request)Parameters
request(object): SignalWatchRequest
Return value
Connection(for chaining)
Error behavior:
- Throws when no matching subscription exists.
watchName()
Starts watching owner changes for a bus name.
Syntax
conn.watchName(name)Parameters
name(string): D-Bus well-known name
Return value
Connection(for chaining)
unwatchName()
Stops watching owner changes for a bus name.
Syntax
conn.unwatchName(name)Parameters
name(string): D-Bus well-known name
Return value
Connection(for chaining)
Error behavior:
- Throws
name watch not foundwhen there is no active watch for the name.
getNameOwner()
Gets the current owner for a bus name.
Syntax
conn.getNameOwner(name)Parameters
name(string): D-Bus well-known name
Return value
object: NameOwnerResult
If the name has no owner, this method does not throw and returns hasOwner: false.
Events
Connection extends EventEmitter.
signal
Emitted on every subscribed D-Bus signal.
conn.on("signal", (sig) => {
console.println(sig.interface, sig.member, sig.body);
});name-owner-changed
Emitted when a watched name changes owner.
conn.on("name-owner-changed", (evt) => {
console.println(evt.name, evt.oldOwner, evt.newOwner);
});ObjectProxy
Created with conn.object(destination, path).
call()
obj.call(method, ...args)- Returns: same shape as CallResult
getProperty() / get()
obj.getProperty(name, interfaceName)
obj.get(name, interfaceName)getProperty()returns PropertyResultget()returns the property value only (result.value)
setProperty() / set()
obj.setProperty(name, value, interfaceName)
obj.set(name, value, interfaceName)introspect()
obj.introspect()- Returns: IntrospectionNode
subscribeSignal() / unsubscribeSignal()
obj.subscribeSignal(member, interfaceName)
obj.unsubscribeSignal(member, interfaceName)These are convenience wrappers that pass destination/path automatically.
Request/response structures
CallRequest
| Property | Type | Description |
|---|---|---|
| destination | string | Service name |
| path | string | Object path |
| method | string | Fully qualified method name (Interface.Method) |
| args | any[] | Method arguments |
| flags | number | D-Bus call flags |
CallResult
| Property | Type | Description |
|---|---|---|
| destination | string | Service name |
| path | string | Object path |
| method | string | Method name used for the call |
| body | any[] | Returned values |
PropertyRequest
| Property | Type | Description |
|---|---|---|
| destination | string | Service name |
| path | string | Object path |
| interface | string | Interface name |
| name | string | Property name |
PropertyResult
| Property | Type | Description |
|---|---|---|
| signature | string | D-Bus signature |
| value | any | Property value |
SetPropertyRequest
| Property | Type | Description |
|---|---|---|
| destination | string | Service name |
| path | string | Object path |
| interface | string | Interface name |
| name | string | Property name |
| value | any | Property value to write |
IntrospectRequest
| Property | Type | Description |
|---|---|---|
| destination | string | Service name |
| path | string | Object path |
IntrospectionNode
| Property | Type | Description |
|---|---|---|
| name | string | Node path/name |
| interfaces | object[] | Interface metadata list |
| children | object[] | Child node list |
Each interface includes methods/signals/properties/annotations.
SignalWatchRequest
| Property | Type | Description |
|---|---|---|
| destination | string | Optional, kept for symmetry with object-based calls |
| sender | string | Signal sender filter |
| path | string | Object path filter |
| interface | string | Interface filter |
| member | string | Member filter |
At least one of sender, path, interface, member must be provided.
NameOwnerResult
| Property | Type | Description |
|---|---|---|
| name | string | Requested bus name |
| owner | string | Unique name (:1.xx) or empty string |
| hasOwner | boolean | Whether an owner currently exists |
Usage examples
1) Basic method call
| |
2) Property read and write
| |
3) Introspection
| |
4) Signal subscription
| |
5) Name watch
| |
Error behavior notes
- Calling methods after
conn.close()throwsconnection not initialized. - Missing required fields in request objects throws an error.
- Invalid object paths throw an error.
getNameOwner()returns{ hasOwner: false }when the name exists but has no owner.- D-Bus tests and runtime behavior are Linux-only.