Real-time Notifications
Use one notifications channel, restrict the types you accept, and react with onMessage so each alert fires exactly once:
function NotificationSystem() { const [toasts, setToasts] = useState<Toast[]>([]);
const { postMessage } = useBroadcastChannel('notifications', { registeredTypes: ['alert', 'info', 'warning'], deduplicationTTL: 60_000, onMessage: { alert: msg => showToast('error', msg.message), info: msg => showToast('info', msg.message), warning: msg => showToast('warning', msg.message), }, });
const announce = (text: string) => postMessage('info', text); // ...}Tips:
onMessageis the right tool here: it fires once per accepted message, with nouseEffectover the log.- Give notifications an
expirationDurationif stale alerts should disappear from the log. - The provider demo ships a working version of this pattern.
