Using with React
A shared client, event subscriptions with cleanup, and live presence/state hooks.
One client, shared
Create the client once in a module and import it everywhere - not inside a component, where re-renders would recreate it.
Subscribing to events
Subscribe in an effect and return the matching
off as cleanup, so handlers do not stack up across re-mounts.Live presence and state
Lifecycle handlers (
onPresence, onStateChange, onChat, onDisconnect, ...) are add-only - there is no way to remove them, so never register them inside a component. Register one of each at module scope and fan out to component subscribers:Hooks then subscribe to the local store, which does support removal.
presenceOf and stateOf return a fresh object on every call, so setting them into React state always triggers a re-render.Connection status UI
Same pattern for connection status - the add-only handlers write to module state, components read it through the store:
⚠
Only
on(event, handler) / off(event, handler) for application events support removal. Everything named on<Something> is add-only and belongs at module scope.