Skip to main content

Listener

The listener module wraps FiveM event registration and validates arguments before your callback runs. It works with schemas from the validator module.
local listener = sure.getModule('listener')
local v = sure.getModule('validator')

Local events

listener
  :on('inventory:useItem', function(itemName, amount)
    print(('Use %sx %s'):format(amount, itemName))
  end)
  :expect(
    v.string().required(),
    v.integer().min(1)
  )

Network events

listener
  :onNet('shop:buy', function(itemName, amount)
    -- This callback only runs when both arguments pass validation.
  end)
  :expect(
    v.string().required(),
    v.integer().between(1, 100)
  )

API

on(eventName, callback)
function
required
Registers a local event through AddEventHandler.
onNet(eventName, callback)
function
required
Registers a network event through RegisterNetEvent.
expect(...validators)
function
Attaches validators to positional event arguments. Argument 1 is checked by validator 1, argument 2 by validator 2, and so on.
listenerMeta
table
on and onNet return a metadata table with expect(...) so the validation contract can be chained close to the callback.
A failed parameter validation prints an error and raises. Keep validators strict for trusted internal events and wrap external flows with server-side permission checks where needed.

Pattern

client.lua
listener
  :onNet('sure:notify', function(message)
    lib.notify({ description = message })
  end)
  :expect(v.string().required())