Represents a deferred ubus method call context.
A request object is created when a published object method is invoked asynchronously. It provides the server-side interface for handling incoming method calls and sending responses.
The request context allows the method handler to:
- Send a successful reply with data (reply())
- Report an error condition (error())
- Defer completion for asynchronous processing (defer())
- Exchange file descriptors with the caller (get_fd(), set_fd())
- Establish channel-based communication (new_channel())
This is used internally by the ubus module when a published object's method is called by a client.
// Method handler receives request as second argument
const obj = publish("my.service", {
hello: (req, msg) => {
req.reply({ message: "Hello" });
}
});- Source
- See
- publish()
Methods
defer() → {boolean}nullable
Defer completion of a ubus method call.
Marks the current request as deferred, allowing the handler to complete the request asynchronously at a later time by calling reply().
Returns true on success.
Returns null if an error occurred.
// In a published object method handler
publish("my.service", {
"async_method": (req, msg) => {
req.defer();
// Do async work...
req.reply({ result: "done" });
}
});- Source
error(rcodeopt) → {boolean}nullable
Finish a ubus method call with an error status.
Completes the current deferred request with the specified error status code without sending any reply data.
Returns true on success.
Returns null if an error occurred or the reply was already sent.
| Name | Type | Description |
|---|---|---|
rcode | number | (optional, default: UBUS_STATUS_UNKNOWN_ERROR)The error status code to return. |
// In a published object method handler
publish("my.service", {
"process": (req, msg) => {
if (!msg.input) {
req.error(UBUS_STATUS_INVALID_ARGUMENT);
return;
}
req.reply({ result: "ok" });
}
});- Source
get_fd() → {number}
Get the caller's file descriptor from a ubus method call.
Returns the UNIX file descriptor number that was passed by the caller, or -1 if no file descriptor was sent.
The file descriptor number, or -1 if none was provided.
- Source
new_channel(cb, disconnect_cbopt, timeoutopt) → {channel}nullable
Create a new ubus channel from a method call context.
Creates a bidirectional channel communication path in response to an incoming method call. The callback will be invoked for incoming messages on the channel.
Returns a channel connection resource.
Returns null if the channel could not be created.
| Name | Type | Description |
|---|---|---|
cb | function | Callback invoked for incoming messages on the channel. |
disconnect_cb | function | (optional) Optional callback invoked when the channel is disconnected. |
timeout | number | (optional, default: 30)The timeout in seconds for subsequent operations. |
- Source
reply(replyopt, rcodeopt) → {boolean}nullable
Send a reply to a deferred ubus method call.
Sends the specified reply data to the caller of a deferred method request. After sending the reply, the request context is finished unless more is set to true.
Returns true on success.
Returns null if an error occurred or the reply was already sent.
| Name | Type | Description |
|---|---|---|
reply | Object | (optional) The reply data to send as an object with field names and values. |
rcode | number | (optional, default: 0)Optional status code to return. Use negative values to indicate more replies will follow. |
// In a published object method handler
publish("my.service", {
"hello": (req, msg) => {
req.reply({ message: "Hello, " + msg.name });
}
});- Source
reply(data) → {boolean}
Send a reply to an asynchronous method call.
Sends the specified data as a reply to the incoming method call. This function should be called within a published object's method handler.
Returns true on success.
Returns null if the reply could not be sent.
| Name | Type | Description |
|---|---|---|
data | * | The data to send as reply. Can be any JSON-serializable value. |
// Synchronous reply
const obj = publish("my.service", {
hello: (req, msg) => {
req.reply({ message: "Hello " + msg.name });
}
});// Asynchronous reply after defer completes
const obj = publish("my.service", {
some_method: (req) => {
ubus_conn.defer("other.object", "other_method", {},
(rc, data) => {
req.reply({ result: data });
});
}
});- Source
set_fd(fd) → {boolean}nullable
Set a file descriptor to send with a ubus method reply.
Associates a file descriptor with the current request to be sent back to the caller along with the reply.
Returns true on success.
Returns null if an error occurred.
| Name | Type | Description |
|---|---|---|
fd | number | The file descriptor number to send. |
// In a published object method handler
publish("my.service", {
"get_fd": (req, msg) => {
let fd = some_file_descriptor;
req.set_fd(fd);
req.reply({ info: "fd sent" });
}
});- Source