One network layer per platform, one client per service
Context and problem
The apps are about to talk to two services: the app-status feed on homefelt.io (#231, #232) and,
with online play, the game server on api.homefelt.io (sign-in, tables and invites over HTTP, a
table over a WebSocket). The web already calls fetch and new WebSocket from several classes, each
building its own URLs and headers. The feed and the server want opposite things: the feed must never
send anything about the player, never retry, and fail open; the server needs a bearer token, a single
refresh, and reconnects. One shared client with options for everything would mix those rules.
Options considered
- One client for everything, configured per call. Little code, but every call site has to get timeouts, headers and sign-in right, and a feed request can pick up a token by mistake.
- One network layer per platform, one client per service. The layer does what every request shares; each client fixes its own service’s rules once.
- A third-party HTTP stack per platform (Alamofire, Retrofit). Typed REST, but more dependencies, and WebSockets still go through URLSession and OkHttp.
Decision
Eduardo chose option 2 on 2026-11-25, with URLSession on iPhone (no third-party library),
OkHttp on Android, and fetch and WebSocket on the web.
The layer (Swift HomefeltNet, Kotlin kotlin/net, web web/src/net) owns:
- a
Transportinterface the clients talk to, and a fake of it, so no test touches the network; - per-request timeouts and a response size cap that stops reading at the cap;
- the client header (
platform,app, date version, 0013), which each client chooses to send or not; - mapping every failure to a few typed errors (
NetError):offline,timeout,tooLarge,status(code),invalid.
Platform rules:
- Android: the app creates one
OkHttpClient, and each service client derives its own withnewBuilder()(its own timeouts and interceptors), so all of them share one connection pool and dispatcher. Never oneOkHttpClientper request or per call site. - iPhone: never
URLSession.shared. Each service client gets its ownURLSessionConfigurationandURLSession, created once per client. The feed’s is.ephemeral, with no cookies, no credential or URL cache storage (urlCache = nil,httpCookieStorage = nil),waitsForConnectivityfalse, and its own timeouts. - Web: one module wraps
fetch(credentials: "omit"unless a client opts in) andWebSocket.
The clients, the same names on all three platforms:
| Client | Service | Rules |
|---|---|---|
StatusClient |
the feed at https://homefelt.io/app-status/v1.json |
No sign-in, no cookies, no client header: only User-Agent: Homefelt. 3 s to connect and 5 s for the whole request, the body’s read included (iOS timeoutIntervalForResource, OkHttp callTimeout, the web’s AbortSignal kept until the body is read); a 64 KiB cap that stops the read; If-None-Match with the cached ETag; no retries. Any failure fails open: the cached feed, or no messages |
AuthClient |
sign-in and refresh at api.homefelt.io |
No timeout and no automatic retry: an aborted /auth/refresh can rotate the token on the server while the client never sees the new one, and the next refresh then looks like token reuse and signs the whole family out. A refresh is single-flight: concurrent callers share one request |
ServerClient |
tables, invites and the account at api.homefelt.io |
Bearer access token; on a 401 it refreshes once through AuthClient and retries once, never more. GETs time out after 15 s. No timeout on non-idempotent POSTs (create a table, redeem an invite): an abort followed by the person trying again could do it twice |
GameSocket |
the table WebSocket | The hello with client (#233), pings, reconnect with backoff and jitter, the server’s close codes (4401, 4426, 4428) |
The feed has two pieces on every platform, with the same names: StatusClient fetches, keeps
the last good body (FeedCache) and spaces fetches 15 minutes apart; StatusStore holds the feed,
the dismissals and the previous launch’s version, and runs select. Swift HomefeltNet, Kotlin
kotlin/net, web web/src/status/statusClient.ts and statusStore.ts.
Only StatusClient is built now (#231 on the apps, #232 on the web). The others are built when the
apps get online play; the web’s existing Auth, table and socket code moves onto this shape in #232.
Consequences
- One new dependency, OkHttp, on Android only; Dependabot’s Gradle entry covers it.
- The feed request carries nothing that identifies the player or the device, by construction: its client never adds the client header, a cookie or a token.
- Tests swap the transport for a fake, as the cores take seeded randomness.
- The cores (
CanastaCore,kotlin/core,canasta-rules,HomefeltStatus) keep no network code.
When we’d reconsider
- A library on top of the layer, such as Ktor on Android, typed REST clients generated from an OpenAPI description, or HTTP/3 where the platform stack lacks it, once one of these is cheaper than what we keep by hand.
- Hand-written clients becoming real work: more services or endpoints than a few files per platform hold comfortably, or the three platforms drifting apart in ways the tests do not catch.
A switch stays behind each platform’s layer: the clients talk to Transport, so swapping what sits
under it, or generating a client, does not touch the screens or the cores.