@notificationhub/client
v0.2.1
Published
NotificationHub client: a live notification feed that survives dropped connections
Readme
@notificationhub/client
The NotificationHub client: a live notification feed that survives dropped connections, plus push-device registration.
pnpm add @notificationhub/clientPublished to public npm, so there is no registry configuration and no token.
Use
import { NotificationClient } from '@notificationhub/client'
const client = new NotificationClient({
baseUrl: 'https://notifications.example.com',
// Called on EVERY connect, not once: the server closes sockets whose
// authorization has aged out, and a stale token produces a reconnect loop.
getToken: () => fetchShortLivedToken(),
feed: 'learning-space',
})
const unsubscribe = client.subscribe(({ items, counts, hasMore }) => render(items, counts, hasMore))
await client.start()subscribe + state are the pair React's useSyncExternalStore wants, so a
hook layer is a few lines rather than a second copy of this state.
What it does
| | |
|---|---|
| start / stop | Reconcile, then hold a socket |
| subscribe / state | Observe the feed |
| onArrived | Items that just arrived — for a toast, chime or badge |
| setFilter | all / unread / unseen, filtered server-side |
| fetchNextPage | Older notifications, appended |
| markSeen / markRead / markUnread | State changes, applied optimistically |
| archive / unarchive / markAllRead | As above |
| registerDevice / removeDevice | Push registration for this device |
| handleFocus | Refetch after a tab has slept |
Registering a device without a feed
registerDevice and removeDevice are also exported as plain functions, for
the two cases that cannot hold a client:
import { registerDevice, removeDevice } from '@notificationhub/client'
await registerDevice({ baseUrl, userToken, token: fcmToken, platform: 'web' })
await removeDevice({ baseUrl, userToken, token: fcmToken })Logout is the one that forces this. The session is ending, so there is no
way to mint a fresh end-user token — the caller has to pass the one it
registered with, which a client's getToken cannot express because it always
asks for a new one.
The second case is a page that registers a device without rendering a feed. Requiring a client there would tie push to whatever gates the feed.
The client's methods delegate to these, so both paths speak one wire format.
onArrived is not subscribe with extra steps. subscribe reports the whole
state whenever anything changes, so a reconcile that delivered three
notifications looks identical to one that changed nothing — which is exactly
what a caller needs to tell apart before it pops a toast. It never fires for
the first load, because existing history did not arrive.
state.loading means a full fetch is outstanding and the list below is not yet
the answer; state.loadingMore means the list IS the answer and more is being
appended. A skeleton belongs to the first and a spinner to the second, so they
are two flags.
state.unread and state.unseen are for the feed this client renders, not
across all of them. state.counts still carries every feed plus a total, which
is what a client displaying several wants — but lighting a badge from the total
means a notification in a feed this client never shows keeps it lit, and nothing
the person does can clear it.
The assumption it is built on
The socket is unreliable. Sockets drop, tabs sleep, phones change network, and the server deliberately closes a connection once its token has aged out.
So the socket carries hints, not data: it says "something changed", and the client fetches the truth over HTTP. A missed message costs one extra request rather than a lost notification — which is what removes message ordering, replay buffers and acknowledgements from the design entirely.
Reconnection uses exponential backoff with jitter, because every client of a tenant is disconnected by the same deploy at the same instant and a bare schedule reconnects them in lockstep. After three failures it falls back to polling, so the feed still updates where a socket cannot be held at all — a corporate proxy, a restrictive network, an old WebView.
State changes are optimistic: applied locally and put back if the server disagrees. A round trip of latency on "mark as read" reads as a broken button.
Releasing
Versioned with semver and published by .github/workflows/publish-client.yml
on a client-v* tag:
npm version --no-git-tag-version minor --prefix packages/client
git commit -am "client: v0.2.0" && git tag client-v0.2.0 && git push --follow-tags