# WebSocket Real-Time Notifications - Usage Guide
This guide shows you how to use the WebSocket real-time notification system in your React components.
## Quick Start
### 1. Enable Global Notifications
Add the `NotificationToastHandler` to your root `App.tsx`:
```tsx
// src/App.tsx
import { Toaster } from 'react-hot-toast';
import { NotificationToastHandler } from './components/NotificationToastHandler';
function App() {
return (
<>
{/* React Hot Toast container */}
{/* WebSocket notification handler (renders nothing, handles side effects) */}
{/* Your app routes and components */}
>
);
}
```
### 2. Add Notification Bell to Header
```tsx
// src/components/Header.tsx
import { NotificationBell } from './components/NotificationBell';
import { useNavigate } from 'react-router-dom';
function Header() {
const navigate = useNavigate();
return (
Flyer Crawler
{/* Notification bell with unread count */}
navigate('/notifications')} showConnectionStatus={true} />
);
}
```
### 3. Listen for Notifications in Components
```tsx
// src/pages/DealsPage.tsx
import { useEventBus } from '../hooks/useEventBus';
import { useCallback, useState } from 'react';
import type { DealNotificationData } from '../types/websocket';
function DealsPage() {
const [deals, setDeals] = useState([]);
// Listen for new deal notifications
const handleDealNotification = useCallback((data: DealNotificationData) => {
console.log('New deals received:', data.deals);
// Update your deals list
setDeals((prev) => [...data.deals, ...prev]);
// Or refetch from API
// refetchDeals();
}, []);
useEventBus('notification:deal', handleDealNotification);
return (
Deals
{/* Render deals */}
);
}
```
## Available Components
### `NotificationBell`
A notification bell icon with unread count and connection status indicator.
**Props:**
- `onClick?: () => void` - Callback when bell is clicked
- `showConnectionStatus?: boolean` - Show green/red/yellow connection dot (default: `true`)
- `className?: string` - Custom CSS classes
**Example:**
```tsx
navigate('/notifications')}
showConnectionStatus={true}
className="mr-4"
/>
```
### `ConnectionStatus`
A simple status indicator showing if WebSocket is connected (no bell icon).
**Example:**
```tsx
```
### `NotificationToastHandler`
Global handler that listens for WebSocket events and displays toasts. Should be rendered once at app root.
**Props:**
- `enabled?: boolean` - Enable/disable toast notifications (default: `true`)
- `playSound?: boolean` - Play sound on notifications (default: `false`)
- `soundUrl?: string` - Custom notification sound URL
**Example:**
```tsx
```
## Available Hooks
### `useWebSocket`
Connect to the WebSocket server and manage connection state.
**Options:**
- `autoConnect?: boolean` - Auto-connect on mount (default: `true`)
- `maxReconnectAttempts?: number` - Max reconnect attempts (default: `5`)
- `reconnectDelay?: number` - Base reconnect delay in ms (default: `1000`)
- `onConnect?: () => void` - Callback on connection
- `onDisconnect?: () => void` - Callback on disconnect
- `onError?: (error: Event) => void` - Callback on error
**Returns:**
- `isConnected: boolean` - Connection status
- `isConnecting: boolean` - Connecting state
- `error: string | null` - Error message if any
- `connect: () => void` - Manual connect function
- `disconnect: () => void` - Manual disconnect function
- `send: (message: WebSocketMessage) => void` - Send message to server
**Example:**
```tsx
const { isConnected, error, connect, disconnect } = useWebSocket({
autoConnect: true,
maxReconnectAttempts: 3,
onConnect: () => console.log('Connected!'),
onDisconnect: () => console.log('Disconnected!'),
});
return (