DEV Community

Cover image for How ABS+ keeps your Audiobookshelf listening position in sync, even offline
borodutch.eth 💎🤲
borodutch.eth 💎🤲

Posted on Edited on Originally published at blog.absplus.app

How ABS+ keeps your Audiobookshelf listening position in sync, even offline

ABS+ writes your Audiobookshelf listening position to local phone storage first and syncs it to your server afterwards. That way a chapter you finish in a subway tunnel or on a flight won't vanish, and it won't stomp on newer progress from another device once you're back online. Every play, pause and finish hits disk before any network call goes out, the app reads what the server already has before writing, and failed requests back off from one second up to five minutes with a schedule that survives an app kill. Here's how the sync code in the repo works as of November 9, 2026 (untouched since the 1.2.4 release on October 28) and why I built each piece that way.

Why syncing a listening position is harder than it looks

Audiobookshelf keeps one progress record per book or podcast episode on your server: current time, duration, a finished flag and a lastUpdate timestamp. Pushing updates with a PATCH to /api/me/progress/<item> on a naive timer sounds easy until you actually listen to audiobooks in the real world. Trains lose signal, phones die mid-chapter, and a second phone sits paused on yesterday's bookmark.

Three constraints forced me to build a real replay queue instead of a one-liner. Your server can stay unreachable for hours while you listen to downloaded files. Audiobookshelf doesn't support conditional writes, so the client can't ask it to update only if nobody else changed the row; avoiding overwrites means reading first. And a PATCH returns without sending the saved progress row back, so the app has to fetch it again just to learn the server's own timestamp.

Step 1: write to the phone before anything touches the network

On Android, the player calls record on play, pause, book end and every 20 seconds during playback. record in ProgressSync.kt runs synchronously: it builds the new position, flushes it to preferences with a blocking commit() instead of async apply(), and kicks off the background worker only after the write lands. If local storage fails, Android throws an error ("Could not save listening progress") rather than pretending it worked, and iOS pops the same text as a toast. The iPhone side in Progress.swift does the exact same thing inside push: encode the snapshot, write it atomically to disk, and start the replay task only if that write succeeds.

The journal stores a single entry per server, account, recipient and title, holding only the latest position. Listening offline for an hour doesn't stack up 180 queued updates to replay later; you end up with one entry saying where you stopped. It never stores passwords or tokens, only the position, timestamps and which account owns it.

Timestamps are strictly monotonic: each event takes the greater of the phone's clock and one millisecond past the previous timestamp for that title. The first server read is saved too, so even if your phone's clock lags behind your server, the next offline event still sorts after what the server already has.

Step 2: ask the server first, and let a newer position win

Before sending anything, the worker fetches the server's current record for that title. If the server's lastUpdate is newer than the phone's, you clearly listened somewhere else after this local event happened, so the server wins: the phone takes the remote position and drops its pending entry. I picked last-write-wins by timestamp on purpose because it matches how people actually use two devices. Whichever one you listened on last decides where playback resumes.

One edge case took the most tests to nail down. Say the app sends a position and the connection drops before the response comes back: the server saved the write, while the phone thinks it failed. On the next retry, the phone reads a newer timestamp from the server and could mistake its own write for a conflict from another device. To prevent that, the app persists the exact payload it's about to send (sent) before making the request. If the server's record matches that saved payload, the app recognizes its own write, adopts the server's timestamp as an acknowledgement and moves on. The code comment is blunt about the one hole left: because Audiobookshelf has no conditional writes, a client can't make the gap between its read and its write atomic if another device writes in that exact window.

Once a PATCH succeeds, the worker fetches the record again to get the server's timestamp and marks the entry delivered. If you kept listening and created a newer local event while that request was in flight, the entry stays pending for the next pass.

Step 3: retry with a delay that survives a restart

