Development ​
Layout ​
fxmanifest.lua lua54; init.lua is in files {}, np_net never runs it itself
init.lua the shim other resources include (shared_script '@np_net/init.lua')
shared/config.lua convars → Config
shared/np_admin_settings.lua the settings np_admin shows
bridge/np_admin.lua np_admin integration lib, vendored byte-identical
server/core.lua store, delta merging, report validation, drop categories, Prometheus text,
HTTP auth, manifest patching. No threads or event wiring: testable in plain Lua
server/heatmap.lua heatmap grid per day and layer, files, merging and queries. Plain Lua too
server/main.lua wiring: push export, report event, playerDropped, ping and tick threads,
state bag handler, heatmap sampler / deaths / AddHeat, SetHttpHandler (metrics
and heatmap routes), npnet command, np_admin integration
client/main.lua batches the client shims' deltas into one report (with the FPS of the last
5 s), prints staff tool output in F8
web/heatmap.html the heatmap viewer (Nimbus UI, canvas), served by the server only
web/nimbus/ Nimbus UI tokens / components / theme CSS for the viewer
data/ heatmap day files (written at runtime, git-ignored)
monitoring/ Prometheus + Grafana docker compose stack and the dashboard
tests/ plain Lua 5.4, no FiveM needed
The shim ​
init.lua runs at the top of the including resource, in its Lua state, on server and client. It returns at once when
it is already loaded in that state (__np_net_shim) or when the resource is np_net.
Sends. The wrapped TriggerClientEvent / TriggerServerEvent (and the latent versions) pack the arguments once
with msgpack.pack_args and call the matching *Internal native with the packed payload and its length, exactly what
the CitizenFX scheduler does. The payload length is the byte count. Recipients for egress: the player count for -1
(also counted as a broadcast), 1 for a single id. A table of targets, a missing pack_args or a missing native falls
back to the original function, counted without bytes.
Which handlers. RegisterNetEvent / RegisterServerEvent mark a name as a net event. Only handlers of marked
names are wrapped; any other AddEventHandler goes to the scheduler untouched. When RegisterNetEvent(name) comes
after AddEventHandler(name, fn), the existing handlers are wrapped in place in the scheduler's handler table (found
as the eventHandlers upvalue of the scheduler's AddEventHandler): same key, same order, the handle stays valid. If
that table can't be found (another wrapper got there first), every handler is wrapped and decides per call.
Receives. An event "arrived over the network" when source is set (not nil or ''). received is counted
inside the wrappers, without an extra handler: a per-name arrival number is bumped by the first wrapper that runs for
an arrival, and the others join it. So it counts once per arrival, however many handlers there are, as long as the
resource has one Lua function handler for the name.
Handler time. For arrivals from the network the wrapper measures the run with os.nanotime (falling back to
os.clock), keeps the total and the slowest run, and passes the handler's results straight through (nil holes
included, no table). Errors are not caught. Wrappers are kept in a weak table so a callback is never wrapped twice
(the scheduler's RegisterNetEvent calls the global AddEventHandler, which is the shim's).
Flush. Counts live in a local stats table, at most 128 names plus __other__. A thread swaps the table every
np_net_flush_ms and delivers the delta with the resource's Lua memory (collectgarbage('count')):
| Side | Delivery |
|---|---|
| server | exports.np_net:push(resource, delta) in a pcall; np_net takes the resource name from GetInvokingResource() |
| client | TriggerEvent('np_net:client:push', resource, delta), a local event np_net's client batches |
Nothing is delivered while np_net isn't started.
Server ​
- Collector.
pushmerges server deltas as trusted (shape still checked).np_net:reportfrom clients goes through the rate limit (every attempt starts the gap) andvalidateReport, then merges asside="client". Client Lua memory is kept per player and averaged; a player's entries are dropped onplayerDropped. - Series. One table per
side×resource×eventandbag×key, capped bynp_net_max_seriesin three budgets (server events, client events, state bag keys). Label values that aren't valid UTF-8 are rendered with U+FFFD. - Threads. Players and ping every 5 s; the tick loop (
Wait(0),os.nanotime) whileConfig.Tickis on, else a 1 s check. - Endpoint.
SetHttpHandlerserves/metrics(so/np_net/metricsfrom outside) forGETandHEAD. Order of checks: path → method (405) → token set (503) → IP list (403) → bearer token (401, constant-time compare). - Patching.
patchManifestinserts the line after the last header directive (fx_version,game(s),lua54…) that comes before the first script directive, and handles header blocks in{ }.unpatchManifestremoves only lines that end with the-- np_netmarker.
Client ​
client/main.lua merges every np_net:client:push into one pending table (max 64 resources, 129 events each) and
sends it every ClientReportMs with TriggerLatentServerEvent at 25 kB/s, so a report never adds to the reliable
buffer it is measuring. The first report waits a random half to full interval. With the interval at 0 the pending
data is dropped.
Tests ​
tests/run.sh # everything
VERBOSE=1 tests/run.sh # with the resource's log lines
Plain Lua 5.4, no FiveM needed. LUA and LUAC pick other binaries.
| Step | |
|---|---|
| syntax | luac -p on every Lua file (outside monitoring/) |
| shim | tests/test_shim.lua: byte counting, fallbacks, broadcasts, handler wrapping (args, source, returns, errors), flush, the 128-name cap |
| server | tests/test_server.lua: collector, report validation, drop categories, Prometheus text, auth, commands, patching, tick, state bags and the np_admin integration against a fake np_admin |
| client | tests/test_client.lua: the whole path, shimmed client resources → np_net client → one report → server → /metrics, plus a shimmed server resource pushing through the export, and the FPS in the report reaching the low FPS heatmap |
| heatmap | tests/test_heatmap.lua: the grid, caps, day files, rollover and pruning, restart, merging, queries, the sampler, drops, deaths, low FPS, AddHeat, the /heatmap routes and tokens, Prometheus counters and the console |
tests/harness.lua gives every FiveM Lua state its own environment table, so the shim loads into several resources
side by side the way FiveM does it. Shared between states: a cooperative scheduler with a virtual clock, exports,
resource states and a small network for client → server events. It boots np_net's files in manifest order.