From b516ab9b4fa09220ba9610eb83e9d495d68840d6 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Wed, 16 Jun 2021 11:36:24 +0100 Subject: [PATCH] Batch messages sent over comms to prevent flooding --- .../@node-red/editor-api/lib/editor/comms.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/node_modules/@node-red/editor-api/lib/editor/comms.js b/packages/node_modules/@node-red/editor-api/lib/editor/comms.js index 9c96bbf5b..50e77bfa4 100644 --- a/packages/node_modules/@node-red/editor-api/lib/editor/comms.js +++ b/packages/node_modules/@node-red/editor-api/lib/editor/comms.js @@ -158,25 +158,31 @@ function CommsConnection(ws, user) { } CommsConnection.prototype.send = function(topic,data) { - var self = this; if (topic && data) { this.stack.push({topic:topic,data:data}); } + this._queueSend(); +} +CommsConnection.prototype._queueSend = function() { + var self = this; if (!this._xmitTimer) { this._xmitTimer = setTimeout(function() { try { - self.ws.send(JSON.stringify(self.stack)); + self.ws.send(JSON.stringify(self.stack.splice(0,50))); self.lastSentTime = Date.now(); } catch(err) { removeActiveConnection(self); log.warn(log._("comms.error-send",{message:err.toString()})); } delete self._xmitTimer; - self.stack = []; + if (self.stack.length > 0) { + self._queueSend(); + } },50); } } + CommsConnection.prototype.subscribe = function(topic) { runtimeAPI.comms.subscribe({ user: this.user,