# Callbacks

The Callbacks module provides a standalone, framework-agnostic system for registering and triggering callbacks between the client and server.

***

## Accessing the Module

```lua
local CALLBACKS <const> = exports.boii_utils:get("modules.callbacks")
```

***

## Server

### register\_callback(name, cb)

Registers a server-side callback that clients can trigger.

### Parameters

| Name | Type       | Description                                             |
| ---- | ---------- | ------------------------------------------------------- |
| name | `string`   | The unique identifier for the callback event.           |
| cb   | `function` | The function to execute when the callback is triggered. |

### Example

```lua
CALLBACKS.register("get_server_time", function(source, data, cb)
    local response = {
        unix = os.time(),
        hour = GetClockHours()
    }
    cb(response)
end)
```

***

### Client

## trigger\_callback(name, data, cb)

Triggers a server-side callback and handles the result asynchronously.

### Parameters

| Name | Type       | Description                                                               |
| ---- | ---------- | ------------------------------------------------------------------------- |
| name | `string`   | The event name to trigger.                                                |
| data | `table`    | Optional data to send to the server.                                      |
| cb   | `function` | Callback function to handle the response. Signature: `function(response)` |

### Example

```lua
CALLBACKS.trigger("get_server_time", nil, function(response)
    print("Unix time:", response.unix)
    print("In-game hour:", response.hour)
end)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.boii.dev/old-docs/boii_utils/api/callbacks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
