radius-read
Version:
Realtime Dashboard
161 lines • 9.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocketIoService = void 0;
const tslib_1 = require("tslib");
const socket_io_1 = require("socket.io");
const socketio_enum_1 = require("./../enums/socketio.enum");
const ws_1 = tslib_1.__importDefault(require("ws"));
/**
* Service for managing Socket.IO connections and WebSocket communication.
*/
class SocketIoService {
/**
* Creates an instance of SocketIoService.
*/
constructor() { }
/**
* Initializes the Socket.IO server with the provided WebSocket URL and application instance.
* @param wsURL
* @param app
* @param callback
*/
static init(wsURL, app, callback) {
this._io = new socket_io_1.Server(app, {
cors: {
origin: '*'
},
});
callback({ eventType: socketio_enum_1.SocketIoEventType.Initialized });
try {
this._io.on("connection", (socket) => {
callback({ eventType: socketio_enum_1.SocketIoEventType.Connected, ioSocket: socket });
let lightSocket;
let connectionKey;
socket.on("register", ({ userId, tenantCode }) => {
connectionKey = `${userId}_${tenantCode}`;
if (!this._wsConnections[connectionKey]) {
try {
lightSocket = new ws_1.default(wsURL);
this._wsConnections[connectionKey] = lightSocket;
lightSocket.on("open", () => {
callback({ eventType: socketio_enum_1.SocketIoEventType.WsConnected, ioSocket: socket, lightSocket: lightSocket, userId: userId, tenantCode: tenantCode });
var msg = JSON.stringify({ EvCodeApp: socketio_enum_1.SocketIoEventType.WsConnected, userId: userId, tenantCode: tenantCode });
this._io.to(connectionKey).emit("message", msg);
callback({ eventType: socketio_enum_1.SocketIoEventType.MessageSent, ioSocket: socket, lightSocket: lightSocket, message: msg, userId: userId, tenantCode: tenantCode });
});
lightSocket.on("message", (message) => {
callback({ eventType: socketio_enum_1.SocketIoEventType.WsMessageRecieved, ioSocket: socket, lightSocket: lightSocket, message: message, userId: userId, tenantCode: tenantCode });
// var msg = JSON.stringify({ EvCodeApp: SocketIoEventType.WsMessageRecieved, userId: userId, tenantCode: tenantCode, evtData: message.toString() });
// var msg = message.toString() && JSON.parse(message.toString());
// if (msg?.EvGen === 'Sync')
// socket.emit("message", message.toString());
// else
this._io.to(connectionKey).emit("message", message.toString());
callback({ eventType: socketio_enum_1.SocketIoEventType.MessageSent, ioSocket: socket, lightSocket: lightSocket, message: message.toString(), userId: userId, tenantCode: tenantCode });
});
lightSocket.on("close", (event) => {
callback({ eventType: socketio_enum_1.SocketIoEventType.WsDisconnected, ioSocket: socket, lightSocket: lightSocket, event: event, userId: userId, tenantCode: tenantCode });
this._wsConnections[connectionKey] = undefined;
var msg = JSON.stringify({ EvCodeApp: socketio_enum_1.SocketIoEventType.WsDisconnected, userId: userId, tenantCode: tenantCode, evtData: event });
this._io.to(connectionKey).emit("message", msg);
callback({ eventType: socketio_enum_1.SocketIoEventType.MessageSent, ioSocket: socket, lightSocket: lightSocket, message: msg, userId: userId, tenantCode: tenantCode });
});
lightSocket.on("error", (event) => {
callback({ eventType: socketio_enum_1.SocketIoEventType.WsError, ioSocket: socket, lightSocket: lightSocket, event: event, userId: userId, tenantCode: tenantCode });
var msg = JSON.stringify({ EvCodeApp: socketio_enum_1.SocketIoEventType.WsError, userId: userId, tenantCode: tenantCode, evtData: event });
this._io.to(connectionKey).emit("message", msg);
callback({ eventType: socketio_enum_1.SocketIoEventType.MessageSent, ioSocket: socket, lightSocket: lightSocket, message: msg, userId: userId, tenantCode: tenantCode });
});
}
catch (ex) {
callback({ eventType: socketio_enum_1.SocketIoEventType.WsError, ioSocket: socket, lightSocket: lightSocket, event: ex, userId: userId, tenantCode: tenantCode });
}
}
else {
var _lightSocket = this._wsConnections[connectionKey];
if ((_lightSocket === null || _lightSocket === void 0 ? void 0 : _lightSocket.readyState) === (_lightSocket === null || _lightSocket === void 0 ? void 0 : _lightSocket.OPEN)) {
var msg = JSON.stringify({ EvCodeApp: socketio_enum_1.SocketIoEventType.WsConnected, userId: userId, tenantCode: tenantCode });
socket.emit("message", msg);
}
}
socket.join(connectionKey);
});
// Handle broadcasting messages
socket.on("broadcast", ({ userId, tenantCode, message }) => {
const connectionKey = `${userId}_${tenantCode}`;
const lightSocket = this._wsConnections[connectionKey];
callback({ eventType: socketio_enum_1.SocketIoEventType.MessageRecieved, ioSocket: socket, lightSocket: lightSocket, userId: userId, tenantCode: tenantCode, message: message });
if (lightSocket && lightSocket.readyState === lightSocket.OPEN) {
lightSocket.send(message);
callback({ eventType: socketio_enum_1.SocketIoEventType.WsMessageSent, ioSocket: socket, lightSocket: lightSocket, userId: userId, tenantCode: tenantCode, message: message });
}
});
// Handle client disconnection
socket.on("disconnect", () => tslib_1.__awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
callback({ eventType: socketio_enum_1.SocketIoEventType.Disconnected, ioSocket: socket, lightSocket: lightSocket });
let clients = yield ((_b = (_a = this._io) === null || _a === void 0 ? void 0 : _a.in(connectionKey)) === null || _b === void 0 ? void 0 : _b.fetchSockets());
if ((clients === null || clients === void 0 ? void 0 : clients.length) === 0) {
(_c = this._wsConnections[connectionKey]) === null || _c === void 0 ? void 0 : _c.close();
}
}));
socket.on('connect_error', (error) => {
callback({ eventType: socketio_enum_1.SocketIoEventType.Error, ioSocket: socket, lightSocket: lightSocket, error: error });
});
});
}
catch (e) {
callback({ eventType: socketio_enum_1.SocketIoEventType.Error, error: e });
}
}
/**
* Deinitializes the Socket.IO server and closes all WebSocket connections.
*/
static deinit() {
var _a;
(_a = this._io) === null || _a === void 0 ? void 0 : _a.close();
}
/**
* Sends a message to a specific user and tenant using Socket.IO.
* @param userId
* @param tenantCode
* @param message
*/
static sendIoMessage(userId, tenantCode, message) {
const connectionKey = `${userId}_${tenantCode}`;
this._io.to(connectionKey).emit("message", message.toString());
}
/**
* Sends a WebSocket message to a specific user and tenant.
* @param userId
* @param tenantCode
* @param message
*/
static sendWsMessage(userId, tenantCode, message) {
const connectionKey = `${userId}_${tenantCode}`;
const lightSocket = this._wsConnections[connectionKey];
lightSocket === null || lightSocket === void 0 ? void 0 : lightSocket.send(message);
}
/**
* Verifies if a WebSocket connection exists for the given userId and tenantCode.
* @param userId
* @param tenantCode
* @returns
*/
static isConnectionExists(userId, tenantCode) {
const connectionKey = `${userId}_${tenantCode}`;
return this._wsConnections[connectionKey] ? true : false;
}
}
exports.SocketIoService = SocketIoService;
/**
* WebSocket connections indexed by userId and tenantCode.
* @date Jun 26, 2025 04:05:15 PM
* @author Biswaranjan Nayak
*
* @private
* @static
* @type {({ [key: string]: WebSocket | undefined })}
*/
SocketIoService._wsConnections = {};
//# sourceMappingURL=socketio.service.js.map