Real-Time WebSocket System
Build in layers: transport first, then alert logic, then persistence, then UI.
Architecture
Campaign Data Source
|
v
Alert Engine -----> PostgreSQL (alert_rules, alert_events)
|
v
Socket.io Server
|
[rooms per campaign_id]
|
v
React Client <-- useNotifications hook
|
[Badge + Dropdown UI]
Build Workflow
1. Choose transport (Socket.io vs native ws)
2. Set up WebSocket server with per-campaign rooms
3. Design PostgreSQL schema for alert rules and events
4. Implement threshold evaluation engine
5. Wire campaign data source to engine
6. Build React client hook and notification UI
1. Transport Selection
Use Socket.io when:
- Browser clients need auto-reconnect
- You need per-campaign rooms (Socket.io rooms are built-in)
- HTTP long-poll fallback is required
- Namespaces help separate concerns (e.g.,
/alertsvs/metrics)
Use native ws when:
- Server-to-server only (no browser clients)
- Minimal dependency footprint is required
- Custom binary protocol is needed
Default choice: Socket.io. All patterns below use Socket.io.
2. WebSocket Server — Key Patterns
// Namespace for alerts
const alertsNs = io.of('/alerts');
// Auth middleware
alertsNs.use((socket, next) => {
const token = socket.handshake.auth.token;
if (!verifyToken(token)) return next(new Error('Unauthorized'));
next();
});
// Join per-campaign room on connect
alertsNs.on('connection', (socket) => {
const campaignIds: string[] = socket.handshake.query.campaigns as string[];
campaignIds.forEach(id => socket.join(`campaign:${id}`));
});
// Emit alert to campaign room
function emitAlert(campaignId: string, alert: AlertPayload) {
alertsNs.to(`campaign:${campaignId}`).emit('alert:triggered', alert);
}
For full server setup, CORS config, and scaling with Redis adapter, see references/websocket-server.md.
3. Alert Engine — Key Patterns
Threshold evaluation (called after each data refresh):
async function evaluateThresholds(campaignId: string, metrics: CampaignMetrics) {
const rules = await db.query(
'SELECT * FROM alert_rules WHERE campaign_id = $1 AND enabled = true',
[campaignId]
);
for (const rule of rules.rows) {
const value = metrics[rule.metric_key];
if (exceedsThreshold(value, rule.operator, rule.threshold_value)) {
await persistAlert(campaignId, rule, value);
emitAlert(campaignId, { ruleId: rule.id, metric: rule.metric_key, value });
}
}
}
For the full PostgreSQL schema (alert_rules, alert_events), cooldown logic, and operator types, see references/alert-engine.md.
4. React Notification UI — Key Patterns
// Notification badge
<button onClick={toggleDropdown}>
<BellIcon />
{unreadCount > 0 && <span className="badge">{unreadCount}</span>}
</button>
For useNotifications hook (Socket.io client + state), dropdown component, and mark-as-read API pattern, see references/react-ui.md.
Resources
- references/websocket-server.md — Full server setup, Redis adapter for scaling, auth, CORS, error handling
- references/alert-engine.md — PostgreSQL schema, threshold operators, cooldown logic, campaign-level config
- references/react-ui.md — useNotifications hook, badge component, dropdown, mark-as-read
When NOT to Use This Skill
- Infrequent data updates — if data changes less than once per minute, HTTP polling is simpler and less resource-intensive than a persistent WebSocket connection
- Request-response APIs — WebSockets are for bidirectional streaming; for standard query-response patterns, use REST or GraphQL
- Large file transfers — WebSockets are optimized for small, frequent messages; use HTTP multipart uploads for large file transfers
Common Mistakes
- Not implementing heartbeat/ping-pong to detect stale connections — TCP connections can silently break; without keepalive pings, the server accumulates phantom connections that never receive messages
- Broadcasting to all connections instead of targeted rooms/channels — sending every event to every client wastes bandwidth and leaks data to unintended recipients
- Not handling reconnection with exponential backoff on the client — clients that reconnect immediately on disconnect hammer the server; always implement exponential backoff