-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisten.js
More file actions
32 lines (25 loc) · 899 Bytes
/
Copy pathlisten.js
File metadata and controls
32 lines (25 loc) · 899 Bytes
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
import { WebSocket } from "ws";
const AUTH_TOKEN = process.env.AUTH_TOKEN;
if (!AUTH_TOKEN) {
console.error("❌ Error: Please provide an AUTH_TOKEN variable to run this script.");
process.exit(1);
}
const subprotocols = ["access_token", AUTH_TOKEN];
const ws = new WebSocket("ws://localhost:5000/ws", subprotocols);
ws.on("open", () => {
console.log("✅ Connected to WebSocket Server! Watching for database updates...");
});
ws.on("message", (buffer) => {
try {
const payload = JSON.parse(buffer.toString());
console.log("🚀 Live Notification Received:\n", JSON.stringify(payload, null, 2));
} catch (err) {
console.log("📡 Raw Notification:", buffer.toString());
}
});
ws.on("error", (err) => {
console.error("❌ WebSocket Client Connection Error:", err.message);
});
ws.on("close", () => {
console.log("🔒 Connection closed safely by the server.");
});