-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
74 lines (57 loc) · 2.76 KB
/
Copy pathclient.ts
File metadata and controls
74 lines (57 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Multi-namespace client — connects to all three namespaces on the same server.
*
* Run (after starting the server): npx tsx examples/multi-namespace/client.ts
*/
import { io } from 'socket.io-client';
const BASE_URL = 'http://localhost:3000';
// ── Prices client — listens to bid and ask topics ────────────────────────────
const pricesSocket = io(`${BASE_URL}/prices`, { transports: ['websocket'] });
pricesSocket.on('connect', () => {
console.log('[prices] connected');
pricesSocket.emit('subscribe', { topic: 'bid', token: null });
pricesSocket.emit('subscribe', { topic: 'ask', token: null });
});
pricesSocket.on('bid', (payload: unknown) => {
console.log('[prices] bid:', payload);
});
pricesSocket.on('ask', (payload: unknown) => {
console.log('[prices] ask:', payload);
});
// ── Snapshots client — latest portfolio value only ───────────────────────────
const snapshotsSocket = io(`${BASE_URL}/snapshots`, { transports: ['websocket'] });
snapshotsSocket.on('connect', () => {
console.log('[snapshots] connected');
snapshotsSocket.emit('subscribe', { topic: 'portfolio', token: null });
});
snapshotsSocket.on('portfolio', (payload: unknown) => {
console.log('[snapshots] portfolio:', payload);
});
// ── Alerts client — AT_LEAST_ONCE with acknowledgement ───────────────────────
const alertsSocket = io(`${BASE_URL}/alerts`, { transports: ['websocket'] });
alertsSocket.on('connect', () => {
console.log('[alerts] connected');
alertsSocket.emit('subscribe', { topic: 'critical', token: null });
alertsSocket.emit('subscribe', { topic: 'info', token: null });
});
alertsSocket.on('critical', (payload: unknown, metadata?: { ackRequired?: boolean; topic?: string; messageId?: string }) => {
console.log('[alerts] CRITICAL:', payload);
if (metadata?.ackRequired) {
alertsSocket.emit('ack', { topic: metadata.topic, messageId: metadata.messageId });
console.log('[alerts] acked message', metadata.messageId);
}
});
alertsSocket.on('info', (payload: unknown, metadata?: { ackRequired?: boolean; topic?: string; messageId?: string }) => {
console.log('[alerts] info:', payload);
if (metadata?.ackRequired) {
alertsSocket.emit('ack', { topic: metadata.topic, messageId: metadata.messageId });
}
});
// ── Graceful shutdown ────────────────────────────────────────────────────────
process.on('SIGINT', () => {
pricesSocket.disconnect();
snapshotsSocket.disconnect();
alertsSocket.disconnect();
console.log('\nDisconnected.');
process.exit(0);
});