Network¶
Asking web servers, and talking with them over WebSocket and server-sent events. fetch reads JSON as Trebol
values. See tasks and the network and
talking to a server.
fetch¶
Requests a URL. JSON answers come back as maps and blocks.
fetch url
url(url!string!)/with options(block!object!): method, headers, body, form, multipart, timeout, full
What comes back follows the type the server gives: JSON as Trebol values, text (text/..., XML such as
application/xml or image/svg+xml, JavaScript, form data) as a string, and any other file as binary!, an
Excel file among them although its type has xml in its name.
The options of /with, a block or an object:
Option |
What it does |
|---|---|
|
the HTTP method, |
|
a map, object or block of names and values added to the URL after |
|
a block or map of header names and values |
|
what to send: a string or binary as it is, any other value as JSON |
|
a block or map sent as a web form ( |
|
a block of names and values sent as |
|
|
|
an upload field or a |
|
how long to wait for the answer, a |
|
when true, |
A network failure, a timeout and, without full, an answer with an HTTP error are access errors, which
try catches.
The block of options is made into an object of its own, as make object! makes one, so each option it sets
is a new word of that object, and a word of the same name to the right reads that new word, not the
program’s: in fetch/with url [body: body] the second body is the option itself, which has no value yet,
and fetch fails with body has no value. Give the program’s word another name, or put its value in
with compose:
order: #(bike: "Road" due: 2026-11-20)
fetch/with "/api/bookings" [body: order]
body: #(bike: "Road" due: 2026-11-20)
fetch/with "/api/bookings" compose/only [body: (body)]
cached¶
The last answer to a GET of url, at once (none the first time), and asks again behind: a reaction (is, react) that reads it runs again when the answer comes.
cached url
url(url!string!)
For a face that shows what a server has, with is or react: table is [cached "/api/bookings"] [...]. The
first time it gives none, the face shows what it shows empty, and the answer fills it when it comes; after
that it gives the answer it has at once, and asks again behind, and the face changes only if the answer did.
It asks again for a url when a screen is entered and reads it, when the page comes back into view (the
reader went to another tab and came back), after a fetch with method 'POST, 'PUT, 'PATCH or
'DELETE to the same path or one above or under it (/api/bookings/7 for /api/bookings), and on refresh.
A request that fails keeps the last answer, says why in the console, and is asked again the next time.
A reaction whose url is written out, cached "/api/bookings", runs again when that url’s answer comes; one
whose url is computed runs again when any answer comes.
view [
title "Workshop"
table is [cached "/api/bookings"] [customer "Customer" bike "Bike"]
text is [either b: cached "/api/bookings" [rejoin [length? b " bookings"]] ["Loading…"]]
]
refresh¶
Asks again for an answer kept by cached, now; reactions that read it run again when it comes.
refresh url
url(url!string!)
Asks again, now, for a url that cached keeps, and gives nothing back; when the answer comes, the reactions
that read cached of that url run again, and a face that shows it changes if the answer did. A program calls
it when it knows the server has something new, as when a stream of events says so (see open-events):
on-message news [refresh "/api/status"]
A reaction that asks again for a url it reads itself (refresh, or a post to its path) does not run again for
an answer that it alone asked for, or it would ask again for ever. Any other answer runs it again: the first
one, asked by its own read of cached, and one that a handler, a timer or another task asks for, even while
the reaction waits. A task the reaction starts with spawn is another task: what it asks for is not the
reaction’s own.
Two reactions that each refresh a url the other reads run each other again for as long as the page is open, one fetch at a time, as a timer that sets itself again would: that is what the program asks for. To stop, make one of them refresh only when something it shows has changed.
show-progress¶
Shows how far a request went on a face: a progress bar’s value, or the bar of an upload or a photo.
show-progress face done total
face(object!)done(integer!none!): Bytes done; none once the request is overtotal(integer!none!): Bytes in all, when known
What fetch/with [progress: face] calls as the request goes; a program does not need to. A progress face
gets the part done as its value, from 0 to 1, or none while the total is unknown; an upload gets it as its
sent, which shows a bar under its files, and none once the request is over, which hides it.
http-request¶
Sends an HTTP request; used by fetch.
http-request url options
url(url!string!)options(object!none!)
What fetch calls with its options as an object, or none; a program calls fetch.
open-socket¶
Opens a WebSocket; gives a handle once it is open.
open-socket url
url(url!string!)
Opens a WebSocket to a ws:// or wss:// url and waits until it is open; a url that cannot be opened is an
access error. The handle it gives takes on-message, on-close and on-error, send sends on it, and
close-stream (or close) hangs up. A text message comes to on-message in value, loaded as JSON when it
is a JSON object or array; a binary message comes as binary!. send sends a string or binary as it is and
any other value as JSON. on-close gets the close code in value. A socket that drops is not opened again.
desk: open-socket ws://localhost:8089/desk
on-message desk [print value/stock]
send desk #(ask: "bell")
open-events¶
Opens a stream of server-sent events; gives a handle. It opens again by itself when it fails or the server ends it.
open-events url
url(url!string!)/with options(block!object!): headers, query, cookies, reconnect, last-id
Opens a stream of server-sent events (text/event-stream) and gives a handle at once; the stream opens behind.
Each event comes to on-message with three words:
Word |
What it has |
|---|---|
|
the event’s data: a JSON object or array as a map or block, anything else as a string |
|
the event’s |
|
the last |
A stream that fails or that the server ends opens again by itself on the same handle, so the handlers set on
it go on working, as a browser’s EventSource does. Before each new attempt it waits: the retry: the server
sent, in milliseconds, or else one second that doubles up to 30 seconds, and goes back to one after a stream
that delivered an event. The browser coming back online ends the wait at once. Each new request sends the
last id in a Last-Event-ID header, for the server to send what came after it.
on-openruns each time the stream is open, withreconnectedfalse the first time and true after.on-errorruns for each attempt that fails, with the error invalue,attempt(1 for the first that failed since the last event) and the HTTPstatus, ornonewhen the network failed; it is not the end.An answer of 204, or of 4xx such as 401 when the user is no longer signed in, stops the stream: the server does not want it. A 4xx runs
on-errorfirst; thenon-closeruns, with the status invalue.on-closeruns, too, when the program callsclose-stream, and, withreconnect: no, when the server ends the stream (valueisnonefor both).
The options of /with, a block or an object, are those of fetch that apply, and two of its own. As in
fetch, the block is an object of its own: in [headers: headers] the second headers is the option, not
the program’s word, so give that word another name:
Option |
What it does |
|---|---|
|
a block or map of header names and values, as an |
|
a map, object or block of names and values added to the URL after |
|
|
|
|
|
an event id to send as |
news: open-events/with "/api/events" [headers: [Authorization "Bearer 5f2a"]]
on-open news [if reconnected [refresh "/api/status"]]
on-message news [if event = 'window [refresh "/api/status"]]
on-error news [print ["stream down, attempt" attempt]]
A stream the server refused stays closed: to go on, the program signs in again and opens a new one, which
is a new handle, so it sets its handlers again. A function that opens the stream and, on a 401 or 403, calls
itself does both; keeping the last id and giving it as last-id lets the server send what was missed.
When signing in again fails too, the program should not open the stream again, or it asks forever.
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/status"]
on-close news [
if find [401 403] value [
if renew-token [listen] ; renew-token: the app's own, true when signed in again
]
]
]
listen
events-request¶
Opens a stream of server-sent events; used by open-events.
events-request url options
url(url!string!)options(object!none!)
What open-events calls with its options as an object, or none; a program calls open-events.
on-message¶
Runs a block for each message that comes on a stream.
on-message stream body
stream(handle!)body(block!)
Sets a block to run for each message of a socket or stream of events, as a task of its own; the message is in
value, and for a stream of events its event and id too (see open-events). A stream may have more than
one; messages that came before the first was set run it when it is set. Gives the handle.
on-open¶
Runs a block when a stream of events is open, the first time and after each reconnection (reconnected).
on-open stream body
stream(handle!)body(block!)
Sets a block to run each time a stream of events is open: the first time, with reconnected false, and after
each time it opened again, with reconnected true, when a program asks again for what it may have missed.
on-close¶
Runs a block when a stream closes.
on-close stream body
stream(handle!)body(block!)
Sets a block to run when a socket or stream closes. For a socket, value is the close code. A stream of
events closes only when the program calls close-stream, when the server refuses it (204 or 4xx, value is
the status) or, with reconnect: no, when the server ends it.
on-error¶
Runs a block when a stream fails; a stream of events tries again, with attempt and status.
on-error stream body
stream(handle!)body(block!)
Sets a block to run when a socket or stream fails, with the error in value. A stream of events goes on
trying: the block runs for each attempt that fails, with attempt, the count, and status, the HTTP status or
none.
close-stream¶
Closes a socket or a stream of events.
close-stream stream
stream(handle!)
Closes a socket or a stream of events for good: a stream of events is not opened again, and its on-close
runs. close does the same with a handle (and hides a dialog); a socket takes send to send a value on it, as
open-socket says.
read-file¶
The bytes of a file the user chose.
read-file file
file(handle!)