ubus. request

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:

This is used internally by the ubus module when a published object's method is called by a client.

Example
// Method handler receives request as second argument
const obj = publish("my.service", {
    hello: (req, msg) => {
        req.reply({ message: "Hello" });
    }
});
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.

Returns: boolean
Example
// In a published object method handler
publish("my.service", {
    "async_method": (req, msg) => {
        req.defer();
        // Do async work...
        req.reply({ result: "done" });
    }
});

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.

Parameters:
NameTypeDescription
rcodenumber(optional, default: UBUS_STATUS_UNKNOWN_ERROR)

The error status code to return.

Returns: boolean
Example
// 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" });
    }
});

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.

Returns: number

The file descriptor number, or -1 if none was provided.

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.

Parameters:
NameTypeDescription
cbfunction

Callback invoked for incoming messages on the channel.

disconnect_cbfunction(optional)

Optional callback invoked when the channel is disconnected.

timeoutnumber(optional, default: 30)

The timeout in seconds for subsequent operations.

Returns: channel

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.

Parameters:
NameTypeDescription
replyObject(optional)

The reply data to send as an object with field names and values.

rcodenumber(optional, default: 0)

Optional status code to return. Use negative values to indicate more replies will follow.

Returns: boolean
Example
// In a published object method handler
publish("my.service", {
    "hello": (req, msg) => {
        req.reply({ message: "Hello, " + msg.name });
    }
});

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.

Parameters:
NameTypeDescription
data*

The data to send as reply. Can be any JSON-serializable value.

Returns: boolean
Examples
// 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 });
            });
    }
});

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.

Parameters:
NameTypeDescription
fdnumber

The file descriptor number to send.

Returns: boolean
Example
// 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" });
    }
});