From 5c38754b523335a43780fdb13e0c715a924edb90 Mon Sep 17 00:00:00 2001 From: sourav Date: Sat, 16 May 2020 14:04:04 +0530 Subject: [PATCH 01/18] Debugging tillnow --- dist/src/api/v1/calls/index.js | 5 +- dist/src/messageHandler/index.js | 1 + dist/src/models/client.js | 1 + dist/src/models/realm.js | 11 + dist/src/services/webSocketServer/index.js | 14 +- dist/src/utils.js | 15 ++ package-lock.json | 256 ++++++++++++++++++++- package.json | 5 +- src/api/v1/calls/index.ts | 18 +- src/messageHandler/index.ts | 44 +++- src/models/client.ts | 3 +- src/models/realm.ts | 16 +- src/services/webSocketServer/index.ts | 50 ++-- src/utils.ts | 17 ++ 14 files changed, 404 insertions(+), 52 deletions(-) create mode 100644 dist/src/utils.js create mode 100644 src/utils.ts diff --git a/dist/src/api/v1/calls/index.js b/dist/src/api/v1/calls/index.js index 17ae5e300..b8c8f3c71 100644 --- a/dist/src/api/v1/calls/index.js +++ b/dist/src/api/v1/calls/index.js @@ -4,10 +4,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); const express_1 = __importDefault(require("express")); -exports.default = ({ realm, messageHandler }) => { +exports.default = ({ realm, messageHandler, }) => { const app = express_1.default.Router(); const handle = (req, res, next) => { const { id } = req.params; + console.log("Got request..."); if (!id) return next(); const client = realm.getClientById(id); @@ -19,7 +20,7 @@ exports.default = ({ realm, messageHandler }) => { type, src: id, dst, - payload + payload, }; messageHandler.handle(client, message); res.sendStatus(200); diff --git a/dist/src/messageHandler/index.js b/dist/src/messageHandler/index.js index c8393cae6..0782b8540 100644 --- a/dist/src/messageHandler/index.js +++ b/dist/src/messageHandler/index.js @@ -25,6 +25,7 @@ class MessageHandler { this.handlersRegistry.registerHandler(enums_1.MessageType.EXPIRE, handleTransmission); } handle(client, message) { + console.log("Handle Message"); return this.handlersRegistry.handle(client, message); } } diff --git a/dist/src/models/client.js b/dist/src/models/client.js index 79473e452..928473eab 100644 --- a/dist/src/models/client.js +++ b/dist/src/models/client.js @@ -4,6 +4,7 @@ class Client { constructor({ id, token }) { this.socket = null; this.lastPing = new Date().getTime(); + console.log("Create a client"); this.id = id; this.token = token; } diff --git a/dist/src/models/realm.js b/dist/src/models/realm.js index 743b184f4..ed68c8504 100644 --- a/dist/src/models/realm.js +++ b/dist/src/models/realm.js @@ -5,10 +5,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); const v4_1 = __importDefault(require("uuid/v4")); const messageQueue_1 = require("./messageQueue"); +const utils_1 = require("../utils"); +const Redis = require("ioredis"); +// const redisPub = new Redis(); +const redisSub = new Redis(); class Realm { constructor() { this.clients = new Map(); this.messageQueues = new Map(); + redisSub.subscribe("clients", (err) => { + if (!err) + utils_1.clog("Subscribed to Clients"); + }); } getClientsIds() { return [...this.clients.keys()]; @@ -30,9 +38,11 @@ class Realm { return true; } getMessageQueueById(id) { + console.log("Getting MessageQueue"); return this.messageQueues.get(id); } addMessageToQueue(id, message) { + console.log("Add MessageQueue"); if (!this.getMessageQueueById(id)) { this.messageQueues.set(id, new messageQueue_1.MessageQueue()); } @@ -47,6 +57,7 @@ class Realm { while (this.getClientById(clientId)) { clientId = generateId(); } + console.log("Generate ID", clientId); return clientId; } } diff --git a/dist/src/services/webSocketServer/index.js b/dist/src/services/webSocketServer/index.js index fde0b0309..f24c3f642 100644 --- a/dist/src/services/webSocketServer/index.js +++ b/dist/src/services/webSocketServer/index.js @@ -8,15 +8,15 @@ const url_1 = __importDefault(require("url")); const ws_1 = __importDefault(require("ws")); const enums_1 = require("../../enums"); const client_1 = require("../../models/client"); -const WS_PATH = 'peerjs'; +const WS_PATH = "peerjs"; class WebSocketServer extends events_1.default { - constructor({ server, realm, config }) { + constructor({ server, realm, config, }) { super(); this.setMaxListeners(0); this.realm = realm; this.config = config; const path = this.config.path; - this.path = `${path}${path.endsWith('/') ? "" : "/"}${WS_PATH}`; + this.path = `${path}${path.endsWith("/") ? "" : "/"}${WS_PATH}`; this.socketServer = new ws_1.default.Server({ path: this.path, server }); this.socketServer.on("connection", (socket, req) => this._onSocketConnection(socket, req)); this.socketServer.on("error", (error) => this._onSocketError(error)); @@ -36,7 +36,7 @@ class WebSocketServer extends events_1.default { // ID-taken, invalid token socket.send(JSON.stringify({ type: enums_1.MessageType.ID_TAKEN, - payload: { msg: "ID is taken" } + payload: { msg: "ID is taken" }, })); return socket.close(); } @@ -48,12 +48,13 @@ class WebSocketServer extends events_1.default { // handle error this.emit("error", error); } - _registerClient({ socket, id, token }) { + _registerClient({ socket, id, token, }) { // Check concurrent limit const clientsCount = this.realm.getClientsIds().length; if (clientsCount >= this.config.concurrent_limit) { return this._sendErrorAndClose(socket, enums_1.Errors.CONNECTION_LIMIT_EXCEED); } + console.log("Registering New Client", JSON.stringify({ id, token })); const newClient = new client_1.Client({ id, token }); this.realm.setClient(newClient, id); socket.send(JSON.stringify({ type: enums_1.MessageType.OPEN })); @@ -72,6 +73,7 @@ class WebSocketServer extends events_1.default { socket.on("message", (data) => { try { const message = JSON.parse(data); + console.log("WSS::New Message from Client"); message.src = client.getId(); this.emit("message", client, message); } @@ -84,7 +86,7 @@ class WebSocketServer extends events_1.default { _sendErrorAndClose(socket, msg) { socket.send(JSON.stringify({ type: enums_1.MessageType.ERROR, - payload: { msg } + payload: { msg }, })); socket.close(); } diff --git a/dist/src/utils.js b/dist/src/utils.js new file mode 100644 index 000000000..2b1788693 --- /dev/null +++ b/dist/src/utils.js @@ -0,0 +1,15 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const chalk = require("chalk"); +const stackTrace = require("stack-trace"); +exports.clog = (message) => { + console.log(chalk.blue(message)); +}; +exports.trace = () => { + stackTrace + .get() + .filter((site) => !site.getFileName().includes("node_modules")) + .map((site) => { + console.log(chalk.red(`${site.getFileName()} --- ${site.getLineNumber()}`)); + }); +}; diff --git a/package-lock.json b/package-lock.json index 9e7ad046e..f87b09238 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,28 @@ "chalk": "^2.0.0", "esutils": "^2.0.2", "js-tokens": "^4.0.0" + }, + "dependencies": { + "chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "dev": true, + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + } + }, + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dev": true, + "requires": { + "has-flag": "^3.0.0" + } + } } }, "@sinonjs/commons": { @@ -570,6 +592,26 @@ "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-4.1.0.tgz", "integrity": "sha1-1UVjW+HjPFQmScaRc+Xeas+uNN0=", "dev": true + }, + "chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "dev": true, + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + } + }, + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dev": true, + "requires": { + "has-flag": "^3.0.0" + } } } }, @@ -678,23 +720,47 @@ } }, "chalk": { - "version": "2.4.2", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", - "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", - "dev": true, + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.0.0.tgz", + "integrity": "sha512-N9oWFcegS0sFr9oh1oz2d7Npos6vNoWW9HvtCg5N1KRFpUhaAhvTv5Y58g880fZaEYSNm3qDz8SU1UrGvp+n7A==", "requires": { - "ansi-styles": "^3.2.1", - "escape-string-regexp": "^1.0.5", - "supports-color": "^5.3.0" + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" }, "dependencies": { + "ansi-styles": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.2.1.tgz", + "integrity": "sha512-9VGjrMsG1vePxcSweQsN20KY/c4zN0h9fLjqAbwbPfahM3t+NL+M9HC8xeXG2I8pX5NoamTGNuomEUFI7fcUjA==", + "requires": { + "@types/color-name": "^1.1.1", + "color-convert": "^2.0.1" + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" + }, "supports-color": { - "version": "5.5.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", - "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", - "dev": true, + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.1.0.tgz", + "integrity": "sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==", "requires": { - "has-flag": "^3.0.0" + "has-flag": "^4.0.0" } } } @@ -844,6 +910,11 @@ } } }, + "cluster-key-slot": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.0.tgz", + "integrity": "sha512-2Nii8p3RwAPiFwsnZvukotvow2rIHM+yQ6ZcBXGHdniadkYGZYiGmkHJIbZPIV9nfv7m/U1IPMVVcAhoWFeklw==" + }, "collection-visit": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/collection-visit/-/collection-visit-1.0.0.tgz", @@ -1057,6 +1128,11 @@ } } }, + "denque": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/denque/-/denque-1.4.1.tgz", + "integrity": "sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ==" + }, "depd": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", @@ -1208,6 +1284,17 @@ "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", "dev": true }, + "chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "dev": true, + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + } + }, "debug": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", @@ -1249,6 +1336,15 @@ "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.0.1.tgz", "integrity": "sha512-VTyMAUfdm047mwKl+u79WIdrZxtFtn+nBxHeb844XBQ9uMNTuTHdx2hc5RiAJYqwTj3wc/xe5HLSdJSkJ+WfZw==", "dev": true + }, + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dev": true, + "requires": { + "has-flag": "^3.0.0" + } } } }, @@ -2593,6 +2689,37 @@ } } }, + "ioredis": { + "version": "4.16.3", + "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-4.16.3.tgz", + "integrity": "sha512-Ejvcs2yW19Vq8AipvbtfcX3Ig8XG9EAyFOvGbhI/Q1QoVOK9ZdgY092kdOyOWIYBnPHjfjMJhU9qhsnp0i0K1w==", + "requires": { + "cluster-key-slot": "^1.1.0", + "debug": "^4.1.1", + "denque": "^1.1.0", + "lodash.defaults": "^4.2.0", + "lodash.flatten": "^4.4.0", + "redis-commands": "1.5.0", + "redis-errors": "^1.2.0", + "redis-parser": "^3.0.0", + "standard-as-callback": "^2.0.1" + }, + "dependencies": { + "debug": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", + "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "requires": { + "ms": "^2.1.1" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, "ipaddr.js": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.0.tgz", @@ -2963,6 +3090,16 @@ "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", "dev": true }, + "lodash.defaults": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz", + "integrity": "sha1-0JF4cW/+pN3p5ft7N/bwgCJ0WAw=" + }, + "lodash.flatten": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/lodash.flatten/-/lodash.flatten-4.4.0.tgz", + "integrity": "sha1-8xwiIlqWMtK7+OSt2+8kCqdlph8=" + }, "log-symbols": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-3.0.0.tgz", @@ -2970,6 +3107,28 @@ "dev": true, "requires": { "chalk": "^2.4.2" + }, + "dependencies": { + "chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "dev": true, + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + } + }, + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dev": true, + "requires": { + "has-flag": "^3.0.0" + } + } } }, "lolex": { @@ -3556,6 +3715,28 @@ "read-pkg": "^3.0.0", "shell-quote": "^1.6.1", "string.prototype.padend": "^3.0.0" + }, + "dependencies": { + "chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "dev": true, + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + } + }, + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dev": true, + "requires": { + "has-flag": "^3.0.0" + } + } } }, "npm-run-path": { @@ -4061,6 +4242,24 @@ "readable-stream": "^2.0.2" } }, + "redis-commands": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.5.0.tgz", + "integrity": "sha512-6KxamqpZ468MeQC3bkWmCB1fp56XL64D4Kf0zJSwDZbVLLm7KFkoIcHrgRvQ+sk8dnhySs7+yBg94yIkAK7aJg==" + }, + "redis-errors": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", + "integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=" + }, + "redis-parser": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", + "integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=", + "requires": { + "redis-errors": "^1.0.0" + } + }, "regex-not": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/regex-not/-/regex-not-1.0.2.tgz", @@ -4564,6 +4763,17 @@ "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", "dev": true }, + "stack-trace": { + "version": "0.0.10", + "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", + "integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=", + "dev": true + }, + "standard-as-callback": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/standard-as-callback/-/standard-as-callback-2.0.1.tgz", + "integrity": "sha512-NQOxSeB8gOI5WjSaxjBgog2QFw55FV8TkS6Y07BiB3VJ8xNTvUYm0wl0s8ObgQ5NhdpnNfigMIKjgPESzgr4tg==" + }, "static-extend": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/static-extend/-/static-extend-0.1.2.tgz", @@ -4995,6 +5205,28 @@ "latest-version": "^3.0.0", "semver-diff": "^2.0.0", "xdg-basedir": "^3.0.0" + }, + "dependencies": { + "chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "dev": true, + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + } + }, + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dev": true, + "requires": { + "has-flag": "^3.0.0" + } + } } }, "uri-js": { diff --git a/package.json b/package.json index 42bd9975a..89db392b3 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "tsc": "tsc", "prebuild": "npm run lint", "test": "npm run lint && mocha -r ts-node/register \"test/**/*\"", - "start": "bin/peerjs --port ${PORT:=9000}", + "start": "bin/peerjs --port ${PORT:=9000} --path /myapp", "dev:start": "npm-run-all build start", "dev": "nodemon --watch src -e ts --exec npm run dev:start" }, @@ -46,8 +46,10 @@ "@types/express": "^4.17.3", "@types/ws": "^7.2.3", "body-parser": "^1.19.0", + "chalk": "^4.0.0", "cors": "^2.8.5", "express": "^4.17.1", + "ioredis": "^4.16.3", "uuid": "^3.4.0", "ws": "^7.2.3", "yargs": "^15.3.1" @@ -67,6 +69,7 @@ "npm-run-all": "^4.1.5", "rimraf": "^3.0.2", "sinon": "^7.5.0", + "stack-trace": "0.0.10", "ts-node": "^8.7.0", "typescript": "^3.8.3" }, diff --git a/src/api/v1/calls/index.ts b/src/api/v1/calls/index.ts index ea42f8c13..9e48bd07f 100644 --- a/src/api/v1/calls/index.ts +++ b/src/api/v1/calls/index.ts @@ -3,12 +3,24 @@ import { IMessageHandler } from "../../../messageHandler"; import { IMessage } from "../../../models/message"; import { IRealm } from "../../../models/realm"; -export default ({ realm, messageHandler }: { realm: IRealm, messageHandler: IMessageHandler; }): express.Router => { +export default ({ + realm, + messageHandler, +}: { + realm: IRealm; + messageHandler: IMessageHandler; +}): express.Router => { const app = express.Router(); - const handle = (req: express.Request, res: express.Response, next: express.NextFunction): any => { + const handle = ( + req: express.Request, + res: express.Response, + next: express.NextFunction + ): any => { const { id } = req.params; + console.log("Got request..."); + if (!id) return next(); const client = realm.getClientById(id); @@ -23,7 +35,7 @@ export default ({ realm, messageHandler }: { realm: IRealm, messageHandler: IMes type, src: id, dst, - payload + payload, }; messageHandler.handle(client, message); diff --git a/src/messageHandler/index.ts b/src/messageHandler/index.ts index 7f6c67a26..92ddd55f9 100644 --- a/src/messageHandler/index.ts +++ b/src/messageHandler/index.ts @@ -11,11 +11,17 @@ export interface IMessageHandler { } export class MessageHandler implements IMessageHandler { - constructor(realm: IRealm, private readonly handlersRegistry: IHandlersRegistry = new HandlersRegistry()) { + constructor( + realm: IRealm, + private readonly handlersRegistry: IHandlersRegistry = new HandlersRegistry() + ) { const transmissionHandler: Handler = TransmissionHandler({ realm }); const heartbeatHandler: Handler = HeartbeatHandler; - const handleTransmission: Handler = (client: IClient | undefined, { type, src, dst, payload }: IMessage): boolean => { + const handleTransmission: Handler = ( + client: IClient | undefined, + { type, src, dst, payload }: IMessage + ): boolean => { return transmissionHandler(client, { type, src, @@ -24,17 +30,37 @@ export class MessageHandler implements IMessageHandler { }); }; - const handleHeartbeat = (client: IClient | undefined, message: IMessage) => heartbeatHandler(client, message); + const handleHeartbeat = (client: IClient | undefined, message: IMessage) => + heartbeatHandler(client, message); - this.handlersRegistry.registerHandler(MessageType.HEARTBEAT, handleHeartbeat); - this.handlersRegistry.registerHandler(MessageType.OFFER, handleTransmission); - this.handlersRegistry.registerHandler(MessageType.ANSWER, handleTransmission); - this.handlersRegistry.registerHandler(MessageType.CANDIDATE, handleTransmission); - this.handlersRegistry.registerHandler(MessageType.LEAVE, handleTransmission); - this.handlersRegistry.registerHandler(MessageType.EXPIRE, handleTransmission); + this.handlersRegistry.registerHandler( + MessageType.HEARTBEAT, + handleHeartbeat + ); + this.handlersRegistry.registerHandler( + MessageType.OFFER, + handleTransmission + ); + this.handlersRegistry.registerHandler( + MessageType.ANSWER, + handleTransmission + ); + this.handlersRegistry.registerHandler( + MessageType.CANDIDATE, + handleTransmission + ); + this.handlersRegistry.registerHandler( + MessageType.LEAVE, + handleTransmission + ); + this.handlersRegistry.registerHandler( + MessageType.EXPIRE, + handleTransmission + ); } public handle(client: IClient | undefined, message: IMessage): boolean { + console.log("Handle Message"); return this.handlersRegistry.handle(client, message); } } diff --git a/src/models/client.ts b/src/models/client.ts index 4dee3ca89..a3157a6d9 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -22,7 +22,8 @@ export class Client implements IClient { private socket: MyWebSocket | null = null; private lastPing: number = new Date().getTime(); - constructor({ id, token }: { id: string, token: string; }) { + constructor({ id, token }: { id: string; token: string; }) { + console.log("Create a client"); this.id = id; this.token = token; } diff --git a/src/models/realm.ts b/src/models/realm.ts index 5b73fc2d9..c5bbafadb 100644 --- a/src/models/realm.ts +++ b/src/models/realm.ts @@ -2,6 +2,12 @@ import uuidv4 from "uuid/v4"; import { IClient } from "./client"; import { IMessage } from "./message"; import { IMessageQueue, MessageQueue } from "./messageQueue"; +import { clog } from "../utils"; + +const Redis = require("ioredis"); + +// const redisPub = new Redis(); +const redisSub = new Redis(); export interface IRealm { getClientsIds(): string[]; @@ -27,6 +33,12 @@ export class Realm implements IRealm { private readonly clients: Map = new Map(); private readonly messageQueues: Map = new Map(); + constructor() { + redisSub.subscribe("clients", (err: Error) => { + if (!err) clog("Subscribed to Clients"); + }); + } + public getClientsIds(): string[] { return [...this.clients.keys()]; } @@ -54,10 +66,12 @@ export class Realm implements IRealm { } public getMessageQueueById(id: string): IMessageQueue | undefined { + console.log("Getting MessageQueue"); return this.messageQueues.get(id); } public addMessageToQueue(id: string, message: IMessage): void { + console.log("Add MessageQueue"); if (!this.getMessageQueueById(id)) { this.messageQueues.set(id, new MessageQueue()); } @@ -70,7 +84,6 @@ export class Realm implements IRealm { } public generateClientId(generateClientId?: () => string): string { - const generateId = generateClientId ? generateClientId : uuidv4; let clientId = generateId(); @@ -78,6 +91,7 @@ export class Realm implements IRealm { while (this.getClientById(clientId)) { clientId = generateId(); } + console.log("Generate ID", clientId); return clientId; } diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts index 3b237a17a..4db0a23bb 100644 --- a/src/services/webSocketServer/index.ts +++ b/src/services/webSocketServer/index.ts @@ -18,18 +18,25 @@ interface IAuthParams { key?: string; } -type CustomConfig = Pick; +type CustomConfig = Pick; -const WS_PATH = 'peerjs'; +const WS_PATH = "peerjs"; export class WebSocketServer extends EventEmitter implements IWebSocketServer { - public readonly path: string; private readonly realm: IRealm; private readonly config: CustomConfig; public readonly socketServer: WebSocketLib.Server; - constructor({ server, realm, config }: { server: any, realm: IRealm, config: CustomConfig; }) { + constructor({ + server, + realm, + config, + }: { + server: any; + realm: IRealm; + config: CustomConfig; + }) { super(); this.setMaxListeners(0); @@ -38,11 +45,13 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { this.config = config; const path = this.config.path; - this.path = `${path}${path.endsWith('/') ? "" : "/"}${WS_PATH}`; + this.path = `${path}${path.endsWith("/") ? "" : "/"}${WS_PATH}`; this.socketServer = new WebSocketLib.Server({ path: this.path, server }); - this.socketServer.on("connection", (socket: MyWebSocket, req) => this._onSocketConnection(socket, req)); + this.socketServer.on("connection", (socket: MyWebSocket, req) => + this._onSocketConnection(socket, req) + ); this.socketServer.on("error", (error: Error) => this._onSocketError(error)); } @@ -64,10 +73,12 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { if (client) { if (token !== client.getToken()) { // ID-taken, invalid token - socket.send(JSON.stringify({ - type: MessageType.ID_TAKEN, - payload: { msg: "ID is taken" } - })); + socket.send( + JSON.stringify({ + type: MessageType.ID_TAKEN, + payload: { msg: "ID is taken" }, + }) + ); return socket.close(); } @@ -83,12 +94,15 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { this.emit("error", error); } - private _registerClient({ socket, id, token }: - { - socket: MyWebSocket; - id: string; - token: string; - }): void { + private _registerClient({ + socket, + id, + token, + }: { + socket: MyWebSocket; + id: string; + token: string; + }): void { // Check concurrent limit const clientsCount = this.realm.getClientsIds().length; @@ -96,6 +110,7 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { return this._sendErrorAndClose(socket, Errors.CONNECTION_LIMIT_EXCEED); } + console.log("Registering New Client", JSON.stringify({ id, token })); const newClient: IClient = new Client({ id, token }); this.realm.setClient(newClient, id); socket.send(JSON.stringify({ type: MessageType.OPEN })); @@ -119,6 +134,7 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { try { const message = JSON.parse(data as string); + console.log("WSS::New Message from Client"); message.src = client.getId(); this.emit("message", client, message); @@ -134,7 +150,7 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { socket.send( JSON.stringify({ type: MessageType.ERROR, - payload: { msg } + payload: { msg }, }) ); diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 000000000..14a5bcdf8 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,17 @@ +const chalk = require("chalk"); +const stackTrace = require("stack-trace"); + +export const clog = (message: string) => { + console.log(chalk.blue(message)); +}; + +export const trace = () => { + stackTrace + .get() + .filter((site: any) => !site.getFileName().includes("node_modules")) + .map((site: any) => { + console.log( + chalk.red(`${site.getFileName()} --- ${site.getLineNumber()}`) + ); + }); +}; From de243f5d2b5eddd89629acbca424f1d02313e0ea Mon Sep 17 00:00:00 2001 From: sourav Date: Sat, 16 May 2020 17:47:28 +0530 Subject: [PATCH 02/18] Pub Sub clients --- dist/src/models/realm.js | 22 ++++++++++++++++++ dist/src/services/webSocketServer/index.js | 2 +- src/models/realm.ts | 27 +++++++++++++++++++++- src/services/webSocketServer/index.ts | 5 +++- 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/dist/src/models/realm.js b/dist/src/models/realm.js index ed68c8504..f327ae217 100644 --- a/dist/src/models/realm.js +++ b/dist/src/models/realm.js @@ -4,11 +4,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); const v4_1 = __importDefault(require("uuid/v4")); +const client_1 = require("./client"); const messageQueue_1 = require("./messageQueue"); const utils_1 = require("../utils"); const Redis = require("ioredis"); +const os = require("os"); // const redisPub = new Redis(); const redisSub = new Redis(); +const redisPub = new Redis(); class Realm { constructor() { this.clients = new Map(); @@ -17,6 +20,19 @@ class Realm { if (!err) utils_1.clog("Subscribed to Clients"); }); + redisSub.on("message", (channel, message) => { + if (channel === "clients") { + const { client, id, host } = JSON.parse(message); + if (host == os.hostname()) { + utils_1.clog("Same Host -------> Return"); + return; + } + const { token, lastPing } = client; + const newClient = new client_1.Client({ id, token }); + newClient.setLastPing(lastPing); + this.clients.set(id, newClient); + } + }); } getClientsIds() { return [...this.clients.keys()]; @@ -29,6 +45,12 @@ class Realm { } setClient(client, id) { this.clients.set(id, client); + utils_1.clog("Publish Client"); + redisPub.publish("clients", JSON.stringify({ + client, + id, + host: os.hostname(), + })); } removeClientById(id) { const client = this.getClientById(id); diff --git a/dist/src/services/webSocketServer/index.js b/dist/src/services/webSocketServer/index.js index f24c3f642..90049c347 100644 --- a/dist/src/services/webSocketServer/index.js +++ b/dist/src/services/webSocketServer/index.js @@ -54,7 +54,7 @@ class WebSocketServer extends events_1.default { if (clientsCount >= this.config.concurrent_limit) { return this._sendErrorAndClose(socket, enums_1.Errors.CONNECTION_LIMIT_EXCEED); } - console.log("Registering New Client", JSON.stringify({ id, token })); + console.log("Registering New Client::", JSON.stringify({ id, token, socket })); const newClient = new client_1.Client({ id, token }); this.realm.setClient(newClient, id); socket.send(JSON.stringify({ type: enums_1.MessageType.OPEN })); diff --git a/src/models/realm.ts b/src/models/realm.ts index c5bbafadb..d660611fe 100644 --- a/src/models/realm.ts +++ b/src/models/realm.ts @@ -1,13 +1,15 @@ import uuidv4 from "uuid/v4"; -import { IClient } from "./client"; +import { IClient, Client } from "./client"; import { IMessage } from "./message"; import { IMessageQueue, MessageQueue } from "./messageQueue"; import { clog } from "../utils"; const Redis = require("ioredis"); +const os = require("os"); // const redisPub = new Redis(); const redisSub = new Redis(); +const redisPub = new Redis(); export interface IRealm { getClientsIds(): string[]; @@ -37,6 +39,20 @@ export class Realm implements IRealm { redisSub.subscribe("clients", (err: Error) => { if (!err) clog("Subscribed to Clients"); }); + + redisSub.on("message", (channel: string, message: any) => { + if (channel === "clients") { + const { client, id, host } = JSON.parse(message); + if (host == os.hostname()) { + clog("Same Host -------> Return"); + return; + } + const { token, lastPing } = client; + const newClient: IClient = new Client({ id, token }); + newClient.setLastPing(lastPing); + this.clients.set(id, newClient); + } + }); } public getClientsIds(): string[] { @@ -53,6 +69,15 @@ export class Realm implements IRealm { public setClient(client: IClient, id: string): void { this.clients.set(id, client); + clog("Publish Client"); + redisPub.publish( + "clients", + JSON.stringify({ + client, + id, + host: os.hostname(), + }) + ); } public removeClientById(id: string): boolean { diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts index 4db0a23bb..929035d86 100644 --- a/src/services/webSocketServer/index.ts +++ b/src/services/webSocketServer/index.ts @@ -110,7 +110,10 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { return this._sendErrorAndClose(socket, Errors.CONNECTION_LIMIT_EXCEED); } - console.log("Registering New Client", JSON.stringify({ id, token })); + console.log( + "Registering New Client::", + JSON.stringify({ id, token, socket }) + ); const newClient: IClient = new Client({ id, token }); this.realm.setClient(newClient, id); socket.send(JSON.stringify({ type: MessageType.OPEN })); From 11d0dadbee1c784bfa6248ce0f3a44f70aabc620 Mon Sep 17 00:00:00 2001 From: sourav Date: Sat, 16 May 2020 17:58:40 +0530 Subject: [PATCH 03/18] Added SSL Certs in CLI --- package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 89db392b3..133d42f9f 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,10 @@ "test": "npm run lint && mocha -r ts-node/register \"test/**/*\"", "start": "bin/peerjs --port ${PORT:=9000} --path /myapp", "dev:start": "npm-run-all build start", - "dev": "nodemon --watch src -e ts --exec npm run dev:start" + "dev": "nodemon --watch src -e ts --exec npm run dev:start", + "startp": "bin/peerjs --port ${PORT:=9000} --path /myapp --sslkey ../certs/frontm_ai.key --sslcert ../certs/frontm_ai.crt", + "prod:start": "npm-run-all build startp", + "prod": "nodemon --watch src -e ts --exec npm run prod:start" }, "release": { "branch": "master" From 9d419d2f524888eb6034c731ba57ac24fd3299ef Mon Sep 17 00:00:00 2001 From: sourav Date: Sat, 16 May 2020 18:17:21 +0530 Subject: [PATCH 04/18] Added Config for Redis --- dist/src/models/realm.js | 8 ++++++-- package.json | 4 ++-- src/models/realm.ts | 10 ++++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/dist/src/models/realm.js b/dist/src/models/realm.js index f327ae217..f908e560f 100644 --- a/dist/src/models/realm.js +++ b/dist/src/models/realm.js @@ -9,9 +9,13 @@ const messageQueue_1 = require("./messageQueue"); const utils_1 = require("../utils"); const Redis = require("ioredis"); const os = require("os"); +const redisHost = process.env.NODE_ENV === "development" + ? "127.0.0.1" + : "fmqueue.7piuva.ng.0001.use1.cache.amazonaws.com"; +const redisPort = 6379; // const redisPub = new Redis(); -const redisSub = new Redis(); -const redisPub = new Redis(); +const redisSub = new Redis(redisPort, redisHost); +const redisPub = new Redis(redisPort, redisHost); class Realm { constructor() { this.clients = new Map(); diff --git a/package.json b/package.json index 133d42f9f..a7eadd6e3 100644 --- a/package.json +++ b/package.json @@ -36,10 +36,10 @@ "test": "npm run lint && mocha -r ts-node/register \"test/**/*\"", "start": "bin/peerjs --port ${PORT:=9000} --path /myapp", "dev:start": "npm-run-all build start", - "dev": "nodemon --watch src -e ts --exec npm run dev:start", + "dev": "NODE_ENV=development nodemon --watch src -e ts --exec npm run dev:start", "startp": "bin/peerjs --port ${PORT:=9000} --path /myapp --sslkey ../certs/frontm_ai.key --sslcert ../certs/frontm_ai.crt", "prod:start": "npm-run-all build startp", - "prod": "nodemon --watch src -e ts --exec npm run prod:start" + "prod": "NODE_ENV=production nodemon --watch src -e ts --exec npm run prod:start" }, "release": { "branch": "master" diff --git a/src/models/realm.ts b/src/models/realm.ts index d660611fe..f8c69af81 100644 --- a/src/models/realm.ts +++ b/src/models/realm.ts @@ -7,9 +7,15 @@ import { clog } from "../utils"; const Redis = require("ioredis"); const os = require("os"); +const redisHost = + process.env.NODE_ENV === "development" + ? "127.0.0.1" + : "fmqueue.7piuva.ng.0001.use1.cache.amazonaws.com"; +const redisPort = 6379; + // const redisPub = new Redis(); -const redisSub = new Redis(); -const redisPub = new Redis(); +const redisSub = new Redis(redisPort, redisHost); +const redisPub = new Redis(redisPort, redisHost); export interface IRealm { getClientsIds(): string[]; From f81d147a997d58cc65715fea28826550fc33fe6d Mon Sep 17 00:00:00 2001 From: sourav Date: Sat, 16 May 2020 23:43:28 +0530 Subject: [PATCH 05/18] PubSub for messaginf --- dist/src/models/realm.js | 23 +++++++--- dist/src/services/webSocketServer/index.js | 36 ++++++++++++++- src/models/realm.ts | 34 +++++++++++---- src/services/webSocketServer/index.ts | 51 +++++++++++++++++++--- 4 files changed, 124 insertions(+), 20 deletions(-) diff --git a/dist/src/models/realm.js b/dist/src/models/realm.js index f908e560f..10f388dab 100644 --- a/dist/src/models/realm.js +++ b/dist/src/models/realm.js @@ -26,15 +26,23 @@ class Realm { }); redisSub.on("message", (channel, message) => { if (channel === "clients") { - const { client, id, host } = JSON.parse(message); + const { client = null, id = null, host = null, action = null, } = JSON.parse(message); if (host == os.hostname()) { utils_1.clog("Same Host -------> Return"); return; } const { token, lastPing } = client; - const newClient = new client_1.Client({ id, token }); - newClient.setLastPing(lastPing); - this.clients.set(id, newClient); + if (action === "set") { + const newClient = new client_1.Client({ id, token }); + newClient.setLastPing(lastPing); + this.clients.set(id, newClient); + } + if (action === "delete") { + const client = this.getClientById(id); + if (!client) + return false; + this.clients.delete(id); + } } }); } @@ -49,11 +57,11 @@ class Realm { } setClient(client, id) { this.clients.set(id, client); - utils_1.clog("Publish Client"); redisPub.publish("clients", JSON.stringify({ client, id, host: os.hostname(), + action: "set", })); } removeClientById(id) { @@ -61,6 +69,11 @@ class Realm { if (!client) return false; this.clients.delete(id); + redisPub.publish("clients", JSON.stringify({ + id, + host: os.hostname(), + action: "delete", + })); return true; } getMessageQueueById(id) { diff --git a/dist/src/services/webSocketServer/index.js b/dist/src/services/webSocketServer/index.js index 90049c347..3f861bfb1 100644 --- a/dist/src/services/webSocketServer/index.js +++ b/dist/src/services/webSocketServer/index.js @@ -8,6 +8,16 @@ const url_1 = __importDefault(require("url")); const ws_1 = __importDefault(require("ws")); const enums_1 = require("../../enums"); const client_1 = require("../../models/client"); +const utils_1 = require("../../utils"); +const Redis = require("ioredis"); +const os = require("os"); +const redisHost = process.env.NODE_ENV === "development" + ? "127.0.0.1" + : "fmqueue.7piuva.ng.0001.use1.cache.amazonaws.com"; +const redisPort = 6379; +// const redisPub = new Redis(); +const redisSub = new Redis(redisPort, redisHost); +const redisPub = new Redis(redisPort, redisHost); const WS_PATH = "peerjs"; class WebSocketServer extends events_1.default { constructor({ server, realm, config, }) { @@ -20,6 +30,25 @@ class WebSocketServer extends events_1.default { this.socketServer = new ws_1.default.Server({ path: this.path, server }); this.socketServer.on("connection", (socket, req) => this._onSocketConnection(socket, req)); this.socketServer.on("error", (error) => this._onSocketError(error)); + redisSub.subscribe("ws_message", (err) => { + if (!err) + utils_1.clog("Subscribed to WebSocket Messages"); + }); + redisSub.on("message", (channel, message) => { + if (channel === "ws_message") { + const { id = null, host = null, socket_message = null } = JSON.parse(message); + utils_1.clog(`WS_MESSAGE::: ${message}`); + if (host == os.hostname()) { + utils_1.clog("Same Host -------> Return"); + return; + } + utils_1.clog("Parsing WS_MESSAGE and raising Event"); + const ws_message = JSON.parse(socket_message); + ws_message.src = id; + const other_client = this.realm.getClientById(id); + this.emit("message", other_client, ws_message); + } + }); } _onSocketConnection(socket, req) { const { query = {} } = url_1.default.parse(req.url, true); @@ -54,7 +83,6 @@ class WebSocketServer extends events_1.default { if (clientsCount >= this.config.concurrent_limit) { return this._sendErrorAndClose(socket, enums_1.Errors.CONNECTION_LIMIT_EXCEED); } - console.log("Registering New Client::", JSON.stringify({ id, token, socket })); const newClient = new client_1.Client({ id, token }); this.realm.setClient(newClient, id); socket.send(JSON.stringify({ type: enums_1.MessageType.OPEN })); @@ -73,8 +101,12 @@ class WebSocketServer extends events_1.default { socket.on("message", (data) => { try { const message = JSON.parse(data); - console.log("WSS::New Message from Client"); message.src = client.getId(); + redisPub.publish("ws_message", JSON.stringify({ + id: client.getId(), + host: os.hostname(), + socket_message: message, + })); this.emit("message", client, message); } catch (e) { diff --git a/src/models/realm.ts b/src/models/realm.ts index f8c69af81..807038803 100644 --- a/src/models/realm.ts +++ b/src/models/realm.ts @@ -48,15 +48,28 @@ export class Realm implements IRealm { redisSub.on("message", (channel: string, message: any) => { if (channel === "clients") { - const { client, id, host } = JSON.parse(message); + const { + client = null, + id = null, + host = null, + action = null, + } = JSON.parse(message); if (host == os.hostname()) { clog("Same Host -------> Return"); return; } const { token, lastPing } = client; - const newClient: IClient = new Client({ id, token }); - newClient.setLastPing(lastPing); - this.clients.set(id, newClient); + if (action === "set") { + const newClient: IClient = new Client({ id, token }); + newClient.setLastPing(lastPing); + this.clients.set(id, newClient); + } + + if (action === "delete") { + const client = this.getClientById(id); + if (!client) return false; + this.clients.delete(id); + } } }); } @@ -75,24 +88,29 @@ export class Realm implements IRealm { public setClient(client: IClient, id: string): void { this.clients.set(id, client); - clog("Publish Client"); redisPub.publish( "clients", JSON.stringify({ client, id, host: os.hostname(), + action: "set", }) ); } public removeClientById(id: string): boolean { const client = this.getClientById(id); - if (!client) return false; - this.clients.delete(id); - + redisPub.publish( + "clients", + JSON.stringify({ + id, + host: os.hostname(), + action: "delete", + }) + ); return true; } diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts index 929035d86..cd4f258e6 100644 --- a/src/services/webSocketServer/index.ts +++ b/src/services/webSocketServer/index.ts @@ -7,6 +7,20 @@ import { Errors, MessageType } from "../../enums"; import { Client, IClient } from "../../models/client"; import { IRealm } from "../../models/realm"; import { MyWebSocket } from "./webSocket"; +import { clog } from "../../utils"; + +const Redis = require("ioredis"); +const os = require("os"); + +const redisHost = + process.env.NODE_ENV === "development" + ? "127.0.0.1" + : "fmqueue.7piuva.ng.0001.use1.cache.amazonaws.com"; +const redisPort = 6379; + +// const redisPub = new Redis(); +const redisSub = new Redis(redisPort, redisHost); +const redisPub = new Redis(redisPort, redisHost); export interface IWebSocketServer extends EventEmitter { readonly path: string; @@ -53,6 +67,30 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { this._onSocketConnection(socket, req) ); this.socketServer.on("error", (error: Error) => this._onSocketError(error)); + + redisSub.subscribe("ws_message", (err: Error) => { + if (!err) clog("Subscribed to WebSocket Messages"); + }); + + redisSub.on("message", (channel: string, message: any) => { + if (channel === "ws_message") { + const { id = null, host = null, socket_message = null } = JSON.parse( + message + ); + clog(`WS_MESSAGE::: ${message}`); + if (host == os.hostname()) { + clog("Same Host -------> Return"); + return; + } + + clog("Parsing WS_MESSAGE and raising Event"); + const ws_message = JSON.parse(socket_message as string); + ws_message.src = id; + const other_client = this.realm.getClientById(id); + + this.emit("message", other_client, ws_message); + } + }); } private _onSocketConnection(socket: MyWebSocket, req: IncomingMessage): void { @@ -110,10 +148,6 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { return this._sendErrorAndClose(socket, Errors.CONNECTION_LIMIT_EXCEED); } - console.log( - "Registering New Client::", - JSON.stringify({ id, token, socket }) - ); const newClient: IClient = new Client({ id, token }); this.realm.setClient(newClient, id); socket.send(JSON.stringify({ type: MessageType.OPEN })); @@ -137,8 +171,15 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { try { const message = JSON.parse(data as string); - console.log("WSS::New Message from Client"); message.src = client.getId(); + redisPub.publish( + "ws_message", + JSON.stringify({ + id: client.getId(), + host: os.hostname(), + socket_message: message, + }) + ); this.emit("message", client, message); } catch (e) { From 87cb9916260835dd0e467e93c403ebea8ca608a5 Mon Sep 17 00:00:00 2001 From: sourav Date: Sun, 17 May 2020 00:14:58 +0530 Subject: [PATCH 06/18] PubSub for messaginf --- dist/src/services/webSocketServer/index.js | 7 ++++--- src/services/webSocketServer/index.ts | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/dist/src/services/webSocketServer/index.js b/dist/src/services/webSocketServer/index.js index 3f861bfb1..ad0348ad0 100644 --- a/dist/src/services/webSocketServer/index.js +++ b/dist/src/services/webSocketServer/index.js @@ -40,12 +40,12 @@ class WebSocketServer extends events_1.default { utils_1.clog(`WS_MESSAGE::: ${message}`); if (host == os.hostname()) { utils_1.clog("Same Host -------> Return"); - return; + // return; } utils_1.clog("Parsing WS_MESSAGE and raising Event"); - const ws_message = JSON.parse(socket_message); - ws_message.src = id; + const ws_message = socket_message; const other_client = this.realm.getClientById(id); + utils_1.clog(`WS_MESSAGE:::${other_client}`); this.emit("message", other_client, ws_message); } }); @@ -102,6 +102,7 @@ class WebSocketServer extends events_1.default { try { const message = JSON.parse(data); message.src = client.getId(); + utils_1.clog(`Before Publish::: ${JSON.stringify(message)}`); redisPub.publish("ws_message", JSON.stringify({ id: client.getId(), host: os.hostname(), diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts index cd4f258e6..462ba4431 100644 --- a/src/services/webSocketServer/index.ts +++ b/src/services/webSocketServer/index.ts @@ -80,13 +80,13 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { clog(`WS_MESSAGE::: ${message}`); if (host == os.hostname()) { clog("Same Host -------> Return"); - return; + // return; } clog("Parsing WS_MESSAGE and raising Event"); - const ws_message = JSON.parse(socket_message as string); - ws_message.src = id; + const ws_message = socket_message; const other_client = this.realm.getClientById(id); + clog(`WS_MESSAGE:::${other_client}`); this.emit("message", other_client, ws_message); } @@ -172,6 +172,7 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { const message = JSON.parse(data as string); message.src = client.getId(); + clog(`Before Publish::: ${JSON.stringify(message)}`); redisPub.publish( "ws_message", JSON.stringify({ From 6072e263ee929468c73f59330c62e678f65762f6 Mon Sep 17 00:00:00 2001 From: sourav Date: Sun, 17 May 2020 00:32:34 +0530 Subject: [PATCH 07/18] PubSub for messaginf --- src/services/webSocketServer/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts index 462ba4431..3f21b5ccc 100644 --- a/src/services/webSocketServer/index.ts +++ b/src/services/webSocketServer/index.ts @@ -169,8 +169,8 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { // Handle messages from peers. socket.on("message", (data: WebSocketLib.Data) => { try { + clog("Main Server Message"); const message = JSON.parse(data as string); - message.src = client.getId(); clog(`Before Publish::: ${JSON.stringify(message)}`); redisPub.publish( From bc55ad6c939453fb09183c39624e7b0166fafa77 Mon Sep 17 00:00:00 2001 From: sourav Date: Sun, 17 May 2020 12:00:08 +0530 Subject: [PATCH 08/18] PubSub for messaginf --- .../handlers/transmission/index.js | 11 +++++++++-- dist/src/models/realm.js | 3 +++ dist/src/services/webSocketServer/index.js | 1 + dist/src/start.js | 16 ++++++++++++++++ .../handlers/transmission/index.ts | 16 ++++++++++++++-- src/models/realm.ts | 4 ++++ src/start.ts | 18 ++++++++++++++++++ 7 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 dist/src/start.js create mode 100644 src/start.ts diff --git a/dist/src/messageHandler/handlers/transmission/index.js b/dist/src/messageHandler/handlers/transmission/index.js index 5183eb472..410cace3d 100644 --- a/dist/src/messageHandler/handlers/transmission/index.js +++ b/dist/src/messageHandler/handlers/transmission/index.js @@ -1,22 +1,27 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const enums_1 = require("../../../enums"); -exports.TransmissionHandler = ({ realm }) => { +const utils_1 = require("../../../utils"); +exports.TransmissionHandler = ({ realm, }) => { const handle = (client, message) => { const type = message.type; const srcId = message.src; const dstId = message.dst; + utils_1.clog("Transmission:: Got message"); const destinationClient = realm.getClientById(dstId); // User is connected! if (destinationClient) { + utils_1.clog("Transmission:: Got Destination Client"); const socket = destinationClient.getSocket(); try { if (socket) { + utils_1.clog("Transmission:: Got Socket"); const data = JSON.stringify(message); socket.send(data); } else { // Neither socket no res available. Peer dead? + utils_1.clog("Transmission:: Peer Dead"); throw new Error("Peer dead"); } } @@ -25,15 +30,17 @@ exports.TransmissionHandler = ({ realm }) => { // the associated WebSocket has not closed. // Tell other side to stop trying. if (socket) { + utils_1.clog("Transmission:: Closing Socket Connection"); socket.close(); } else { + utils_1.clog("Transmission:: Removing Client"); realm.removeClientById(destinationClient.getId()); } handle(client, { type: enums_1.MessageType.LEAVE, src: dstId, - dst: srcId + dst: srcId, }); } } diff --git a/dist/src/models/realm.js b/dist/src/models/realm.js index 10f388dab..1b6e7db33 100644 --- a/dist/src/models/realm.js +++ b/dist/src/models/realm.js @@ -9,6 +9,9 @@ const messageQueue_1 = require("./messageQueue"); const utils_1 = require("../utils"); const Redis = require("ioredis"); const os = require("os"); +process.env.NODE_ENV = process.env.NODE_ENV + ? process.env.NODE_ENV + : "development"; const redisHost = process.env.NODE_ENV === "development" ? "127.0.0.1" : "fmqueue.7piuva.ng.0001.use1.cache.amazonaws.com"; diff --git a/dist/src/services/webSocketServer/index.js b/dist/src/services/webSocketServer/index.js index ad0348ad0..97e9b774e 100644 --- a/dist/src/services/webSocketServer/index.js +++ b/dist/src/services/webSocketServer/index.js @@ -100,6 +100,7 @@ class WebSocketServer extends events_1.default { // Handle messages from peers. socket.on("message", (data) => { try { + utils_1.clog("Main Server Message"); const message = JSON.parse(data); message.src = client.getId(); utils_1.clog(`Before Publish::: ${JSON.stringify(message)}`); diff --git a/dist/src/start.js b/dist/src/start.js new file mode 100644 index 000000000..7b7e41dbd --- /dev/null +++ b/dist/src/start.js @@ -0,0 +1,16 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const index_1 = require("./index"); +const config = { + port: 9000, + expire_timeout: 5000, + alive_timeout: 60000, + key: "peerjs", + path: "/myapp", + concurrent_limit: 5000, + allow_discovery: false, + proxied: false, + cleanup_out_msgs: 1000, +}; +index_1.PeerServer(config); +console.log("Server started at port 9000"); diff --git a/src/messageHandler/handlers/transmission/index.ts b/src/messageHandler/handlers/transmission/index.ts index bd31c707c..624386c4a 100644 --- a/src/messageHandler/handlers/transmission/index.ts +++ b/src/messageHandler/handlers/transmission/index.ts @@ -2,25 +2,35 @@ import { MessageType } from "../../../enums"; import { IClient } from "../../../models/client"; import { IMessage } from "../../../models/message"; import { IRealm } from "../../../models/realm"; +import { clog } from "../../../utils"; -export const TransmissionHandler = ({ realm }: { realm: IRealm; }): (client: IClient | undefined, message: IMessage) => boolean => { +export const TransmissionHandler = ({ + realm, +}: { + realm: IRealm; +}): ((client: IClient | undefined, message: IMessage) => boolean) => { const handle = (client: IClient | undefined, message: IMessage) => { const type = message.type; const srcId = message.src; const dstId = message.dst; + clog("Transmission:: Got message"); + const destinationClient = realm.getClientById(dstId); // User is connected! if (destinationClient) { + clog("Transmission:: Got Destination Client"); const socket = destinationClient.getSocket(); try { if (socket) { + clog("Transmission:: Got Socket"); const data = JSON.stringify(message); socket.send(data); } else { // Neither socket no res available. Peer dead? + clog("Transmission:: Peer Dead"); throw new Error("Peer dead"); } } catch (e) { @@ -28,15 +38,17 @@ export const TransmissionHandler = ({ realm }: { realm: IRealm; }): (client: ICl // the associated WebSocket has not closed. // Tell other side to stop trying. if (socket) { + clog("Transmission:: Closing Socket Connection"); socket.close(); } else { + clog("Transmission:: Removing Client"); realm.removeClientById(destinationClient.getId()); } handle(client, { type: MessageType.LEAVE, src: dstId, - dst: srcId + dst: srcId, }); } } else { diff --git a/src/models/realm.ts b/src/models/realm.ts index 807038803..553a10aa4 100644 --- a/src/models/realm.ts +++ b/src/models/realm.ts @@ -7,6 +7,10 @@ import { clog } from "../utils"; const Redis = require("ioredis"); const os = require("os"); +process.env.NODE_ENV = process.env.NODE_ENV + ? process.env.NODE_ENV + : "development"; + const redisHost = process.env.NODE_ENV === "development" ? "127.0.0.1" diff --git a/src/start.ts b/src/start.ts new file mode 100644 index 000000000..a0099d0c5 --- /dev/null +++ b/src/start.ts @@ -0,0 +1,18 @@ +import { PeerServer } from "./index"; +import { IConfig } from "./config"; + +const config: IConfig = { + port: 9000, + expire_timeout: 5000, + alive_timeout: 60000, + key: "peerjs", + path: "/myapp", + concurrent_limit: 5000, + allow_discovery: false, + proxied: false, + cleanup_out_msgs: 1000, +}; + +PeerServer(config); + +console.log("Server started at port 9000"); From 55d3e489ce066e4d2f4b9de3c8938c7f340d12a7 Mon Sep 17 00:00:00 2001 From: sourav Date: Sun, 17 May 2020 12:36:44 +0530 Subject: [PATCH 09/18] PubSub for messaginf --- src/services/webSocketServer/index.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts index 3f21b5ccc..b55615215 100644 --- a/src/services/webSocketServer/index.ts +++ b/src/services/webSocketServer/index.ts @@ -77,7 +77,6 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { const { id = null, host = null, socket_message = null } = JSON.parse( message ); - clog(`WS_MESSAGE::: ${message}`); if (host == os.hostname()) { clog("Same Host -------> Return"); // return; @@ -86,7 +85,6 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { clog("Parsing WS_MESSAGE and raising Event"); const ws_message = socket_message; const other_client = this.realm.getClientById(id); - clog(`WS_MESSAGE:::${other_client}`); this.emit("message", other_client, ws_message); } @@ -172,7 +170,6 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { clog("Main Server Message"); const message = JSON.parse(data as string); message.src = client.getId(); - clog(`Before Publish::: ${JSON.stringify(message)}`); redisPub.publish( "ws_message", JSON.stringify({ From bb635573d5905006d925372023f89fc120274bba Mon Sep 17 00:00:00 2001 From: sourav Date: Sun, 17 May 2020 12:38:17 +0530 Subject: [PATCH 10/18] PubSub for messaginf --- dist/src/services/webSocketServer/index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/dist/src/services/webSocketServer/index.js b/dist/src/services/webSocketServer/index.js index 97e9b774e..c6feed49e 100644 --- a/dist/src/services/webSocketServer/index.js +++ b/dist/src/services/webSocketServer/index.js @@ -37,7 +37,6 @@ class WebSocketServer extends events_1.default { redisSub.on("message", (channel, message) => { if (channel === "ws_message") { const { id = null, host = null, socket_message = null } = JSON.parse(message); - utils_1.clog(`WS_MESSAGE::: ${message}`); if (host == os.hostname()) { utils_1.clog("Same Host -------> Return"); // return; @@ -45,7 +44,6 @@ class WebSocketServer extends events_1.default { utils_1.clog("Parsing WS_MESSAGE and raising Event"); const ws_message = socket_message; const other_client = this.realm.getClientById(id); - utils_1.clog(`WS_MESSAGE:::${other_client}`); this.emit("message", other_client, ws_message); } }); @@ -103,7 +101,6 @@ class WebSocketServer extends events_1.default { utils_1.clog("Main Server Message"); const message = JSON.parse(data); message.src = client.getId(); - utils_1.clog(`Before Publish::: ${JSON.stringify(message)}`); redisPub.publish("ws_message", JSON.stringify({ id: client.getId(), host: os.hostname(), From 34a6b2b666a44a52e36bbabb437ffb7c24a9105f Mon Sep 17 00:00:00 2001 From: sourav Date: Sun, 17 May 2020 14:41:02 +0530 Subject: [PATCH 11/18] Removed PUBSUB for clients --- dist/src/instance.js | 19 +++--- dist/src/messageHandler/index.js | 1 - dist/src/services/webSocketServer/index.js | 34 +---------- src/instance.ts | 35 +++++++---- src/messageHandler/index.ts | 1 - src/models/realm.ts | 67 ---------------------- src/services/webSocketServer/index.ts | 45 +-------------- 7 files changed, 38 insertions(+), 164 deletions(-) diff --git a/dist/src/instance.js b/dist/src/instance.js index ca142e988..b2173fe48 100644 --- a/dist/src/instance.js +++ b/dist/src/instance.js @@ -10,32 +10,36 @@ const messagesExpire_1 = require("./services/messagesExpire"); const webSocketServer_1 = require("./services/webSocketServer"); const messageHandler_1 = require("./messageHandler"); const api_1 = require("./api"); -exports.createInstance = ({ app, server, options }) => { +exports.createInstance = ({ app, server, options, }) => { const config = options; const realm = new realm_1.Realm(); const messageHandler = new messageHandler_1.MessageHandler(realm); const api = api_1.Api({ config, realm, messageHandler }); - const messagesExpire = new messagesExpire_1.MessagesExpire({ realm, config, messageHandler }); + const messagesExpire = new messagesExpire_1.MessagesExpire({ + realm, + config, + messageHandler, + }); const checkBrokenConnections = new checkBrokenConnections_1.CheckBrokenConnections({ realm, config, - onClose: client => { + onClose: (client) => { app.emit("disconnect", client); - } + }, }); app.use(options.path, api); //use mountpath for WS server - const customConfig = Object.assign(Object.assign({}, config), { path: path_1.default.posix.join(app.path(), options.path, '/') }); + const customConfig = Object.assign(Object.assign({}, config), { path: path_1.default.posix.join(app.path(), options.path, "/") }); const wss = new webSocketServer_1.WebSocketServer({ server, realm, - config: customConfig + config: customConfig, }); wss.on("connection", (client) => { const messageQueue = realm.getMessageQueueById(client.getId()); if (messageQueue) { let message; - while (message = messageQueue.readMessage()) { + while ((message = messageQueue.readMessage())) { messageHandler.handle(client, message); } realm.clearMessageQueue(client.getId()); @@ -43,6 +47,7 @@ exports.createInstance = ({ app, server, options }) => { app.emit("connection", client); }); wss.on("message", (client, message) => { + console.log("In handle Peer Message", message); app.emit("message", client, message); messageHandler.handle(client, message); }); diff --git a/dist/src/messageHandler/index.js b/dist/src/messageHandler/index.js index 0782b8540..c8393cae6 100644 --- a/dist/src/messageHandler/index.js +++ b/dist/src/messageHandler/index.js @@ -25,7 +25,6 @@ class MessageHandler { this.handlersRegistry.registerHandler(enums_1.MessageType.EXPIRE, handleTransmission); } handle(client, message) { - console.log("Handle Message"); return this.handlersRegistry.handle(client, message); } } diff --git a/dist/src/services/webSocketServer/index.js b/dist/src/services/webSocketServer/index.js index c6feed49e..6ac8c62e3 100644 --- a/dist/src/services/webSocketServer/index.js +++ b/dist/src/services/webSocketServer/index.js @@ -8,16 +8,6 @@ const url_1 = __importDefault(require("url")); const ws_1 = __importDefault(require("ws")); const enums_1 = require("../../enums"); const client_1 = require("../../models/client"); -const utils_1 = require("../../utils"); -const Redis = require("ioredis"); -const os = require("os"); -const redisHost = process.env.NODE_ENV === "development" - ? "127.0.0.1" - : "fmqueue.7piuva.ng.0001.use1.cache.amazonaws.com"; -const redisPort = 6379; -// const redisPub = new Redis(); -const redisSub = new Redis(redisPort, redisHost); -const redisPub = new Redis(redisPort, redisHost); const WS_PATH = "peerjs"; class WebSocketServer extends events_1.default { constructor({ server, realm, config, }) { @@ -30,23 +20,6 @@ class WebSocketServer extends events_1.default { this.socketServer = new ws_1.default.Server({ path: this.path, server }); this.socketServer.on("connection", (socket, req) => this._onSocketConnection(socket, req)); this.socketServer.on("error", (error) => this._onSocketError(error)); - redisSub.subscribe("ws_message", (err) => { - if (!err) - utils_1.clog("Subscribed to WebSocket Messages"); - }); - redisSub.on("message", (channel, message) => { - if (channel === "ws_message") { - const { id = null, host = null, socket_message = null } = JSON.parse(message); - if (host == os.hostname()) { - utils_1.clog("Same Host -------> Return"); - // return; - } - utils_1.clog("Parsing WS_MESSAGE and raising Event"); - const ws_message = socket_message; - const other_client = this.realm.getClientById(id); - this.emit("message", other_client, ws_message); - } - }); } _onSocketConnection(socket, req) { const { query = {} } = url_1.default.parse(req.url, true); @@ -98,14 +71,9 @@ class WebSocketServer extends events_1.default { // Handle messages from peers. socket.on("message", (data) => { try { - utils_1.clog("Main Server Message"); + console.log("Received Message from Peer"); const message = JSON.parse(data); message.src = client.getId(); - redisPub.publish("ws_message", JSON.stringify({ - id: client.getId(), - host: os.hostname(), - socket_message: message, - })); this.emit("message", client, message); } catch (e) { diff --git a/src/instance.ts b/src/instance.ts index 1cd032e97..4030e3b98 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -1,6 +1,6 @@ import express from "express"; import { Server } from "net"; -import path from 'path'; +import path from "path"; import { IClient } from "./models/client"; import { IMessage } from "./models/message"; import { Realm } from "./models/realm"; @@ -12,9 +12,13 @@ import { MessageHandler } from "./messageHandler"; import { Api } from "./api"; import { IConfig } from "./config"; -export const createInstance = ({ app, server, options }: { - app: express.Application, - server: Server, +export const createInstance = ({ + app, + server, + options, +}: { + app: express.Application; + server: Server; options: IConfig; }): void => { const config = options; @@ -22,24 +26,31 @@ export const createInstance = ({ app, server, options }: { const messageHandler = new MessageHandler(realm); const api = Api({ config, realm, messageHandler }); - const messagesExpire: IMessagesExpire = new MessagesExpire({ realm, config, messageHandler }); + const messagesExpire: IMessagesExpire = new MessagesExpire({ + realm, + config, + messageHandler, + }); const checkBrokenConnections = new CheckBrokenConnections({ realm, config, - onClose: client => { + onClose: (client) => { app.emit("disconnect", client); - } + }, }); app.use(options.path, api); //use mountpath for WS server - const customConfig = { ...config, path: path.posix.join(app.path(), options.path, '/') }; + const customConfig = { + ...config, + path: path.posix.join(app.path(), options.path, "/"), + }; const wss: IWebSocketServer = new WebSocketServer({ server, realm, - config: customConfig + config: customConfig, }); wss.on("connection", (client: IClient) => { @@ -48,7 +59,7 @@ export const createInstance = ({ app, server, options }: { if (messageQueue) { let message: IMessage | undefined; - while (message = messageQueue.readMessage()) { + while ((message = messageQueue.readMessage())) { messageHandler.handle(client, message); } realm.clearMessageQueue(client.getId()); @@ -58,6 +69,7 @@ export const createInstance = ({ app, server, options }: { }); wss.on("message", (client: IClient, message: IMessage) => { + console.log("In handle Peer Message", message.type); app.emit("message", client, message); messageHandler.handle(client, message); }); @@ -72,4 +84,5 @@ export const createInstance = ({ app, server, options }: { messagesExpire.startMessagesExpiration(); checkBrokenConnections.start(); -}; \ No newline at end of file +}; + diff --git a/src/messageHandler/index.ts b/src/messageHandler/index.ts index 92ddd55f9..44150a865 100644 --- a/src/messageHandler/index.ts +++ b/src/messageHandler/index.ts @@ -60,7 +60,6 @@ export class MessageHandler implements IMessageHandler { } public handle(client: IClient | undefined, message: IMessage): boolean { - console.log("Handle Message"); return this.handlersRegistry.handle(client, message); } } diff --git a/src/models/realm.ts b/src/models/realm.ts index 553a10aa4..4948f7547 100644 --- a/src/models/realm.ts +++ b/src/models/realm.ts @@ -4,23 +4,6 @@ import { IMessage } from "./message"; import { IMessageQueue, MessageQueue } from "./messageQueue"; import { clog } from "../utils"; -const Redis = require("ioredis"); -const os = require("os"); - -process.env.NODE_ENV = process.env.NODE_ENV - ? process.env.NODE_ENV - : "development"; - -const redisHost = - process.env.NODE_ENV === "development" - ? "127.0.0.1" - : "fmqueue.7piuva.ng.0001.use1.cache.amazonaws.com"; -const redisPort = 6379; - -// const redisPub = new Redis(); -const redisSub = new Redis(redisPort, redisHost); -const redisPub = new Redis(redisPort, redisHost); - export interface IRealm { getClientsIds(): string[]; @@ -45,39 +28,6 @@ export class Realm implements IRealm { private readonly clients: Map = new Map(); private readonly messageQueues: Map = new Map(); - constructor() { - redisSub.subscribe("clients", (err: Error) => { - if (!err) clog("Subscribed to Clients"); - }); - - redisSub.on("message", (channel: string, message: any) => { - if (channel === "clients") { - const { - client = null, - id = null, - host = null, - action = null, - } = JSON.parse(message); - if (host == os.hostname()) { - clog("Same Host -------> Return"); - return; - } - const { token, lastPing } = client; - if (action === "set") { - const newClient: IClient = new Client({ id, token }); - newClient.setLastPing(lastPing); - this.clients.set(id, newClient); - } - - if (action === "delete") { - const client = this.getClientById(id); - if (!client) return false; - this.clients.delete(id); - } - } - }); - } - public getClientsIds(): string[] { return [...this.clients.keys()]; } @@ -92,29 +42,12 @@ export class Realm implements IRealm { public setClient(client: IClient, id: string): void { this.clients.set(id, client); - redisPub.publish( - "clients", - JSON.stringify({ - client, - id, - host: os.hostname(), - action: "set", - }) - ); } public removeClientById(id: string): boolean { const client = this.getClientById(id); if (!client) return false; this.clients.delete(id); - redisPub.publish( - "clients", - JSON.stringify({ - id, - host: os.hostname(), - action: "delete", - }) - ); return true; } diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts index b55615215..43e019f14 100644 --- a/src/services/webSocketServer/index.ts +++ b/src/services/webSocketServer/index.ts @@ -7,20 +7,8 @@ import { Errors, MessageType } from "../../enums"; import { Client, IClient } from "../../models/client"; import { IRealm } from "../../models/realm"; import { MyWebSocket } from "./webSocket"; -import { clog } from "../../utils"; - -const Redis = require("ioredis"); -const os = require("os"); - -const redisHost = - process.env.NODE_ENV === "development" - ? "127.0.0.1" - : "fmqueue.7piuva.ng.0001.use1.cache.amazonaws.com"; -const redisPort = 6379; // const redisPub = new Redis(); -const redisSub = new Redis(redisPort, redisHost); -const redisPub = new Redis(redisPort, redisHost); export interface IWebSocketServer extends EventEmitter { readonly path: string; @@ -67,28 +55,6 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { this._onSocketConnection(socket, req) ); this.socketServer.on("error", (error: Error) => this._onSocketError(error)); - - redisSub.subscribe("ws_message", (err: Error) => { - if (!err) clog("Subscribed to WebSocket Messages"); - }); - - redisSub.on("message", (channel: string, message: any) => { - if (channel === "ws_message") { - const { id = null, host = null, socket_message = null } = JSON.parse( - message - ); - if (host == os.hostname()) { - clog("Same Host -------> Return"); - // return; - } - - clog("Parsing WS_MESSAGE and raising Event"); - const ws_message = socket_message; - const other_client = this.realm.getClientById(id); - - this.emit("message", other_client, ws_message); - } - }); } private _onSocketConnection(socket: MyWebSocket, req: IncomingMessage): void { @@ -167,18 +133,9 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { // Handle messages from peers. socket.on("message", (data: WebSocketLib.Data) => { try { - clog("Main Server Message"); + console.log("Received Message from Peer"); const message = JSON.parse(data as string); message.src = client.getId(); - redisPub.publish( - "ws_message", - JSON.stringify({ - id: client.getId(), - host: os.hostname(), - socket_message: message, - }) - ); - this.emit("message", client, message); } catch (e) { this.emit("error", e); From 25e8862a48f9eec56b216bb385b12361f0f9e15c Mon Sep 17 00:00:00 2001 From: sourav Date: Sun, 17 May 2020 19:48:11 +0530 Subject: [PATCH 12/18] Distributed Message handling --- dist/src/instance.js | 2 +- dist/src/models/realm.js | 50 ---------------------- dist/src/services/webSocketServer/index.js | 21 ++++++++- src/instance.ts | 4 +- src/models/realm.ts | 3 +- src/services/webSocketServer/index.ts | 24 ++++++++++- 6 files changed, 46 insertions(+), 58 deletions(-) diff --git a/dist/src/instance.js b/dist/src/instance.js index b2173fe48..d6cde771a 100644 --- a/dist/src/instance.js +++ b/dist/src/instance.js @@ -48,7 +48,7 @@ exports.createInstance = ({ app, server, options, }) => { }); wss.on("message", (client, message) => { console.log("In handle Peer Message", message); - app.emit("message", client, message); + // app.emit("message", client, message); messageHandler.handle(client, message); }); wss.on("close", (client) => { diff --git a/dist/src/models/realm.js b/dist/src/models/realm.js index 1b6e7db33..258fc6f62 100644 --- a/dist/src/models/realm.js +++ b/dist/src/models/realm.js @@ -4,50 +4,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); const v4_1 = __importDefault(require("uuid/v4")); -const client_1 = require("./client"); const messageQueue_1 = require("./messageQueue"); -const utils_1 = require("../utils"); -const Redis = require("ioredis"); -const os = require("os"); -process.env.NODE_ENV = process.env.NODE_ENV - ? process.env.NODE_ENV - : "development"; -const redisHost = process.env.NODE_ENV === "development" - ? "127.0.0.1" - : "fmqueue.7piuva.ng.0001.use1.cache.amazonaws.com"; -const redisPort = 6379; -// const redisPub = new Redis(); -const redisSub = new Redis(redisPort, redisHost); -const redisPub = new Redis(redisPort, redisHost); class Realm { constructor() { this.clients = new Map(); this.messageQueues = new Map(); - redisSub.subscribe("clients", (err) => { - if (!err) - utils_1.clog("Subscribed to Clients"); - }); - redisSub.on("message", (channel, message) => { - if (channel === "clients") { - const { client = null, id = null, host = null, action = null, } = JSON.parse(message); - if (host == os.hostname()) { - utils_1.clog("Same Host -------> Return"); - return; - } - const { token, lastPing } = client; - if (action === "set") { - const newClient = new client_1.Client({ id, token }); - newClient.setLastPing(lastPing); - this.clients.set(id, newClient); - } - if (action === "delete") { - const client = this.getClientById(id); - if (!client) - return false; - this.clients.delete(id); - } - } - }); } getClientsIds() { return [...this.clients.keys()]; @@ -60,23 +21,12 @@ class Realm { } setClient(client, id) { this.clients.set(id, client); - redisPub.publish("clients", JSON.stringify({ - client, - id, - host: os.hostname(), - action: "set", - })); } removeClientById(id) { const client = this.getClientById(id); if (!client) return false; this.clients.delete(id); - redisPub.publish("clients", JSON.stringify({ - id, - host: os.hostname(), - action: "delete", - })); return true; } getMessageQueueById(id) { diff --git a/dist/src/services/webSocketServer/index.js b/dist/src/services/webSocketServer/index.js index 6ac8c62e3..f2640abd7 100644 --- a/dist/src/services/webSocketServer/index.js +++ b/dist/src/services/webSocketServer/index.js @@ -8,6 +8,9 @@ const url_1 = __importDefault(require("url")); const ws_1 = __importDefault(require("ws")); const enums_1 = require("../../enums"); const client_1 = require("../../models/client"); +const Redis = require("ioredis"); +const MessagePublisher = new Redis(); +const MessageSubscriber = new Redis(); const WS_PATH = "peerjs"; class WebSocketServer extends events_1.default { constructor({ server, realm, config, }) { @@ -20,6 +23,19 @@ class WebSocketServer extends events_1.default { this.socketServer = new ws_1.default.Server({ path: this.path, server }); this.socketServer.on("connection", (socket, req) => this._onSocketConnection(socket, req)); this.socketServer.on("error", (error) => this._onSocketError(error)); + MessageSubscriber.subscribe("transmission", (err) => { + if (!err) + console.log("Subscribed to Transmission messages"); + }); + MessageSubscriber.on("message", (channel, tmessage) => { + if (channel === "transmission") { + const receivedMessage = JSON.parse(tmessage); + if (receivedMessage.dst && + this.realm.getClientById(receivedMessage.dst)) { + this.emit("message", undefined, JSON.parse(tmessage)); + } + } + }); } _onSocketConnection(socket, req) { const { query = {} } = url_1.default.parse(req.url, true); @@ -71,9 +87,12 @@ class WebSocketServer extends events_1.default { // Handle messages from peers. socket.on("message", (data) => { try { - console.log("Received Message from Peer"); const message = JSON.parse(data); message.src = client.getId(); + if (message.type !== "HEARTBEAT") { + MessagePublisher.publish("transmission", JSON.stringify(message)); + return; + } this.emit("message", client, message); } catch (e) { diff --git a/src/instance.ts b/src/instance.ts index 4030e3b98..c255a2529 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -69,8 +69,8 @@ export const createInstance = ({ }); wss.on("message", (client: IClient, message: IMessage) => { - console.log("In handle Peer Message", message.type); - app.emit("message", client, message); + console.log("In handle Peer Message", message); + // app.emit("message", client, message); messageHandler.handle(client, message); }); diff --git a/src/models/realm.ts b/src/models/realm.ts index 4948f7547..1aa99f958 100644 --- a/src/models/realm.ts +++ b/src/models/realm.ts @@ -1,8 +1,7 @@ import uuidv4 from "uuid/v4"; -import { IClient, Client } from "./client"; +import { IClient } from "./client"; import { IMessage } from "./message"; import { IMessageQueue, MessageQueue } from "./messageQueue"; -import { clog } from "../utils"; export interface IRealm { getClientsIds(): string[]; diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts index 43e019f14..e7918ce7a 100644 --- a/src/services/webSocketServer/index.ts +++ b/src/services/webSocketServer/index.ts @@ -8,7 +8,9 @@ import { Client, IClient } from "../../models/client"; import { IRealm } from "../../models/realm"; import { MyWebSocket } from "./webSocket"; -// const redisPub = new Redis(); +const Redis = require("ioredis"); +const MessagePublisher = new Redis(); +const MessageSubscriber = new Redis(); export interface IWebSocketServer extends EventEmitter { readonly path: string; @@ -55,6 +57,21 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { this._onSocketConnection(socket, req) ); this.socketServer.on("error", (error: Error) => this._onSocketError(error)); + MessageSubscriber.subscribe("transmission", (err: Error) => { + if (!err) console.log("Subscribed to Transmission messages"); + }); + + MessageSubscriber.on("message", (channel: string, tmessage: string) => { + if (channel === "transmission") { + const receivedMessage = JSON.parse(tmessage); + if ( + receivedMessage.dst && + this.realm.getClientById(receivedMessage.dst) + ) { + this.emit("message", undefined, JSON.parse(tmessage)); + } + } + }); } private _onSocketConnection(socket: MyWebSocket, req: IncomingMessage): void { @@ -133,9 +150,12 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { // Handle messages from peers. socket.on("message", (data: WebSocketLib.Data) => { try { - console.log("Received Message from Peer"); const message = JSON.parse(data as string); message.src = client.getId(); + if (message.type !== "HEARTBEAT") { + MessagePublisher.publish("transmission", JSON.stringify(message)); + return; + } this.emit("message", client, message); } catch (e) { this.emit("error", e); From 98bd0159c973abbb7381c98b90d8d2ffc55510af Mon Sep 17 00:00:00 2001 From: sourav Date: Sun, 17 May 2020 19:56:30 +0530 Subject: [PATCH 13/18] Distributed Message handling --- dist/src/instance.js | 2 +- dist/src/services/webSocketServer/index.js | 11 ++++++++--- src/instance.ts | 2 +- src/services/webSocketServer/index.ts | 13 ++++++++++--- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/dist/src/instance.js b/dist/src/instance.js index d6cde771a..684c0fa17 100644 --- a/dist/src/instance.js +++ b/dist/src/instance.js @@ -47,7 +47,7 @@ exports.createInstance = ({ app, server, options, }) => { app.emit("connection", client); }); wss.on("message", (client, message) => { - console.log("In handle Peer Message", message); + console.log("In handle Peer Message"); // app.emit("message", client, message); messageHandler.handle(client, message); }); diff --git a/dist/src/services/webSocketServer/index.js b/dist/src/services/webSocketServer/index.js index f2640abd7..e0cc63f53 100644 --- a/dist/src/services/webSocketServer/index.js +++ b/dist/src/services/webSocketServer/index.js @@ -8,9 +8,14 @@ const url_1 = __importDefault(require("url")); const ws_1 = __importDefault(require("ws")); const enums_1 = require("../../enums"); const client_1 = require("../../models/client"); +const env = process.env.NODE_ENV ? process.env.NODE_ENV : "development"; +const redisHost = env === "production" + ? "fmqueue.7piuva.ng.0001.use1.cache.amazonaws.com" + : "127.0.0.1"; +const redisPort = 6379; const Redis = require("ioredis"); -const MessagePublisher = new Redis(); -const MessageSubscriber = new Redis(); +const MessagePublisher = new Redis(redisPort, redisHost); +const MessageSubscriber = new Redis(redisPort, redisHost); const WS_PATH = "peerjs"; class WebSocketServer extends events_1.default { constructor({ server, realm, config, }) { @@ -32,7 +37,7 @@ class WebSocketServer extends events_1.default { const receivedMessage = JSON.parse(tmessage); if (receivedMessage.dst && this.realm.getClientById(receivedMessage.dst)) { - this.emit("message", undefined, JSON.parse(tmessage)); + this.emit("message", undefined, receivedMessage); } } }); diff --git a/src/instance.ts b/src/instance.ts index c255a2529..2f417564d 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -69,7 +69,7 @@ export const createInstance = ({ }); wss.on("message", (client: IClient, message: IMessage) => { - console.log("In handle Peer Message", message); + console.log("In handle Peer Message"); // app.emit("message", client, message); messageHandler.handle(client, message); }); diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts index e7918ce7a..6d919262b 100644 --- a/src/services/webSocketServer/index.ts +++ b/src/services/webSocketServer/index.ts @@ -8,9 +8,16 @@ import { Client, IClient } from "../../models/client"; import { IRealm } from "../../models/realm"; import { MyWebSocket } from "./webSocket"; +const env = process.env.NODE_ENV ? process.env.NODE_ENV : "development"; +const redisHost = + env === "production" + ? "fmqueue.7piuva.ng.0001.use1.cache.amazonaws.com" + : "127.0.0.1"; +const redisPort = 6379; + const Redis = require("ioredis"); -const MessagePublisher = new Redis(); -const MessageSubscriber = new Redis(); +const MessagePublisher = new Redis(redisPort, redisHost); +const MessageSubscriber = new Redis(redisPort, redisHost); export interface IWebSocketServer extends EventEmitter { readonly path: string; @@ -68,7 +75,7 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { receivedMessage.dst && this.realm.getClientById(receivedMessage.dst) ) { - this.emit("message", undefined, JSON.parse(tmessage)); + this.emit("message", undefined, receivedMessage); } } }); From dbb208074b2b5c903bef9c9944a47cd9d2e4d724 Mon Sep 17 00:00:00 2001 From: sourav Date: Sun, 17 May 2020 22:49:41 +0530 Subject: [PATCH 14/18] Debugging Statements --- dist/src/services/webSocketServer/index.js | 1 + src/services/webSocketServer/index.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/dist/src/services/webSocketServer/index.js b/dist/src/services/webSocketServer/index.js index e0cc63f53..820e7e10a 100644 --- a/dist/src/services/webSocketServer/index.js +++ b/dist/src/services/webSocketServer/index.js @@ -75,6 +75,7 @@ class WebSocketServer extends events_1.default { if (clientsCount >= this.config.concurrent_limit) { return this._sendErrorAndClose(socket, enums_1.Errors.CONNECTION_LIMIT_EXCEED); } + console.log("NEW CLIENT:::", id); const newClient = new client_1.Client({ id, token }); this.realm.setClient(newClient, id); socket.send(JSON.stringify({ type: enums_1.MessageType.OPEN })); diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts index 6d919262b..37f3c5f3b 100644 --- a/src/services/webSocketServer/index.ts +++ b/src/services/webSocketServer/index.ts @@ -136,6 +136,8 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { return this._sendErrorAndClose(socket, Errors.CONNECTION_LIMIT_EXCEED); } + console.log("NEW CLIENT:::", id); + const newClient: IClient = new Client({ id, token }); this.realm.setClient(newClient, id); socket.send(JSON.stringify({ type: MessageType.OPEN })); From 3f9fe3ed5e7d1d23e00e94333076c2169fa62dd8 Mon Sep 17 00:00:00 2001 From: sourav Date: Mon, 18 May 2020 17:08:57 +0530 Subject: [PATCH 15/18] Added configuration for Redis --- bin/peerjs | 63 +++++++++++++-------- dist/src/config/index.js | 7 ++- dist/src/index.js | 3 +- dist/src/instance.js | 2 +- dist/src/services/webSocketServer/index.js | 24 ++++---- dist/src/start.js | 7 ++- package.json | 7 +-- src/config/index.ts | 10 +++- src/index.ts | 26 +++++---- src/instance.ts | 3 +- src/services/webSocketServer/index.ts | 65 ++++++++++++++-------- src/start.ts | 7 ++- 12 files changed, 139 insertions(+), 85 deletions(-) diff --git a/bin/peerjs b/bin/peerjs index f6a04e67a..5a5674961 100755 --- a/bin/peerjs +++ b/bin/peerjs @@ -5,7 +5,7 @@ const path = require("path"); const pkg = require("../package.json"); const fs = require("fs"); const optimistUsageLength = 98; -const yargs = require("yargs") +const yargs = require("yargs"); const version = pkg.version; const { PeerServer } = require("../dist/src"); const opts = yargs @@ -16,57 +16,71 @@ const opts = yargs demandOption: false, alias: "t", describe: "timeout (milliseconds)", - default: 5000 + default: 5000, }, concurrent_limit: { demandOption: false, alias: "c", describe: "concurrent limit", - default: 5000 + default: 5000, }, alive_timeout: { demandOption: false, describe: "broken connection check timeout (milliseconds)", - default: 60000 + default: 60000, }, key: { demandOption: false, alias: "k", describe: "connection key", - default: "peerjs" + default: "peerjs", }, sslkey: { demandOption: false, - describe: "path to SSL key" + describe: "path to SSL key", }, sslcert: { demandOption: false, - describe: "path to SSL certificate" + describe: "path to SSL certificate", }, port: { demandOption: true, alias: "p", - describe: "port" + describe: "port", }, path: { demandOption: false, describe: "custom path", - default: "/" + default: "/", }, allow_discovery: { demandOption: false, - describe: "allow discovery of peers" + describe: "allow discovery of peers", }, proxied: { demandOption: false, describe: "Set true if PeerServer stays behind a reverse proxy", - default: false - } + default: false, + }, + redis: { + demandOption: false, + describe: "Should it use redis?", + default: false, + }, + redisHost: { + demandOption: false, + describe: "Redis Host to use", + default: "", + }, + redisPort: { + demandOption: false, + describe: "Redis Port to use", + default: 0, + }, }) - .boolean("allow_discovery") - .argv; + .boolean("allow_discovery").argv; -process.on("uncaughtException", function (e) { +process.on("uncaughtException", function(e) { console.error("Error: " + e); }); @@ -74,33 +88,38 @@ if (opts.sslkey || opts.sslcert) { if (opts.sslkey && opts.sslcert) { opts.ssl = { key: fs.readFileSync(path.resolve(opts.sslkey)), - cert: fs.readFileSync(path.resolve(opts.sslcert)) + cert: fs.readFileSync(path.resolve(opts.sslcert)), }; delete opts.sslkey; delete opts.sslcert; } else { - console.error("Warning: PeerServer will not run because either " + - "the key or the certificate has not been provided."); + console.error( + "Warning: PeerServer will not run because either " + + "the key or the certificate has not been provided." + ); process.exit(1); } } const userPath = opts.path; -const server = PeerServer(opts, server => { +const server = PeerServer(opts, (server) => { const host = server.address().address; const port = server.address().port; console.log( "Started PeerServer on %s, port: %s, path: %s (v. %s)", - host, port, userPath || "/", version + host, + port, + userPath || "/", + version ); }); -server.on("connection", client => { +server.on("connection", (client) => { console.log(`Client connected: ${client.getId()}`); }); -server.on("disconnect", client => { +server.on("disconnect", (client) => { console.log(`Client disconnected: ${client.getId()}`); }); diff --git a/dist/src/config/index.js b/dist/src/config/index.js index 6a1771720..22e07132e 100644 --- a/dist/src/config/index.js +++ b/dist/src/config/index.js @@ -12,7 +12,10 @@ const defaultConfig = { cleanup_out_msgs: 1000, ssl: { key: "", - cert: "" - } + cert: "", + }, + redis: false, + redisHost: "", + redisPort: 0, }; exports.default = defaultConfig; diff --git a/dist/src/index.js b/dist/src/index.js index 7be958b6a..ae8e160c0 100644 --- a/dist/src/index.js +++ b/dist/src/index.js @@ -16,8 +16,7 @@ function ExpressPeerServer(server, options) { } app.on("mount", () => { if (!server) { - throw new Error("Server is not passed to constructor - " + - "can't start PeerServer"); + throw new Error("Server is not passed to constructor - " + "can't start PeerServer"); } instance_1.createInstance({ app, server, options: newOptions }); }); diff --git a/dist/src/instance.js b/dist/src/instance.js index 684c0fa17..82839362b 100644 --- a/dist/src/instance.js +++ b/dist/src/instance.js @@ -48,7 +48,7 @@ exports.createInstance = ({ app, server, options, }) => { }); wss.on("message", (client, message) => { console.log("In handle Peer Message"); - // app.emit("message", client, message); + app.emit("message", client, message); messageHandler.handle(client, message); }); wss.on("close", (client) => { diff --git a/dist/src/services/webSocketServer/index.js b/dist/src/services/webSocketServer/index.js index 820e7e10a..4672e2a7f 100644 --- a/dist/src/services/webSocketServer/index.js +++ b/dist/src/services/webSocketServer/index.js @@ -8,14 +8,8 @@ const url_1 = __importDefault(require("url")); const ws_1 = __importDefault(require("ws")); const enums_1 = require("../../enums"); const client_1 = require("../../models/client"); -const env = process.env.NODE_ENV ? process.env.NODE_ENV : "development"; -const redisHost = env === "production" - ? "fmqueue.7piuva.ng.0001.use1.cache.amazonaws.com" - : "127.0.0.1"; -const redisPort = 6379; +const utils_1 = require("../../utils"); const Redis = require("ioredis"); -const MessagePublisher = new Redis(redisPort, redisHost); -const MessageSubscriber = new Redis(redisPort, redisHost); const WS_PATH = "peerjs"; class WebSocketServer extends events_1.default { constructor({ server, realm, config, }) { @@ -28,11 +22,19 @@ class WebSocketServer extends events_1.default { this.socketServer = new ws_1.default.Server({ path: this.path, server }); this.socketServer.on("connection", (socket, req) => this._onSocketConnection(socket, req)); this.socketServer.on("error", (error) => this._onSocketError(error)); - MessageSubscriber.subscribe("transmission", (err) => { + if (config.redis) { + this.messagePublisher = new Redis(this.config.redisPort, this.config.redisHost); + this.messageSubscriber = new Redis(this.config.redisPort, this.config.redisHost); + this._configureRedis(); + } + } + _configureRedis() { + this.messageSubscriber.subscribe("transmission", (err) => { if (!err) console.log("Subscribed to Transmission messages"); }); - MessageSubscriber.on("message", (channel, tmessage) => { + this.messageSubscriber.on("message", (channel, tmessage) => { + utils_1.clog(`Received Message on Channel:: ${channel}`); if (channel === "transmission") { const receivedMessage = JSON.parse(tmessage); if (receivedMessage.dst && @@ -95,8 +97,8 @@ class WebSocketServer extends events_1.default { try { const message = JSON.parse(data); message.src = client.getId(); - if (message.type !== "HEARTBEAT") { - MessagePublisher.publish("transmission", JSON.stringify(message)); + if (message.type !== "HEARTBEAT" && this.config.redis) { + this.messagePublisher.publish("transmission", JSON.stringify(message)); return; } this.emit("message", client, message); diff --git a/dist/src/start.js b/dist/src/start.js index 7b7e41dbd..cdf92e55b 100644 --- a/dist/src/start.js +++ b/dist/src/start.js @@ -1,7 +1,7 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("./index"); -const config = { +const options = { port: 9000, expire_timeout: 5000, alive_timeout: 60000, @@ -11,6 +11,9 @@ const config = { allow_discovery: false, proxied: false, cleanup_out_msgs: 1000, + redis: true, + redisHost: "127.0.0.1", + redisPort: 6379, }; -index_1.PeerServer(config); +index_1.PeerServer(options); console.log("Server started at port 9000"); diff --git a/package.json b/package.json index a7eadd6e3..dfad5c2c7 100644 --- a/package.json +++ b/package.json @@ -34,12 +34,9 @@ "tsc": "tsc", "prebuild": "npm run lint", "test": "npm run lint && mocha -r ts-node/register \"test/**/*\"", - "start": "bin/peerjs --port ${PORT:=9000} --path /myapp", + "start": "bin/peerjs --port ${PORT:=9000} --path /myapp --redis true --redisHost '127.0.0.1' --redisPort 6379", "dev:start": "npm-run-all build start", - "dev": "NODE_ENV=development nodemon --watch src -e ts --exec npm run dev:start", - "startp": "bin/peerjs --port ${PORT:=9000} --path /myapp --sslkey ../certs/frontm_ai.key --sslcert ../certs/frontm_ai.crt", - "prod:start": "npm-run-all build startp", - "prod": "NODE_ENV=production nodemon --watch src -e ts --exec npm run prod:start" + "dev": "NODE_ENV=development nodemon --watch src -e ts --exec npm run dev:start" }, "release": { "branch": "master" diff --git a/src/config/index.ts b/src/config/index.ts index 6ac406ae0..6a269aa12 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -13,6 +13,9 @@ export interface IConfig { cert: string; }; readonly generateClientId?: () => string; + readonly redis?: boolean; + readonly redisHost?: string; + readonly redisPort?: number; } const defaultConfig: IConfig = { @@ -27,8 +30,11 @@ const defaultConfig: IConfig = { cleanup_out_msgs: 1000, ssl: { key: "", - cert: "" - } + cert: "", + }, + redis: false, + redisHost: "", + redisPort: 0, }; export default defaultConfig; diff --git a/src/index.ts b/src/index.ts index a71882888..d8763829e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import defaultConfig, { IConfig } from "./config"; import { createInstance } from "./instance"; type Optional = { - [P in keyof T]?: (T[P] | undefined); + [P in keyof T]?: T[P] | undefined; }; function ExpressPeerServer(server: Server, options?: IConfig) { @@ -15,17 +15,21 @@ function ExpressPeerServer(server: Server, options?: IConfig) { const newOptions: IConfig = { ...defaultConfig, - ...options + ...options, }; if (newOptions.proxied) { - app.set("trust proxy", newOptions.proxied === "false" ? false : !!newOptions.proxied); + app.set( + "trust proxy", + newOptions.proxied === "false" ? false : !!newOptions.proxied + ); } app.on("mount", () => { if (!server) { - throw new Error("Server is not passed to constructor - " + - "can't start PeerServer"); + throw new Error( + "Server is not passed to constructor - " + "can't start PeerServer" + ); } createInstance({ app, server, options: newOptions }); @@ -34,12 +38,15 @@ function ExpressPeerServer(server: Server, options?: IConfig) { return app; } -function PeerServer(options: Optional = {}, callback?: (server: Server) => void) { +function PeerServer( + options: Optional = {}, + callback?: (server: Server) => void +) { const app = express(); const newOptions: IConfig = { ...defaultConfig, - ...options + ...options, }; const port = newOptions.port; @@ -62,7 +69,4 @@ function PeerServer(options: Optional = {}, callback?: (server: Server) return peerjs; } -export { - ExpressPeerServer, - PeerServer -}; +export { ExpressPeerServer, PeerServer }; diff --git a/src/instance.ts b/src/instance.ts index 2f417564d..0f758d841 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -70,7 +70,7 @@ export const createInstance = ({ wss.on("message", (client: IClient, message: IMessage) => { console.log("In handle Peer Message"); - // app.emit("message", client, message); + app.emit("message", client, message); messageHandler.handle(client, message); }); @@ -85,4 +85,3 @@ export const createInstance = ({ messagesExpire.startMessagesExpiration(); checkBrokenConnections.start(); }; - diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts index 37f3c5f3b..c80ca3e15 100644 --- a/src/services/webSocketServer/index.ts +++ b/src/services/webSocketServer/index.ts @@ -7,17 +7,9 @@ import { Errors, MessageType } from "../../enums"; import { Client, IClient } from "../../models/client"; import { IRealm } from "../../models/realm"; import { MyWebSocket } from "./webSocket"; - -const env = process.env.NODE_ENV ? process.env.NODE_ENV : "development"; -const redisHost = - env === "production" - ? "fmqueue.7piuva.ng.0001.use1.cache.amazonaws.com" - : "127.0.0.1"; -const redisPort = 6379; +import { clog } from "../../utils"; const Redis = require("ioredis"); -const MessagePublisher = new Redis(redisPort, redisHost); -const MessageSubscriber = new Redis(redisPort, redisHost); export interface IWebSocketServer extends EventEmitter { readonly path: string; @@ -29,7 +21,10 @@ interface IAuthParams { key?: string; } -type CustomConfig = Pick; +type CustomConfig = Pick< + IConfig, + "path" | "key" | "concurrent_limit" | "redis" | "redisHost" | "redisPort" +>; const WS_PATH = "peerjs"; @@ -37,6 +32,8 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { public readonly path: string; private readonly realm: IRealm; private readonly config: CustomConfig; + private readonly messageSubscriber: any; + private readonly messagePublisher: any; public readonly socketServer: WebSocketLib.Server; constructor({ @@ -64,21 +61,40 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { this._onSocketConnection(socket, req) ); this.socketServer.on("error", (error: Error) => this._onSocketError(error)); - MessageSubscriber.subscribe("transmission", (err: Error) => { + + if (config.redis) { + this.messagePublisher = new Redis( + this.config.redisPort, + this.config.redisHost + ); + this.messageSubscriber = new Redis( + this.config.redisPort, + this.config.redisHost + ); + this._configureRedis(); + } + } + + private _configureRedis() { + this.messageSubscriber.subscribe("transmission", (err: Error) => { if (!err) console.log("Subscribed to Transmission messages"); }); - - MessageSubscriber.on("message", (channel: string, tmessage: string) => { - if (channel === "transmission") { - const receivedMessage = JSON.parse(tmessage); - if ( - receivedMessage.dst && - this.realm.getClientById(receivedMessage.dst) - ) { - this.emit("message", undefined, receivedMessage); + this.messageSubscriber.on( + "message", + (channel: string, tmessage: string) => { + clog(`Received Message on Channel:: ${channel}`); + + if (channel === "transmission") { + const receivedMessage = JSON.parse(tmessage); + if ( + receivedMessage.dst && + this.realm.getClientById(receivedMessage.dst) + ) { + this.emit("message", undefined, receivedMessage); + } } } - }); + ); } private _onSocketConnection(socket: MyWebSocket, req: IncomingMessage): void { @@ -161,8 +177,11 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { try { const message = JSON.parse(data as string); message.src = client.getId(); - if (message.type !== "HEARTBEAT") { - MessagePublisher.publish("transmission", JSON.stringify(message)); + if (message.type !== "HEARTBEAT" && this.config.redis) { + this.messagePublisher.publish( + "transmission", + JSON.stringify(message) + ); return; } this.emit("message", client, message); diff --git a/src/start.ts b/src/start.ts index a0099d0c5..7e935ec82 100644 --- a/src/start.ts +++ b/src/start.ts @@ -1,7 +1,7 @@ import { PeerServer } from "./index"; import { IConfig } from "./config"; -const config: IConfig = { +const options: IConfig = { port: 9000, expire_timeout: 5000, alive_timeout: 60000, @@ -11,8 +11,11 @@ const config: IConfig = { allow_discovery: false, proxied: false, cleanup_out_msgs: 1000, + redis: true, + redisHost: "127.0.0.1", + redisPort: 6379, }; -PeerServer(config); +PeerServer(options); console.log("Server started at port 9000"); From 69d7749e029f676085e931473cbae273df316d62 Mon Sep 17 00:00:00 2001 From: sourav Date: Mon, 18 May 2020 17:27:47 +0530 Subject: [PATCH 16/18] Removed Additional Files --- src/start.ts | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 src/start.ts diff --git a/src/start.ts b/src/start.ts deleted file mode 100644 index 7e935ec82..000000000 --- a/src/start.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { PeerServer } from "./index"; -import { IConfig } from "./config"; - -const options: IConfig = { - port: 9000, - expire_timeout: 5000, - alive_timeout: 60000, - key: "peerjs", - path: "/myapp", - concurrent_limit: 5000, - allow_discovery: false, - proxied: false, - cleanup_out_msgs: 1000, - redis: true, - redisHost: "127.0.0.1", - redisPort: 6379, -}; - -PeerServer(options); - -console.log("Server started at port 9000"); From 850c038ab97747fa87fa896d8001776582861df7 Mon Sep 17 00:00:00 2001 From: sourav Date: Sat, 23 May 2020 19:18:29 +0530 Subject: [PATCH 17/18] Clened up console logs --- dist/src/instance.js | 1 - .../handlers/transmission/index.js | 7 ------- dist/src/models/client.js | 1 - dist/src/models/realm.js | 3 --- dist/src/services/webSocketServer/index.js | 2 -- package.json | 4 +--- src/instance.ts | 1 - .../handlers/transmission/index.ts | 8 -------- src/models/client.ts | 1 - src/models/realm.ts | 3 --- src/services/webSocketServer/index.ts | 3 --- src/utils.ts | 17 ----------------- 12 files changed, 1 insertion(+), 50 deletions(-) delete mode 100644 src/utils.ts diff --git a/dist/src/instance.js b/dist/src/instance.js index 82839362b..ca5c5dcd9 100644 --- a/dist/src/instance.js +++ b/dist/src/instance.js @@ -47,7 +47,6 @@ exports.createInstance = ({ app, server, options, }) => { app.emit("connection", client); }); wss.on("message", (client, message) => { - console.log("In handle Peer Message"); app.emit("message", client, message); messageHandler.handle(client, message); }); diff --git a/dist/src/messageHandler/handlers/transmission/index.js b/dist/src/messageHandler/handlers/transmission/index.js index 410cace3d..d57c69543 100644 --- a/dist/src/messageHandler/handlers/transmission/index.js +++ b/dist/src/messageHandler/handlers/transmission/index.js @@ -1,27 +1,22 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const enums_1 = require("../../../enums"); -const utils_1 = require("../../../utils"); exports.TransmissionHandler = ({ realm, }) => { const handle = (client, message) => { const type = message.type; const srcId = message.src; const dstId = message.dst; - utils_1.clog("Transmission:: Got message"); const destinationClient = realm.getClientById(dstId); // User is connected! if (destinationClient) { - utils_1.clog("Transmission:: Got Destination Client"); const socket = destinationClient.getSocket(); try { if (socket) { - utils_1.clog("Transmission:: Got Socket"); const data = JSON.stringify(message); socket.send(data); } else { // Neither socket no res available. Peer dead? - utils_1.clog("Transmission:: Peer Dead"); throw new Error("Peer dead"); } } @@ -30,11 +25,9 @@ exports.TransmissionHandler = ({ realm, }) => { // the associated WebSocket has not closed. // Tell other side to stop trying. if (socket) { - utils_1.clog("Transmission:: Closing Socket Connection"); socket.close(); } else { - utils_1.clog("Transmission:: Removing Client"); realm.removeClientById(destinationClient.getId()); } handle(client, { diff --git a/dist/src/models/client.js b/dist/src/models/client.js index 928473eab..79473e452 100644 --- a/dist/src/models/client.js +++ b/dist/src/models/client.js @@ -4,7 +4,6 @@ class Client { constructor({ id, token }) { this.socket = null; this.lastPing = new Date().getTime(); - console.log("Create a client"); this.id = id; this.token = token; } diff --git a/dist/src/models/realm.js b/dist/src/models/realm.js index 258fc6f62..743b184f4 100644 --- a/dist/src/models/realm.js +++ b/dist/src/models/realm.js @@ -30,11 +30,9 @@ class Realm { return true; } getMessageQueueById(id) { - console.log("Getting MessageQueue"); return this.messageQueues.get(id); } addMessageToQueue(id, message) { - console.log("Add MessageQueue"); if (!this.getMessageQueueById(id)) { this.messageQueues.set(id, new messageQueue_1.MessageQueue()); } @@ -49,7 +47,6 @@ class Realm { while (this.getClientById(clientId)) { clientId = generateId(); } - console.log("Generate ID", clientId); return clientId; } } diff --git a/dist/src/services/webSocketServer/index.js b/dist/src/services/webSocketServer/index.js index 4672e2a7f..665a0ce4e 100644 --- a/dist/src/services/webSocketServer/index.js +++ b/dist/src/services/webSocketServer/index.js @@ -8,7 +8,6 @@ const url_1 = __importDefault(require("url")); const ws_1 = __importDefault(require("ws")); const enums_1 = require("../../enums"); const client_1 = require("../../models/client"); -const utils_1 = require("../../utils"); const Redis = require("ioredis"); const WS_PATH = "peerjs"; class WebSocketServer extends events_1.default { @@ -34,7 +33,6 @@ class WebSocketServer extends events_1.default { console.log("Subscribed to Transmission messages"); }); this.messageSubscriber.on("message", (channel, tmessage) => { - utils_1.clog(`Received Message on Channel:: ${channel}`); if (channel === "transmission") { const receivedMessage = JSON.parse(tmessage); if (receivedMessage.dst && diff --git a/package.json b/package.json index dfad5c2c7..ec1d2d766 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "tsc": "tsc", "prebuild": "npm run lint", "test": "npm run lint && mocha -r ts-node/register \"test/**/*\"", - "start": "bin/peerjs --port ${PORT:=9000} --path /myapp --redis true --redisHost '127.0.0.1' --redisPort 6379", + "start": "bin/peerjs --port ${PORT:=9000} --path /myapp", "dev:start": "npm-run-all build start", "dev": "NODE_ENV=development nodemon --watch src -e ts --exec npm run dev:start" }, @@ -46,7 +46,6 @@ "@types/express": "^4.17.3", "@types/ws": "^7.2.3", "body-parser": "^1.19.0", - "chalk": "^4.0.0", "cors": "^2.8.5", "express": "^4.17.1", "ioredis": "^4.16.3", @@ -69,7 +68,6 @@ "npm-run-all": "^4.1.5", "rimraf": "^3.0.2", "sinon": "^7.5.0", - "stack-trace": "0.0.10", "ts-node": "^8.7.0", "typescript": "^3.8.3" }, diff --git a/src/instance.ts b/src/instance.ts index 0f758d841..04f4933e5 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -69,7 +69,6 @@ export const createInstance = ({ }); wss.on("message", (client: IClient, message: IMessage) => { - console.log("In handle Peer Message"); app.emit("message", client, message); messageHandler.handle(client, message); }); diff --git a/src/messageHandler/handlers/transmission/index.ts b/src/messageHandler/handlers/transmission/index.ts index 624386c4a..8de0347bc 100644 --- a/src/messageHandler/handlers/transmission/index.ts +++ b/src/messageHandler/handlers/transmission/index.ts @@ -2,7 +2,6 @@ import { MessageType } from "../../../enums"; import { IClient } from "../../../models/client"; import { IMessage } from "../../../models/message"; import { IRealm } from "../../../models/realm"; -import { clog } from "../../../utils"; export const TransmissionHandler = ({ realm, @@ -14,23 +13,18 @@ export const TransmissionHandler = ({ const srcId = message.src; const dstId = message.dst; - clog("Transmission:: Got message"); - const destinationClient = realm.getClientById(dstId); // User is connected! if (destinationClient) { - clog("Transmission:: Got Destination Client"); const socket = destinationClient.getSocket(); try { if (socket) { - clog("Transmission:: Got Socket"); const data = JSON.stringify(message); socket.send(data); } else { // Neither socket no res available. Peer dead? - clog("Transmission:: Peer Dead"); throw new Error("Peer dead"); } } catch (e) { @@ -38,10 +32,8 @@ export const TransmissionHandler = ({ // the associated WebSocket has not closed. // Tell other side to stop trying. if (socket) { - clog("Transmission:: Closing Socket Connection"); socket.close(); } else { - clog("Transmission:: Removing Client"); realm.removeClientById(destinationClient.getId()); } diff --git a/src/models/client.ts b/src/models/client.ts index a3157a6d9..969ccb694 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -23,7 +23,6 @@ export class Client implements IClient { private lastPing: number = new Date().getTime(); constructor({ id, token }: { id: string; token: string; }) { - console.log("Create a client"); this.id = id; this.token = token; } diff --git a/src/models/realm.ts b/src/models/realm.ts index 1aa99f958..bf25e39fb 100644 --- a/src/models/realm.ts +++ b/src/models/realm.ts @@ -51,12 +51,10 @@ export class Realm implements IRealm { } public getMessageQueueById(id: string): IMessageQueue | undefined { - console.log("Getting MessageQueue"); return this.messageQueues.get(id); } public addMessageToQueue(id: string, message: IMessage): void { - console.log("Add MessageQueue"); if (!this.getMessageQueueById(id)) { this.messageQueues.set(id, new MessageQueue()); } @@ -76,7 +74,6 @@ export class Realm implements IRealm { while (this.getClientById(clientId)) { clientId = generateId(); } - console.log("Generate ID", clientId); return clientId; } diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts index c80ca3e15..ec224b16a 100644 --- a/src/services/webSocketServer/index.ts +++ b/src/services/webSocketServer/index.ts @@ -7,7 +7,6 @@ import { Errors, MessageType } from "../../enums"; import { Client, IClient } from "../../models/client"; import { IRealm } from "../../models/realm"; import { MyWebSocket } from "./webSocket"; -import { clog } from "../../utils"; const Redis = require("ioredis"); @@ -82,8 +81,6 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { this.messageSubscriber.on( "message", (channel: string, tmessage: string) => { - clog(`Received Message on Channel:: ${channel}`); - if (channel === "transmission") { const receivedMessage = JSON.parse(tmessage); if ( diff --git a/src/utils.ts b/src/utils.ts deleted file mode 100644 index 14a5bcdf8..000000000 --- a/src/utils.ts +++ /dev/null @@ -1,17 +0,0 @@ -const chalk = require("chalk"); -const stackTrace = require("stack-trace"); - -export const clog = (message: string) => { - console.log(chalk.blue(message)); -}; - -export const trace = () => { - stackTrace - .get() - .filter((site: any) => !site.getFileName().includes("node_modules")) - .map((site: any) => { - console.log( - chalk.red(`${site.getFileName()} --- ${site.getLineNumber()}`) - ); - }); -}; From 21a98d165075ed464791db20b60fd80f7ca2a9c7 Mon Sep 17 00:00:00 2001 From: sourav Date: Sun, 31 May 2020 22:35:48 +0530 Subject: [PATCH 18/18] Added Support for reedis --- .eslintignore | 0 .eslintrc.json | 0 .github/ISSUE_TEMPLATE/config.yml | 0 .github/ISSUE_TEMPLATE/peer-template.md | 0 .gitignore | 0 .gitpod.yml | 0 .travis.yml | 0 Dockerfile | 0 LICENSE | 0 README.md | 0 app.json | 0 changelog.md | 0 dist/app.json | 0 dist/src/api/index.js | 0 dist/src/api/middleware/auth/index.js | 0 dist/src/api/middleware/middleware.js | 0 dist/src/api/v1/calls/index.js | 0 dist/src/api/v1/public/index.js | 0 dist/src/config/index.js | 0 dist/src/enums.js | 0 dist/src/index.js | 0 dist/src/instance.js | 0 dist/src/messageHandler/handler.js | 0 dist/src/messageHandler/handlers/heartbeat/index.js | 0 dist/src/messageHandler/handlers/index.js | 0 dist/src/messageHandler/handlers/transmission/index.js | 0 dist/src/messageHandler/handlersRegistry.js | 0 dist/src/messageHandler/index.js | 0 dist/src/models/client.js | 0 dist/src/models/message.js | 0 dist/src/models/messageQueue.js | 0 dist/src/models/realm.js | 0 dist/src/services/checkBrokenConnections/index.js | 0 dist/src/services/messagesExpire/index.js | 0 dist/src/services/webSocketServer/index.js | 0 dist/src/services/webSocketServer/webSocket.js | 0 dist/src/start.js | 0 dist/src/utils.js | 0 index.d.ts | 0 package-lock.json | 0 package.json | 0 src/api/README.md | 0 src/api/index.ts | 0 src/api/middleware/auth/index.ts | 0 src/api/middleware/middleware.ts | 0 src/api/v1/calls/index.ts | 0 src/api/v1/public/index.ts | 0 src/config/index.ts | 0 src/enums.ts | 0 src/index.ts | 0 src/instance.ts | 0 src/messageHandler/handler.ts | 0 src/messageHandler/handlers/heartbeat/index.ts | 0 src/messageHandler/handlers/index.ts | 0 src/messageHandler/handlers/transmission/index.ts | 0 src/messageHandler/handlersRegistry.ts | 0 src/messageHandler/index.ts | 0 src/models/client.ts | 0 src/models/message.ts | 0 src/models/messageQueue.ts | 0 src/models/realm.ts | 0 src/services/checkBrokenConnections/index.ts | 0 src/services/messagesExpire/index.ts | 0 src/services/webSocketServer/index.ts | 0 src/services/webSocketServer/webSocket.ts | 0 test/messageHandler/handlers/heartbeat/index.ts | 0 test/messageHandler/handlers/transmission/index.ts | 0 test/messageHandler/handlersRegistry.ts | 0 test/models/messageQueue.ts | 0 test/models/realm.ts | 0 test/services/checkBrokenConnections/index.ts | 0 test/services/messagesExpire/index.ts | 0 test/services/webSocketServer/index.ts | 0 test/utils.ts | 0 tsconfig.json | 0 75 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 .eslintignore mode change 100644 => 100755 .eslintrc.json mode change 100644 => 100755 .github/ISSUE_TEMPLATE/config.yml mode change 100644 => 100755 .github/ISSUE_TEMPLATE/peer-template.md mode change 100644 => 100755 .gitignore mode change 100644 => 100755 .gitpod.yml mode change 100644 => 100755 .travis.yml mode change 100644 => 100755 Dockerfile mode change 100644 => 100755 LICENSE mode change 100644 => 100755 README.md mode change 100644 => 100755 app.json mode change 100644 => 100755 changelog.md mode change 100644 => 100755 dist/app.json mode change 100644 => 100755 dist/src/api/index.js mode change 100644 => 100755 dist/src/api/middleware/auth/index.js mode change 100644 => 100755 dist/src/api/middleware/middleware.js mode change 100644 => 100755 dist/src/api/v1/calls/index.js mode change 100644 => 100755 dist/src/api/v1/public/index.js mode change 100644 => 100755 dist/src/config/index.js mode change 100644 => 100755 dist/src/enums.js mode change 100644 => 100755 dist/src/index.js mode change 100644 => 100755 dist/src/instance.js mode change 100644 => 100755 dist/src/messageHandler/handler.js mode change 100644 => 100755 dist/src/messageHandler/handlers/heartbeat/index.js mode change 100644 => 100755 dist/src/messageHandler/handlers/index.js mode change 100644 => 100755 dist/src/messageHandler/handlers/transmission/index.js mode change 100644 => 100755 dist/src/messageHandler/handlersRegistry.js mode change 100644 => 100755 dist/src/messageHandler/index.js mode change 100644 => 100755 dist/src/models/client.js mode change 100644 => 100755 dist/src/models/message.js mode change 100644 => 100755 dist/src/models/messageQueue.js mode change 100644 => 100755 dist/src/models/realm.js mode change 100644 => 100755 dist/src/services/checkBrokenConnections/index.js mode change 100644 => 100755 dist/src/services/messagesExpire/index.js mode change 100644 => 100755 dist/src/services/webSocketServer/index.js mode change 100644 => 100755 dist/src/services/webSocketServer/webSocket.js mode change 100644 => 100755 dist/src/start.js mode change 100644 => 100755 dist/src/utils.js mode change 100644 => 100755 index.d.ts mode change 100644 => 100755 package-lock.json mode change 100644 => 100755 package.json mode change 100644 => 100755 src/api/README.md mode change 100644 => 100755 src/api/index.ts mode change 100644 => 100755 src/api/middleware/auth/index.ts mode change 100644 => 100755 src/api/middleware/middleware.ts mode change 100644 => 100755 src/api/v1/calls/index.ts mode change 100644 => 100755 src/api/v1/public/index.ts mode change 100644 => 100755 src/config/index.ts mode change 100644 => 100755 src/enums.ts mode change 100644 => 100755 src/index.ts mode change 100644 => 100755 src/instance.ts mode change 100644 => 100755 src/messageHandler/handler.ts mode change 100644 => 100755 src/messageHandler/handlers/heartbeat/index.ts mode change 100644 => 100755 src/messageHandler/handlers/index.ts mode change 100644 => 100755 src/messageHandler/handlers/transmission/index.ts mode change 100644 => 100755 src/messageHandler/handlersRegistry.ts mode change 100644 => 100755 src/messageHandler/index.ts mode change 100644 => 100755 src/models/client.ts mode change 100644 => 100755 src/models/message.ts mode change 100644 => 100755 src/models/messageQueue.ts mode change 100644 => 100755 src/models/realm.ts mode change 100644 => 100755 src/services/checkBrokenConnections/index.ts mode change 100644 => 100755 src/services/messagesExpire/index.ts mode change 100644 => 100755 src/services/webSocketServer/index.ts mode change 100644 => 100755 src/services/webSocketServer/webSocket.ts mode change 100644 => 100755 test/messageHandler/handlers/heartbeat/index.ts mode change 100644 => 100755 test/messageHandler/handlers/transmission/index.ts mode change 100644 => 100755 test/messageHandler/handlersRegistry.ts mode change 100644 => 100755 test/models/messageQueue.ts mode change 100644 => 100755 test/models/realm.ts mode change 100644 => 100755 test/services/checkBrokenConnections/index.ts mode change 100644 => 100755 test/services/messagesExpire/index.ts mode change 100644 => 100755 test/services/webSocketServer/index.ts mode change 100644 => 100755 test/utils.ts mode change 100644 => 100755 tsconfig.json diff --git a/.eslintignore b/.eslintignore old mode 100644 new mode 100755 diff --git a/.eslintrc.json b/.eslintrc.json old mode 100644 new mode 100755 diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml old mode 100644 new mode 100755 diff --git a/.github/ISSUE_TEMPLATE/peer-template.md b/.github/ISSUE_TEMPLATE/peer-template.md old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.gitpod.yml b/.gitpod.yml old mode 100644 new mode 100755 diff --git a/.travis.yml b/.travis.yml old mode 100644 new mode 100755 diff --git a/Dockerfile b/Dockerfile old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/app.json b/app.json old mode 100644 new mode 100755 diff --git a/changelog.md b/changelog.md old mode 100644 new mode 100755 diff --git a/dist/app.json b/dist/app.json old mode 100644 new mode 100755 diff --git a/dist/src/api/index.js b/dist/src/api/index.js old mode 100644 new mode 100755 diff --git a/dist/src/api/middleware/auth/index.js b/dist/src/api/middleware/auth/index.js old mode 100644 new mode 100755 diff --git a/dist/src/api/middleware/middleware.js b/dist/src/api/middleware/middleware.js old mode 100644 new mode 100755 diff --git a/dist/src/api/v1/calls/index.js b/dist/src/api/v1/calls/index.js old mode 100644 new mode 100755 diff --git a/dist/src/api/v1/public/index.js b/dist/src/api/v1/public/index.js old mode 100644 new mode 100755 diff --git a/dist/src/config/index.js b/dist/src/config/index.js old mode 100644 new mode 100755 diff --git a/dist/src/enums.js b/dist/src/enums.js old mode 100644 new mode 100755 diff --git a/dist/src/index.js b/dist/src/index.js old mode 100644 new mode 100755 diff --git a/dist/src/instance.js b/dist/src/instance.js old mode 100644 new mode 100755 diff --git a/dist/src/messageHandler/handler.js b/dist/src/messageHandler/handler.js old mode 100644 new mode 100755 diff --git a/dist/src/messageHandler/handlers/heartbeat/index.js b/dist/src/messageHandler/handlers/heartbeat/index.js old mode 100644 new mode 100755 diff --git a/dist/src/messageHandler/handlers/index.js b/dist/src/messageHandler/handlers/index.js old mode 100644 new mode 100755 diff --git a/dist/src/messageHandler/handlers/transmission/index.js b/dist/src/messageHandler/handlers/transmission/index.js old mode 100644 new mode 100755 diff --git a/dist/src/messageHandler/handlersRegistry.js b/dist/src/messageHandler/handlersRegistry.js old mode 100644 new mode 100755 diff --git a/dist/src/messageHandler/index.js b/dist/src/messageHandler/index.js old mode 100644 new mode 100755 diff --git a/dist/src/models/client.js b/dist/src/models/client.js old mode 100644 new mode 100755 diff --git a/dist/src/models/message.js b/dist/src/models/message.js old mode 100644 new mode 100755 diff --git a/dist/src/models/messageQueue.js b/dist/src/models/messageQueue.js old mode 100644 new mode 100755 diff --git a/dist/src/models/realm.js b/dist/src/models/realm.js old mode 100644 new mode 100755 diff --git a/dist/src/services/checkBrokenConnections/index.js b/dist/src/services/checkBrokenConnections/index.js old mode 100644 new mode 100755 diff --git a/dist/src/services/messagesExpire/index.js b/dist/src/services/messagesExpire/index.js old mode 100644 new mode 100755 diff --git a/dist/src/services/webSocketServer/index.js b/dist/src/services/webSocketServer/index.js old mode 100644 new mode 100755 diff --git a/dist/src/services/webSocketServer/webSocket.js b/dist/src/services/webSocketServer/webSocket.js old mode 100644 new mode 100755 diff --git a/dist/src/start.js b/dist/src/start.js old mode 100644 new mode 100755 diff --git a/dist/src/utils.js b/dist/src/utils.js old mode 100644 new mode 100755 diff --git a/index.d.ts b/index.d.ts old mode 100644 new mode 100755 diff --git a/package-lock.json b/package-lock.json old mode 100644 new mode 100755 diff --git a/package.json b/package.json old mode 100644 new mode 100755 diff --git a/src/api/README.md b/src/api/README.md old mode 100644 new mode 100755 diff --git a/src/api/index.ts b/src/api/index.ts old mode 100644 new mode 100755 diff --git a/src/api/middleware/auth/index.ts b/src/api/middleware/auth/index.ts old mode 100644 new mode 100755 diff --git a/src/api/middleware/middleware.ts b/src/api/middleware/middleware.ts old mode 100644 new mode 100755 diff --git a/src/api/v1/calls/index.ts b/src/api/v1/calls/index.ts old mode 100644 new mode 100755 diff --git a/src/api/v1/public/index.ts b/src/api/v1/public/index.ts old mode 100644 new mode 100755 diff --git a/src/config/index.ts b/src/config/index.ts old mode 100644 new mode 100755 diff --git a/src/enums.ts b/src/enums.ts old mode 100644 new mode 100755 diff --git a/src/index.ts b/src/index.ts old mode 100644 new mode 100755 diff --git a/src/instance.ts b/src/instance.ts old mode 100644 new mode 100755 diff --git a/src/messageHandler/handler.ts b/src/messageHandler/handler.ts old mode 100644 new mode 100755 diff --git a/src/messageHandler/handlers/heartbeat/index.ts b/src/messageHandler/handlers/heartbeat/index.ts old mode 100644 new mode 100755 diff --git a/src/messageHandler/handlers/index.ts b/src/messageHandler/handlers/index.ts old mode 100644 new mode 100755 diff --git a/src/messageHandler/handlers/transmission/index.ts b/src/messageHandler/handlers/transmission/index.ts old mode 100644 new mode 100755 diff --git a/src/messageHandler/handlersRegistry.ts b/src/messageHandler/handlersRegistry.ts old mode 100644 new mode 100755 diff --git a/src/messageHandler/index.ts b/src/messageHandler/index.ts old mode 100644 new mode 100755 diff --git a/src/models/client.ts b/src/models/client.ts old mode 100644 new mode 100755 diff --git a/src/models/message.ts b/src/models/message.ts old mode 100644 new mode 100755 diff --git a/src/models/messageQueue.ts b/src/models/messageQueue.ts old mode 100644 new mode 100755 diff --git a/src/models/realm.ts b/src/models/realm.ts old mode 100644 new mode 100755 diff --git a/src/services/checkBrokenConnections/index.ts b/src/services/checkBrokenConnections/index.ts old mode 100644 new mode 100755 diff --git a/src/services/messagesExpire/index.ts b/src/services/messagesExpire/index.ts old mode 100644 new mode 100755 diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts old mode 100644 new mode 100755 diff --git a/src/services/webSocketServer/webSocket.ts b/src/services/webSocketServer/webSocket.ts old mode 100644 new mode 100755 diff --git a/test/messageHandler/handlers/heartbeat/index.ts b/test/messageHandler/handlers/heartbeat/index.ts old mode 100644 new mode 100755 diff --git a/test/messageHandler/handlers/transmission/index.ts b/test/messageHandler/handlers/transmission/index.ts old mode 100644 new mode 100755 diff --git a/test/messageHandler/handlersRegistry.ts b/test/messageHandler/handlersRegistry.ts old mode 100644 new mode 100755 diff --git a/test/models/messageQueue.ts b/test/models/messageQueue.ts old mode 100644 new mode 100755 diff --git a/test/models/realm.ts b/test/models/realm.ts old mode 100644 new mode 100755 diff --git a/test/services/checkBrokenConnections/index.ts b/test/services/checkBrokenConnections/index.ts old mode 100644 new mode 100755 diff --git a/test/services/messagesExpire/index.ts b/test/services/messagesExpire/index.ts old mode 100644 new mode 100755 diff --git a/test/services/webSocketServer/index.ts b/test/services/webSocketServer/index.ts old mode 100644 new mode 100755 diff --git a/test/utils.ts b/test/utils.ts old mode 100644 new mode 100755 diff --git a/tsconfig.json b/tsconfig.json old mode 100644 new mode 100755