Overview
Callbacks solve the problem of asynchronous request-response communication in FiveM, where events are one-way.Request-Response
Ask for data and receive a response
Bi-Directional
Client → Server or Server → Client
Type-Safe
Structured data exchange
Auto-Cleanup
Callbacks removed after execution
Server-Side Methods
RegisterServerCallback
Register a callback that clients can invoke. Signature:
Callback Parameters:
source(number) - Player source who triggered callbackdata(any) - Data sent from clientcb(function) - Response function to call with results
ClientCallback
Send a callback request to a client and receive response. Signature:
Example:
Client-Side Methods
ServerCallback
Send a callback request to server and receive response. Signature:
Example:
RegisterClientCallback
Register a callback that server can invoke. Signature:
Callback Parameters:
data(any) - Data sent from servercb(function) - Response function to call with results
ExtraId Parameter
TheextraId parameter prevents callback conflicts when the same event is triggered multiple times simultaneously.
Without extraId (can cause conflicts):
- Multiple simultaneous calls to same callback
- Loops that trigger callbacks
- Rapid successive calls
Complete Examples
Character Data Request
Server:Inventory Check
Server:Vehicle Spawn Request
Server:Shop Purchase
Server:Client-to-Server Notification
Client (Register):Best Practices
Always Handle Failure Cases
Always Handle Failure Cases
Check for errors in callback responses:
Use Descriptive Callback Names
Use Descriptive Callback Names
Name callbacks by resource and action:
Validate Input Data
Validate Input Data
Always validate callback data:
Use ExtraId for Loops
Use ExtraId for Loops
Prevent callback conflicts in loops:
Troubleshooting
Callback Not Firing
-
Check component name:
- Use
Callbacks(plural), notCallback
- Use
-
Verify callback is registered:
- Server callbacks must be registered before client calls them
- Register callbacks in resource startup, not in event handlers
-
Check event names match exactly:
Getting Wrong Data
-
Use extraId for disambiguation:
- Multiple simultaneous calls need unique extraIds
-
Check callback cleanup:
- Callbacks auto-delete after execution
- Don’t reuse the same callback multiple times
Timeout Issues
-
Callbacks timeout if no response:
- Always call
cb()in your handler - Don’t forget callback in async operations:
- Always call
Next Steps
Core - Fetch
Player and character data access
Core - Logger
Logging system
Events System
Understanding events
Middleware
Event middleware