Phone apps
An app is one JavaScript file your resource ships. The phone loads it and draws it, and your server answers it with SecureCallbacks, so it has WHIP's checks as the built-in apps do. This page builds a small one; whip_phone/docs/MODULES.md is the full reference, with every field, the UI kit, apps drawn with React or three.js, and scoped CSS.
The example is a resource named mycars: a My Cars app listing the player's cars, which marks one that's out on their map.
1. The resource
fx_version 'cerulean'
game 'gta5'
lua54 'yes'
dependency 'whip_core'
shared_script '@whip_core/lib/satellite.lua'
server_script 'server.lua'
client_script 'client.lua'
files { 'html/phone_app.js' } -- a file not listed never reaches playersThere's no dependency 'whip_phone': Whip.registerWith below adds the app whenever whip_phone runs, so mycars starts with or without the phone.
2. Register the app (client)
-- client.lua
Whip.registerWith('whip_phone', function() -- now, and again each time whip_phone starts
local ok, why = exports.whip_phone:RegisterModule({ id = 'mycars', script = 'html/phone_app.js' })
if not ok then print('[mycars] no phone app: ' .. tostring(why)) end
end)
RegisterNetEvent('mycars:waypoint', function(x, y)
SetNewWaypoint(x, y)
end)The app's requests use your resource's name, so app.request('list') asks the SecureCallback mycars:list. The app goes when your resource stops.
3. The app
// html/phone_app.js
(() => {
const { kit: K, icons: I } = window.WhipPhone;
WhipPhone.module({
id: 'mycars', name: 'My Cars', version: '1.0.0', icon: I.pin,
state: () => ({ cars: [], loaded: false }),
async open(app) {
app.state.cars = await app.request('list').catch(() => []); // asks mycars:list
app.state.loaded = true;
},
render(app) {
const s = app.state;
const rows = s.cars.map((c) => K.row({
label: c.name, sub: c.plate, value: c.out ? 'Out' : 'Stored',
act: c.out ? 'find' : '', hint: c.out ? 'Find' : '', d: { id: c.id },
}));
const body = rows.length ? K.list(rows) : K.empty({ title: s.loaded ? 'No cars' : 'Loading…' });
return K.screen('list', K.title('My Cars') + K.scroll(body));
},
actions: {
async find(app, d) {
try {
await app.request('find', { id: Number(d.id) });
app.toast('Marked on your map');
} catch (err) {
app.toast(err.reason || 'Not found');
}
},
},
});
})();render returns HTML from the kit's builders, which escape the text you give them, and the phone draws it again after every action and request. A row with act runs that action with its d values (as strings), from the mouse or the controller, and hint names the A button. A refused request rejects with err.reason.
4. Answer it (server)
-- server.lua
-- Name args in params, or the handler never gets what the app sent
local REQUEST = { rateLimit = 1000, params = { { 'args', 'any' } } }
Whip.register(function()
Whip.core:SecureCallback('mycars:list', REQUEST, function(source)
local out = {}
for _, car in ipairs(Whip.core:GetVehiclesOf(source) or {}) do
out[#out + 1] = {
id = car.vehicleId, name = car.displayName or car.model,
plate = car.plate, out = car.state == 'out',
}
end
return out
end)
Whip.core:SecureCallback('mycars:find', REQUEST, function(source, player, args)
local car = Whip.core:GetVehicle(tonumber(type(args) == 'table' and args.id))
if type(car) ~= 'table' or car.owner.charId ~= player.charId then
return false, 'Not your car'
end
if car.state ~= 'out' or not car.coords then return false, 'It is in a garage' end
TriggerClientEvent('mycars:waypoint', source, car.coords.x, car.coords.y)
return true
end)
end)'any' lets whip_core pass the table through: check each field you read, and that what it names is theirs, as mycars:find checks the owner.What an app can do
| To | Use |
|---|---|
| Ask your server | app.request(action, args), a Promise of the answer |
| Tell the player | app.toast(text), or app.notify({ title, text, kind, args }) for a capsule that opens your app with args |
| Hear your server | on: { changed(app, data) { … } }, run by Push(source, 'mycars', 'changed', data) |
| Show a screen with Back | Keep it in app.state, give K.screen its own key, and return true from back(app) when you close it |
| Put a number on the icon | badge(app) |
| Use the controller's X | x(app), returning { label, run } or null |
| Open another app | app.open('messages', { number }) |
| Show a picture or a font from your resource | app.asset('img/car.png'). A bare path points at whip_phone's page |
| Draw it with React, Vue or three.js | mount(app, el, view) in place of render: Mount mode in MODULES.md |
| Give an answer back to a script | app.done(result), when RunApp opened it |
Rules
- An app's id is lowercase letters, digits,
_or-, up to 32 characters. A built-in's id is refused, and so is one another running resource registered; registering your own again replaces it. - Anything with
data-actis clickable and reached by the controller. Use your own action names, neveros:ones. - Keep
renderfree of changes to state: the phone renders every display at once. - No iframes, since the fold animation photographs the screen. Draw your own page in mount mode instead.
- An app that throws shows an error screen and never takes the phone down.
Trying it in a browser
Serve whip_phone's html folder (for example python -m http.server inside it) and open preview.html: it runs the real phone on html/dev/mock.js, with a bar that fires calls, texts and notifications. Add your app to preview.html and answer its requests in mock.js, as the example Garage app (html/dev/example/app.js) does.