Page MenuHomeSealhub

notifications.mjs
No OneTemporary

Authored By
Unknown
Size
906 B
Referenced Files
None
Subscribers
None

notifications.mjs

import { WebSocket } from "ws";
import { WebSocketServer } from "ws";
const notification_proxy = new WebSocketServer({ port: 3001 });
let notification_subs = [];
notification_proxy.on('connection', (ws) => {
notification_subs.push(ws);
});
export function send_notification(is_ok, context, message)
{
let updated_subs = [];
if (notification_subs.length === 0) {
console.log("WARNING: Got a notification, but nobody is subscribed");
}
for (const sub of notification_subs) {
if (sub.readyState == WebSocket.CONNECTING) {
console.log("WARNING: Unable to forward a notification to client that is still connecting");
updated_subs.push(sub);
} else {
try {
sub.send(JSON.stringify({
is_ok,
context,
message
}));
updated_subs.push(sub);
} catch {
sub.close();
console.log("WARNING: Fail to send a notification, closing the connection");
}
}
}
}

File Metadata

Mime Type
application/javascript
Expires
Sat, Nov 28, 04:26 (1 d, 4 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1226577
Default Alt Text
notifications.mjs (906 B)

Event Timeline