NUI: messages, callbacks, focus and the limits of the in-game browser
In short
Once the loading screen closes, every HUD, menu and phone is NUI: a page in the in-game browser that talks to Lua. That browser is Chromium with a few gaps, and knowing them saves days of “works in Chrome, broken in game”.
| Direction | Lua | Page |
|---|---|---|
| Lua → page | SendNUIMessage({ action = 'open', data = … }) | window.addEventListener('message', e => e.data) |
| Page → Lua | RegisterNuiCallback('name', function(data, cb) cb({ ok = true }) end) | fetch(https://${GetParentResourceName()}/name, …) |
| Input | SetNuiFocus(true, true) / SetNuiFocus(false, false) | Close on Escape and tell Lua |
| Files | ui_page + files | Relative paths; https://cfx-nui-<resource>/… for other resources |
- Call
cbin every NUI callback, error paths included. Without it the page’s fetch never resolves. - Release focus on each way the UI can close, and again when the resource stops.
- Match the case of file names exactly, because Linux servers are case-sensitive.
- In React,
useNuiEvent,fetchNui, mockeddebugDataandisEnvBrowser()let most of the work happen in a normal browser.
The callback side is covered in more depth in NUI callbacks and fetch. The table below lists what the in-game browser does not do, and what to use in its place.
| Limitation | Workaround |
|---|---|
backdrop-filter unreliable | Solid semi-transparent colors |
| H.264 not dependable | WebM video |
Links and window.open do nothing | window.invokeNative('openUrl', url) |
alert and native dialogs useless | Your own modal |
| Async clipboard often fails | Show the text to copy manually |
- To debug, open
http://localhost:13172/in a Chromium browser while the game is running, or runnui_devToolsin F8 (developer mode). - Send a message only when a value changes, and throttle speed and fuel to a few updates per second.
- Animate
transformandopacityonly, and hide a closed UI withdisplay: none. - For a HUD, show what matters at that moment, anchor it to the minimap, respect
GetSafeZoneSize()and hide it whileIsPauseMenuActive()is true. - Route everything through one notification resource, so that ESX, QBCore and ox_lib toasts look alike.
Go deeper:NUI basics: building interfaces for FiveM →NUI callbacks: sending data from the UI to Lua →NUI focus, the mouse cursor and keyboard input →Getting NUI asset paths right →Structuring a React NUI resource for FiveM →Limitations of FiveM’s embedded browser (CEF) →Keeping FiveM NUI fast →Debugging FiveM NUI with Chromium devtools →Designing a FiveM HUD players actually like →Building a custom notification UI for FiveM →