Migrating from 0.1
What changed in @folkwaysio/realtime 0.2, and how to update apps written for 0.1.
0.2 makes memory use explicit and bounded. A client no longer keeps history for every topic it has touched: data is kept only while a view of it is open, and every queue has a budget.
| In 0.1 | In 0.2 |
|---|---|
| A topic handle was a long-lived object holding its history | A handle is a lightweight address. Compare handles by name |
| Messages and values were kept for every topic | Kept only while a view is open: messages.open(), values().open() |
| Every publish appeared in the message list | Only while a message view is open. Publishing is otherwise headless |
| Queues could grow without a set limit | Pending publishes, inbound work, state, iterators and the cache each have a budget; see Resource management |
| Signal lifetimes were estimated on the device | The service sends each signal's expiry and its clock |
| Server code called the Realtime API endpoints for members, DMs, kick and revoke directly | Rest has members, dm, kick() and revoke(), and createToken() returns jti |
| The cache defaulted to 5 MiB | 2 MiB |
Update your code
Open a view where you show data, and dispose it when you're done.
const room = client.topic('rooms/42');
const messages = room.messages.open(); // before subscribing, to receive the replay
const stop = messages.subscribe(() => render(messages.getSnapshot()));
const sub = room.subscribe({ from: { last: 50 } });
// later
stop();
messages.dispose();
await sub.unsubscribe();In React, memoize the view by client, topic and event, and bind it with useSyncExternalStore. Its subscribe
opens and releases the view for you:
const view = useMemo(() => client.topic(name).messages, [client, name]);
const messages = useSyncExternalStore(view.subscribe, view.getSnapshot);Handle QueueFull. It now means one of several budgets is full: pending publishes, a topic's inbound work, an
iterator's buffer. See Resource management.
Iterators. for await loops have their own bounded buffer each; overflow throws QueueFull for that loop only.
Save your own cursor as you process events: the shared cursor tracks callback delivery, not iterator consumption.
Sign-in changes. updateToken() with another identity clears all view data and fails pending sends; views stay
open. Subscribe again once it resolves.
Server. Replace calls to the members, DM, kick and revoke endpoints with folkways.members, folkways.dm,
folkways.kick() and folkways.revoke(). See Issue tokens from your backend.
Before you ship
Test mounting and unmounting screens, headless publishers, slow consumers, large snapshots, reconnects, sign-out and persistent storage with your real data sizes.