Latent Callbacks

fivem_latent_callbacks

This script enables you to make “latent” callbacks, using the Native Trigger(Client/Server)LatentEvent to transfer huge data from server → client and opposite. You can use it for Data Transfer or simply for your normal callbacks.

Download (GitHub)

Usage

  1. Use RegisterCallback(eventName, function(args) … end) to register a callback.
  2. UnregisterCallback(eventName) to remove a registered callback.
  3. Use TriggerCallback(eventName, args, timeout, asyncCallback, method) to call a callback.
    • eventName: string identifier for the callback.
    • args: any Lua table of arguments, with optional key __playerId for server usage.
    • timeout: number of seconds (optional). Rejects promise if time exceeded.
    • asyncCallback: function (optional). If provided, callback is invoked asynchronously.
    • method: ‘normal’ or ‘latent’. ‘latent’ can be used for large data.
  4. Use TriggerLatentCallback(eventName, args, timeout, asyncCallback) as shorthand for a latent call.
Code is accessible Yes
Subscription-based No
Lines (approximately) ~238
Requirements & dependencies NONE
Support Yes
7 Likes

Great job, dude! I found the approach really interesting.

1 Like

Hello! Looks super interesting! If I understand this is useful to optimize also? Or is to load large data without any Wait needed? I was reading the readme and I want test this, but my question was, what this means please?

PLEASE NOTE that when transfering huge payloads the whole server network can freeze, not letting any other event come thru!

And for example this can replace any framework (in my case ESX) callback and optimize them?

1 Like

Hi, thanks for your interest. Firstly no… this script is not meant to optimize anything or reduce load time but to enable the use of latent events in callbacks because without the latent method events return a error when trying to pass huge data. With the warning I was trying to say that when you for example transfer a audio file as raw base64 data, wich will be a huge string often many kilobytes long, the network can be busy transferring the string to its destination resulting in blockage for other events, meaning that no other events, for your case important esx events, come through.
Surely you can use my code to implement the latent method into the esx callbacks but I’m not positive that it will optimize them in any way. :smile:

1 Like

Update (03.04.2025)

I have made some changes:

1. Improved Return Handling

  • You can now use multiple return values directly:
    return "hello", 123
    
  • Still 100% backward compatible with:
    return { "hello", 123 }
    

2. Cleaner Server→Client Calls

  • No need to manually include __playerId when triggering a server callback from the client.
  • It’s now automatically handled internally.

3. Safer Unpacking & Error Handling

  • Fixed bug where unpacking early caused nil or table: 0x... issues.
  • Timeout handling improved: no more unexpected crashes.

4. Cleaner README

  • Full documentation with:
    • How to Use
    • Usage Examples
    • Return Styles
    • Troubleshooting

Get it here: GitHub (GitHub - BahBROOOT/fivem_latent_callbacks: A script that combines latent events and callbacks.)