9. Talking to a server¶
So far the bookings have lived and died with the page: reload it and they are gone. A real workshop keeps them on a server, where every screen that opens finds them. This page fetches the board from one, sends it the new bookings, and shows what it says when it refuses one.
The examples talk to a small workshop server that lives inside this documentation, so they work as they are. It answers at three addresses, as a server written for the app would:
Request |
What it does |
|---|---|
|
gives every booking: |
|
keeps a new booking and gives it back with its |
|
removes booking 2 |
Each example starts with the same two bookings, and each answer takes a moment, as over a real network.
Fetching the board¶
fetch, which the first tutorial used to ask a supplier for parts, works the same in an app. JSON comes back
as Trebol values, a list of bookings as a block of maps:
view [
title "Workshop"
global table board "Loading…" [customer "Customer" bike "Bike" due "Due back"]
]
bookings: fetch "/api/bookings"
foreach b bookings [b/due: to date! b/due]
board/data: bookings
The view shows first, so the screen is there at once; while fetch waits for the server, the app goes on
answering the person. When the answer comes, the table gets its rows.
JSON has no dates: the server sends the day as text, "2026-11-11", and to date! makes it a date again,
which the table shows in the format of the language. On the way out, Trebol does the opposite by itself.
When the server cannot be reached¶
A server may be down, or slow, or give an error. For fetch all of them are errors, and try catches them,
so the app can say so instead of stopping. The second button asks for an address the server does not have:
bookings: copy []
load-board: func [address] [
either error? try [bookings: fetch address] [
toast/role "The workshop cannot be reached" 'danger
] [
foreach b bookings [b/due: to date! b/due]
board/data: bookings
]
]
view [
row [
title "Workshop"
button #refresh "Refresh" [load-board "/api/bookings"]
button "Wrong address" [load-board "/api/bookngs"]
]
global table board "Nothing booked yet" [customer "Customer" bike "Bike" due "Due back"]
]
load-board "/api/bookings"
Sending a booking¶
fetch/with and a body send the body to the address. A map, like the values of a form, goes as JSON, and
the server answers with the booking it kept, now with its id:
bookings: fetch "/api/bookings"
foreach b bookings [b/due: to date! b/due]
view [
title "Book a repair"
form [
field customer "Customer" required
choice bike "Bike" ["Road" "Mountain" "City" "Child's"] required
date due "Due back" required
submit "Book in" [
either error? try [b: fetch/with "/api/bookings" [body: values]] [
toast/role "The booking was not sent. Try again." 'danger
] [
b/due: to date! b/due
append bookings b
board/data: bookings
set-values face/form none
toast/role "Booked in." 'success
]
]
]
global table board [customer "Customer" bike "Bike" due "Due back"]
]
board/data: bookings
The table shows what the server kept, not what was typed: the server has the last word, and the id it gave
is what the app uses to name the booking from now on.
The options block of fetch/with is an object of its own, so [body: body] would read the option it is
making, not a word body of the app: give that word another name, as values here, or see
fetch.
When the server says no¶
Some rules only the server knows. This workshop is closed on Sundays, and a form cannot know which days a workshop opens. The server refuses the booking with status 422 and a reason for each field it did not accept:
{"errors": {"due": "The workshop is closed on Sundays"}}
With full: true, fetch does not turn an answer like that into an error: it gives an object with the
status, the headers and the body of the answer, so the app can read the reasons. field-error shows
each under its field, where the person looks, until they change it:
view [
title "Book a repair"
form [
field customer "Customer" required
choice bike "Bike" ["Road" "Mountain" "City" "Child's"] required
date due "Due back" required
submit "Book in" [
answer: fetch/with "/api/bookings" [body: values full: true]
either answer/status = 201 [
set-values face/form none
toast/role "Booked in." 'success
] [
foreach [name reason] answer/body/errors [field-error face/form name reason]
]
]
]
]
Try a Sunday: 15/11/2026. The form checks what it can by itself, so the person hears about an empty field at once; the server checks the rest. Each says it in the same place and the same way.
Removing a booking¶
A request with method: 'DELETE removes the booking whose id is in the address. Here a booking is chosen in
the table, and the button removes it from the server first and from the board after:
bookings: fetch "/api/bookings"
foreach b bookings [b/due: to date! b/due]
view [
row [
title "Workshop"
global remove-button: button danger #trash "Remove" disabled [
b: board/selected
either error? try [fetch/with rejoin ["/api/bookings/" b/id] [method: 'DELETE]] [
toast/role "The booking was not removed" 'danger
] [
remove find/same bookings b
board/data: bookings
remove-button/disabled: true
toast rejoin [b/customer "'s booking was removed"]
]
]
]
global table board "Nothing booked yet" [customer "Customer" bike "Bike" due "Due back"]
on-select [remove-button/disabled: false]
]
board/data: bookings
find/same finds the very booking that was chosen, not another one that looks like it.
A file from the server¶
Some files only the server can make, like a report as a PDF. download takes the answer of
fetch/with [... full: true] and gives the user its body as a file, without leaving the page. What is asked
goes in the body of a POST, so it shows neither in the address nor in the server’s logs:
answer: fetch/with "/api/reports" [method: 'POST body: #(year: 2026) full: true]
either answer/status < 400 [
download %report.pdf answer
] [
toast/role "The report could not be made" 'danger
]
The name comes from the server, when its answer says one in content-disposition (accents too); otherwise
it is the one given, here report.pdf. download gives back the name the file was saved under. An answer
with a status of 400 or more is an error, so the app looks at the status first, as above.
How far it went¶
A photo of the bike, or a long report, takes a while to go up or come down. progress: shows how far the
request went. Given an upload field, its own bar fills as the files go up and goes away when they are there;
given a progress face, that bar fills; given a block, the block runs as the bytes go, with done and total
set:
view [
title "Bike photos"
global upload photos "Photos of the bike" multiple accept ["image/*"]
row right [
button primary "Send" [
fetch/with "/api/photos" [
multipart: collect [foreach file photos/value [keep reduce ['photo file/file]]]
progress: photos
]
toast/role "The photos are with the shop." 'success
]
]
]
A photo field sends the same way, and its bar fills too. Its value is an object only when the person chose
a new image; otherwise it is the address the server already has, or none. In the submit of a form with
pic: photo picture "Photo":
; docs-test: skip
if object? values/picture [
fetch/with "/api/customers/42/picture" [multipart: reduce ['picture values/picture/file] progress: pic]
]
With a body, the count is first of the bytes sent and then of the bytes received; without one, of the bytes
received. total is none when the server does not say how many bytes come, and then a progress face moves
without saying how far. The block runs at most ten times a second, and always once at the end, when total
is known: done and total are then the bytes that came.
Answers kept¶
A board many people change wants to show at once what it had, and the latest a moment later. cached gives
the last answer it got for an address, at once, and asks the server again behind; is puts it in the table,
and when the new answer comes the table follows by itself:
view [
title "Workshop"
table is [cached "/api/bookings"] [customer "Customer" bike "Bike" due "Due back"]
text muted is [either b: cached "/api/bookings" [rejoin [length? b " bookings"]] ["Loading…"]]
button "Refresh" [refresh "/api/bookings"]
]
The first time there is no answer yet, none, and the table shows empty for a moment. After that, coming
back to the screen shows the board as it was, and asks again. It also asks again when the reader comes back
to the tab from another one, and after the app sends a booking or removes one (a fetch with 'POST,
'PUT, 'PATCH or 'DELETE to /api/bookings or an address under it). refresh asks now, for a button.
News from the server¶
cached asks again when the reader does something; a board on the wall of the workshop wants to follow by
itself, when a mechanic on another screen books a repair. The server says so on a stream of events, and the
board asks again for what it shows:
news: open-events "/api/events"
on-message news [refresh "/api/bookings"]
on-open news [if reconnected [refresh "/api/bookings"]]
view [
title "Workshop"
table is [cached "/api/bookings"] [customer "Customer" bike "Bike" due "Due back"]
]
The server sends an event each time the bookings change, and every face that reads cached "/api/bookings"
follows. A stream that drops, because the server restarted or the network went away for a moment, opens again
by itself and goes on with the same handlers; on-open runs again then, with reconnected true, and the
board asks for what it may have missed. An event’s event field comes as the word event, so one stream can
carry news of many kinds: if event = 'booking [...]. The cookie of the login goes with the stream, as with
fetch; open-events/with takes headers for a server that wants another sign, and the other options in
the reference.
The login does not last forever. When it runs out, the server answers the stream with 401 and the stream
stays closed: asking again would get the same answer. on-close runs then, with the status in value; the
board signs in again and opens a new stream, which is a new handle, so it sets the handlers again. Doing it
all in one function, which calls itself, keeps them in one place; the last id goes with the new stream as
last-id, for the server to send the news the board missed:
last-seen: none
listen: does [
news: open-events/with "/api/events" [last-id: last-seen]
on-message news [
last-seen: any [id last-seen]
refresh "/api/bookings"
]
on-close news [
if find [401 403] value [
if sign-in-again [listen]
]
]
]
listen
sign-in-again is the board’s own: it asks the server for a new login and gives true when it got one. When
it gives false, the board does not open the stream again, or it would ask forever; it shows the login screen
instead.
Your own server, while you write the app¶
The examples here talk to a server inside the documentation. Your app talks to yours, written in whatever
language you like, and once published both usually sit behind one site, so /api/bookings reaches it. While
you write the app, trebol dev can stand in for that site: --proxy sends every address under a prefix to
your server and passes its answers back.
npx trebol dev --proxy /api=http://localhost:8080
Now /api/bookings goes to http://localhost:8080/api/bookings, with its method, body, headers and cookies,
and the app keeps writing fetch "/api/bookings" as it will in production: one origin, so no CORS to set
up. Event streams come through as the server sends them, so open-events hears each piece of news at once,
and so do WebSockets opened under the prefix. Repeat --proxy for more prefixes; an address goes to the
longest prefix it falls under, and /apis is not under /api. When your server is not running, the request
answers 502 and says so, cannot reach http://localhost:8080 for /api (ECONNREFUSED), which the app reads
as any other failed request.
What’s next¶
The bookings live on a server now. The next page draws: the shop’s logo, and its takings as charts.