useNotificationStream
The useNotificationStream hook connects to the real-time notification stream via WebSocket for live notification updates.
Return Value
Whether the WebSocket connection is active
Any connection error that occurred
Show Manually disconnect the WebSocket
Show Manually reconnect the WebSocket
import { useNotificationStream } from "@wacht/tanstack-router";
function App() {
const { isConnected, connectionError } = useNotificationStream({
enabled: true,
onNotification: (notification) => {
console.log("New notification:", notification);
},
onError: (error) => {
console.error("Stream error:", error);
},
});
return (
<div>
<p>Stream: {isConnected ? "Connected" : "Disconnected"}</p>
{connectionError && <p>Error: {connectionError}</p>}
</div>
);
}
Options
Whether to enable the stream
Organization IDs to filter notifications
Workspace IDs to filter notifications
Show Callback when a new notification is received
Show Callback when a connection error occurs
Delay in milliseconds before reconnecting
Maximum number of reconnection attempts
Notification Message
interface NotificationMessage {
id: number;
user_id: number;
deployment_id: number;
title: string;
body: string;
severity: string;
ctas?: { label: string; payload: any }[];
created_at: string;
}
Examples
Basic Stream Connection
import { useNotificationStream } from "@wacht/tanstack-router";
function NotificationIndicator() {
const { isConnected } = useNotificationStream({
enabled: true,
});
return (
<div>
<span
style={{
width: 10,
height: 10,
borderRadius: "50%",
background: isConnected ? "green" : "red",
}}
/>
</div>
);
}
With Channel Subscription
import { useNotificationStream } from "@wacht/tanstack-router";
function ChannelSubscriber() {
useNotificationStream({
enabled: true,
channels: ["updates", "alerts"],
});
return <div>Subscribed to updates and alerts</div>;
}
With Organization Filtering
import { useNotificationStream } from "@wacht/tanstack-router";
function OrgNotifications({ orgId }: { orgId: number }) {
useNotificationStream({
enabled: true,
organizationIds: [orgId],
});
return <div>Listening for organization notifications</div>;
}
With Error Handling
import { useNotificationStream } from "@wacht/tanstack-router";
function NotificationsWithRetry() {
const { isConnected, connectionError, reconnect } = useNotificationStream({
enabled: true,
reconnectDelay: 2000,
maxReconnectAttempts: 10,
onError: (error) => {
toast.error(`Connection error: ${error}`);
},
});
return (
<div>
<p>Status: {isConnected ? "Connected" : "Disconnected"}</p>
{!isConnected && (
<button onClick={() => reconnect()}>Reconnect</button>
)}
{connectionError && <p>Error: {connectionError}</p>}
</div>
);
}
Disconnect on Unmount
import { useNotificationStream } from "@wacht/tanstack-router";
function ConditionalStream({ enabled }: { enabled: boolean }) {
const { isConnected, disconnect } = useNotificationStream({
enabled,
});
useEffect(() => {
return () => {
disconnect();
};
}, [disconnect]);
return <div>{/* Content */}</div>;
}
WebSocket Connection
The hook establishes a WebSocket connection to:
wss://{backend_host}/realtime/notifications
The connection includes:
- Automatic reconnection with exponential backoff
- Ping/pong for keep-alive
- Session-based authentication
- Channel and organization filtering