Failed sends stay pending and get a retry timestamp. Android backs off one second, then two, four, eight and so on up to 256 seconds, locking at five minutes from the tenth failure onward. iOS follows the same curve, min(300, 2^attempts) seconds, with attempts capped at 10. That retry time lives in the persisted journal instead of RAM, so the OS killing the app won't wipe the backoff. Launching the app wakes the worker right away without waiting for you to press play (and on iOS, foregrounding the app or regaining connectivity wakes it too). automaticStartupDrainsPersistedQueueWithoutPlayback tests exactly this: it queues a position, boots sync with nothing playing, and checks that the queue drains by itself.

Step 4: a finished book stays finished

A comment in the iOS code notes a nasty Audiobookshelf quirk: sending an explicit isFinished: false tells the server to mark the book unread and throw away the position in the same payload. So ABS+ omits that field unless the title is actually finished. Playback has a second trap right after the final chapter ends. The player can fire late passive callbacks like a pause or a restore at position zero, and Audiobookshelf clears the finished state as soon as the position moves back from the end. ABS+ ignores those passive callbacks once a book is done and starts a re-read only when you genuinely hit play again. passiveCompletionSurvivesButDeliberateOfflineRereadResumesAfterRestart verifies the whole flow: it finishes a book and a podcast episode offline, fires late zero-position callbacks, confirms the title stays at 100 percent, then plays 24 seconds and checks that the server ends up at 24 seconds with the finished flag cleared.

Linked accounts and the resume choice

If you link multiple accounts on the same server for a title, ABS+ tracks a separate journal entry for each recipient and acknowledges or retries them independently. The offline multi-title replay test plays two titles across two linked recipients, expects four pending entries and verifies all four land once connectivity returns. When you open a title, ABS+ resumes from your own account's newest position and only offers a linked account's spot if it's newer and more than 30 seconds apart. When you're offline, it skips the server check and plays straight from local storage.

What I would tell someone building this

  1. Flush every event to disk before touching the network. The rest of the sync logic gets much simpler once that order is guaranteed.
  2. Keep one latest position per title instead of an event log. Sync only needs to land where the listener stopped.
  3. Persist the payload you're about to send so a dropped response never tricks you into thinking another device caused a conflict.
  4. Store retry timers on disk. An in-memory backoff resets to zero every time Android or iOS kills your process in the background.
  5. Write tests for the ugly race conditions by name. ProgressSyncTest.kt has 23 tests covering things like a local update arriving mid-request, a token rotation during a new event, and logging out mid-refresh.

One caveat: I read these files and tests while writing this up rather than running the suite on a phone today, so everything above reflects the code and tests in the repo rather than a fresh device run.

Try it

ABS+ is a free, open-source Audiobookshelf player with no ads and no analytics. Grab it on Google Play, the App Store or F-Droid, or read the sync implementation directly in the repository (ProgressSync.kt for Android and Progress.swift for iOS). More details are at absplus.app.

Frequently asked questions

Does ABS+ lose my position if I am offline for a whole book?

No. Every playback event writes to your phone first, and the latest position for each title syncs as soon as your server is reachable again.

What happens if I listen on two phones?

Whichever position has the newer timestamp wins. ABS+ reads the server before writing and adopts the server's position instead of overwriting it.

Does progress sync need an ABS+ account?

No. ABS+ connects straight to your own Audiobookshelf server, and the local queue only holds positions and timestamps, never credentials.

Top comments (2)

Collapse
 
beusebiu profile image
Eusebiu Balan •

I had the version with no offline part and still got it wrong 😅

My app read the account once at launch and nothing read it again. Pick a new character on the web and the phone kept the old one through tab switches and pull to refresh, until a cold start.

It reads again when the app comes back to the front now. A read that lands after a change made in the app gets dropped, since the change is newer, and a failed read keeps what the phone has.

Collapse
 
backmeupplz profile image
borodutch.eth 💎🤲 •

Ha, the read-once-at-launch version is the one almost everyone writes first 😅 What you ended up with is the same rule ABS+ uses: the newer change wins whichever side it came from, and if the read fails, the phone keeps its own position and tries again later. The one extra case that bit me was my own write coming back looking like someone else's, which is why the app remembers what it sent before sending it.