[CLAUDE] App+Infra+Api+FE: SignalR realtime notifications E2E
All checks were successful
Deploy SOLUTION_ERP / build-deploy (push) Successful in 2m43s

Clean-arch split:
- Application: IRealtimeNotifier (PushToUserAsync, abstraction)
- Api: NotificationHub (/hubs/notifications, [Authorize]) +
  SignalRNotifier impl với IHubContext<NotificationHub>, uses
  Clients.User(userId) (default provider resolves NameIdentifier="sub")
- Infrastructure: NotificationPushInterceptor — SaveChangesInterceptor
  capture Notification entities state=Added trong SavingChanges,
  push qua IRealtimeNotifier trong SavedChanges sau khi commit thành
  công. Zero caller changes — handlers chỉ cần db.Add(Notification).
  Attached vào ApplicationDbContext cùng với AuditingInterceptor.

Auth:
- JWT config thêm OnMessageReceived event: read ?access_token= từ
  query string khi path = /hubs/* (WebSockets không set headers).
- SignalRNotifier singleton (stateless, chỉ delegate IHubContext).

FE (both apps):
- @microsoft/signalr 8.0.7 vào package.json.
- lib/realtime.ts: singleton connection với lazy start + automatic
  reconnect [0,2s,5s,10s,15s] + accessTokenFactory lấy từ localStorage.
- NotificationBell: useEffect subscribe 'notification-created' khi
  isAuthenticated. On push: invalidate query + toast.message. Fallback
  polling giảm từ 30s → 60s (realtime cover gap).
- AuthContext.logout: dynamic import stopConnection() — avoid leaking
  auth'd socket across users.

Result: ERP-grade feel. Contract transition → Drafter nhận toast ngay
trong vòng 100-300ms (same-origin WebSocket), không cần F5 hay polling.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
pqhuy1987
2026-04-21 20:56:37 +07:00
parent 2a851caa92
commit ea9ab5e352
14 changed files with 319 additions and 3 deletions

View File

@ -13,6 +13,7 @@
"preview": "vite preview"
},
"dependencies": {
"@microsoft/signalr": "^8.0.7",
"@tailwindcss/vite": "^4.2.3",
"@tanstack/react-query": "^5.99.2",
"axios": "^1.15.1",

View File

@ -2,7 +2,10 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { useEffect, useRef, useState } from 'react'
import { Bell, CheckCheck } from 'lucide-react'
import { useNavigate } from 'react-router-dom'
import { toast } from 'sonner'
import { api } from '@/lib/api'
import { ensureConnection } from '@/lib/realtime'
import { useAuth } from '@/contexts/AuthContext'
import { cn } from '@/lib/cn'
type NotificationDto = {
@ -32,13 +35,42 @@ export function NotificationBell() {
const panelRef = useRef<HTMLDivElement>(null)
const navigate = useNavigate()
const qc = useQueryClient()
const { isAuthenticated } = useAuth()
const list = useQuery({
queryKey: ['notifications'],
queryFn: async () => (await api.get<NotificationDto[]>('/notifications', { params: { limit: 20 } })).data,
refetchInterval: 30_000,
// Fallback polling at 60s in case SignalR disconnects + reconnect fails
refetchInterval: 60_000,
})
// Subscribe realtime when authenticated. Toast on push + invalidate query.
useEffect(() => {
if (!isAuthenticated) return
let conn: Awaited<ReturnType<typeof ensureConnection>> | null = null
let cancelled = false
const handler = (payload: NotificationDto) => {
qc.invalidateQueries({ queryKey: ['notifications'] })
toast.message(payload.title, { description: payload.description ?? undefined })
}
ensureConnection()
.then(c => {
if (cancelled) return
conn = c
c.on('notification-created', handler)
})
.catch(() => {
// SignalR unavailable — rely on polling fallback
})
return () => {
cancelled = true
if (conn) conn.off('notification-created', handler)
}
}, [isAuthenticated, qc])
const items = list.data ?? []
const unread = items.filter(n => !n.readAt).length

View File

@ -64,6 +64,8 @@ export function AuthProvider({ children }: { children: ReactNode }) {
localStorage.removeItem(MENU_KEY)
setUser(null)
setMenu([])
// Close realtime socket — avoid leaking auth'd connection across users
import('@/lib/realtime').then(m => m.stopConnection()).catch(() => {})
}
return (

View File

@ -0,0 +1,48 @@
import { HubConnection, HubConnectionBuilder, HubConnectionState, LogLevel } from '@microsoft/signalr'
import { TOKEN_KEY } from '@/lib/api'
// Hub URL resolution:
// - Dev: Vite proxy forwards /api → :5443 but SignalR bypasses axios, so we
// hit the API origin directly from the browser.
// - Prod: VITE_API_BASE_URL (https://api.huypham.vn)
const HUB_URL = (import.meta.env.VITE_API_BASE_URL ?? window.location.origin) + '/hubs/notifications'
let connection: HubConnection | null = null
let startPromise: Promise<void> | null = null
/** Lazily starts (or reuses) a single hub connection. Token read on connect. */
export async function ensureConnection(): Promise<HubConnection> {
if (connection && connection.state === HubConnectionState.Connected) return connection
if (!connection) {
connection = new HubConnectionBuilder()
.withUrl(HUB_URL, {
accessTokenFactory: () => localStorage.getItem(TOKEN_KEY) ?? '',
})
.withAutomaticReconnect([0, 2_000, 5_000, 10_000, 15_000]) // exponential-ish backoff
.configureLogging(LogLevel.Warning)
.build()
}
if (connection.state === HubConnectionState.Disconnected) {
startPromise ??= connection.start().catch(err => {
startPromise = null
throw err
})
await startPromise
startPromise = null
}
return connection
}
/** Stops + forgets the connection. Call on logout. */
export async function stopConnection(): Promise<void> {
if (!connection) return
try {
await connection.stop()
} catch {
/* ignore */
}
connection = null
startPromise = null
}