Skip to content

EspoCRM – Open Source CRM Application

webSocket frontend ref

Yuri Kuznetsov6958a5a35762
diff --git a/client/modules/crm/src/views/calendar/calendar-page.js b/client/modules/crm/src/views/calendar/calendar-page.js
index f5bc89f7f8..c8bb84c3db 100644
--- a/client/modules/crm/src/views/calendar/calendar-page.js
+++ b/client/modules/crm/src/views/calendar/calendar-page.js
@@ -31,6 +31,7 @@ import CalendarEditViewModal from 'crm:views/calendar/modals/edit-view';
import {inject} from 'di';
import {ShortcutManager} from 'helpers/site/shortcut-manager';
import DebounceHelper from 'helpers/util/debounce';
+import WebSocketManager from 'web-socket-manager';
class CalendarPage extends View {
@@ -83,6 +84,13 @@ class CalendarPage extends View {
*/
webSocketBlockInterval = 1000
+ /**
+ * @private
+ * @type {WebSocketManager}
+ */
+ @inject(WebSocketManager)
+ webSocketManager
+
/**
* A shortcut-key => action map.
*
@@ -238,7 +246,7 @@ class CalendarPage extends View {
handler: () => this.handleWebSocketUpdate(),
});
- if (!this.getHelper().webSocketManager) {
+ if (!this.webSocketManager.isEnabled()) {
const testHandler = () => this.webSocketDebounceHelper.process();
this.on('remove', () => window.removeEventListener('calendar-update', testHandler));
@@ -249,9 +257,9 @@ class CalendarPage extends View {
return;
}
- this.getHelper().webSocketManager.subscribe('calendarUpdate', () => this.webSocketDebounceHelper.process());
+ this.webSocketManager.subscribe('calendarUpdate', () => this.webSocketDebounceHelper.process());
- this.on('remove', () => this.getHelper().webSocketManager.unsubscribe('calendarUpdate'));
+ this.on('remove', () => this.webSocketManager.unsubscribe('calendarUpdate'));
}
/**
diff --git a/client/src/app.js b/client/src/app.js
index e4a2b3de96..19e8755d58 100644
--- a/client/src/app.js
+++ b/client/src/app.js
@@ -315,9 +315,9 @@ class App {
/**
* @private
- * @type {module:web-socket-manager|null}
+ * @type {module:web-socket-manager}
*/
- webSocketManager = null
+ webSocketManager
/**
* @private
@@ -436,10 +436,7 @@ class App {
this.themeManager = new ThemeManager(this.settings, this.preferences, this.metadata, this.themeName);
this.modelFactory = new ModelFactory(this.metadata);
this.collectionFactory = new CollectionFactory(this.modelFactory, this.settings, this.metadata);
-
- if (this.settings.get('useWebSocket')) {
- this.webSocketManager = new WebSocketManager(this.settings);
- }
+ this.webSocketManager = new WebSocketManager(this.settings);
container.set(AclManager, this.acl);
container.set(User, this.user);
@@ -494,10 +491,13 @@ class App {
if (this.anotherUser) {
this.viewHelper.webSocketManager = null;
- this.webSocketManager = null;
}
- if (this.webSocketManager) {
+ if (this.settings.get('useWebSocket') && !this.anotherUser) {
+ this.webSocketManager.setEnabled();
+ }
+
+ if (this.webSocketManager.isEnabled()) {
this.webSocketManager.connect(this.auth, this.user.id);
}
@@ -767,13 +767,14 @@ class App {
helper.fieldManager = this.fieldManager;
helper.cache = this.cache;
helper.themeManager = this.themeManager;
- helper.webSocketManager = this.webSocketManager;
helper.numberUtil = this.numberUtil;
helper.pageTitle = new PageTitle(this.settings);
helper.basePath = this.basePath;
helper.appParams = this.appParams;
helper.broadcastChannel = this.broadcastChannel;
+ helper.webSocketManager = this.settings.get('useWebSocket') ? this.webSocketManager : null;
+
container.set(ViewHelper, this.viewHelper);
container.set(LayoutManager, helper.layoutManager);
container.set(PageTitle, helper.pageTitle);
@@ -940,7 +941,7 @@ class App {
}
}
- if (this.webSocketManager) {
+ if (this.webSocketManager.isEnabled()) {
this.webSocketManager.close();
}
diff --git a/client/src/view-helper.js b/client/src/view-helper.js
index 5263942f97..170a19ca13 100644
--- a/client/src/view-helper.js
+++ b/client/src/view-helper.js
@@ -296,7 +296,7 @@ class ViewHelper {
/**
* A web-socket manager. Null if not enabled.
*
- * @type {?module:web-socket-manager}
+ * @type {module:web-socket-manager|null}
*/
webSocketManager = null
diff --git a/client/src/views/notification/badge.js b/client/src/views/notification/badge.js
index 98e712d292..c1faf890a0 100644
--- a/client/src/views/notification/badge.js
+++ b/client/src/views/notification/badge.js
@@ -27,22 +27,48 @@
************************************************************************/
import View from 'view';
+import {inject} from 'di';
+import WebSocketManager from 'web-socket-manager';
class NotificationBadgeView extends View {
template = 'notification/badge'
+ /**
+ * @private
+ * @type {number}
+ */
notificationsCheckInterval = 10
+
+ /**
+ * @private
+ * @type {number}
+ */
groupedCheckInterval = 15
+
+ /**
+ * @private
+ * @type {number}
+ */
waitInterval = 2
/** @private */
useWebSocket = false
+ /**
+ * @private
+ * @type {number|null}
+ */
timeout = null
+
+ /**
+ * @private
+ * @type {number|null}
+ */
groupedTimeout = null
/**
+ * @private
* @type {Object.<string, {
* portalDisabled?: boolean,
* grouped?: boolean,
@@ -56,14 +82,25 @@ class NotificationBadgeView extends View {
*/
popupNotificationsData
+ /**
+ * @private
+ * @type {string}
+ */
soundPath = 'client/sounds/pop_cork'
+ /**
+ * @private
+ * @type {WebSocketManager}
+ */
+ @inject(WebSocketManager)
+ webSocketManager
+
setup() {
this.addActionHandler('showNotifications', () => this.showNotifications());
this.soundPath = this.getBasePath() + (this.getConfig().get('notificationSound') || this.soundPath);
this.notificationSoundsDisabled = true;
- this.useWebSocket = !!this.getHelper().webSocketManager;
+ this.useWebSocket = this.webSocketManager.isEnabled();
const clearTimeouts = () => {
if (this.timeout) {
@@ -304,13 +341,11 @@ class NotificationBadgeView extends View {
}, this.waitInterval * 1000);
};
- const webSocketManager = this.getHelper().webSocketManager;
-
- webSocketManager.subscribe('newNotification', () => onWebSocketNewNotification());
- webSocketManager.subscribeToReconnect(onWebSocketNewNotification);
+ this.webSocketManager.subscribe('newNotification', () => onWebSocketNewNotification());
+ this.webSocketManager.subscribeToReconnect(onWebSocketNewNotification);
- this.once('remove', () => webSocketManager.unsubscribe('newNotification'));
- this.once('remove', () => webSocketManager.unsubscribeFromReconnect(onWebSocketNewNotification));
+ this.once('remove', () => this.webSocketManager.unsubscribe('newNotification'));
+ this.once('remove', () => this.webSocketManager.unsubscribeFromReconnect(onWebSocketNewNotification));
}
/**
@@ -380,7 +415,7 @@ class NotificationBadgeView extends View {
if (useWebSocket) {
const category = 'popupNotifications.' + (data.webSocketCategory || name);
- this.getHelper().webSocketManager.subscribe(category, (c, response) => {
+ this.webSocketManager.subscribe(category, (c, response) => {
if (!response.list) {
return;
}
diff --git a/client/src/views/record/detail.js b/client/src/views/record/detail.js
index 95a98d4716..da35a0eef1 100644
--- a/client/src/views/record/detail.js
+++ b/client/src/views/record/detail.js
@@ -36,6 +36,7 @@ import SelectTemplateModalView from 'views/modals/select-template';
import DebounceHelper from 'helpers/util/debounce';
import {inject} from 'di';
import {ShortcutManager} from 'helpers/site/shortcut-manager';
+import WebSocketManager from 'web-socket-manager';
/**
* A detail record view.
@@ -568,6 +569,13 @@ class DetailRecordView extends BaseRecordView {
*/
_webSocketDebounceInterval = 500
+ /**
+ * @private
+ * @type {WebSocketManager}
+ */
+ @inject(WebSocketManager)
+ webSocketManager
+
/**
* A shortcut-key => action map.
*
@@ -2015,7 +2023,7 @@ class DetailRecordView extends BaseRecordView {
if (
!this.options.webSocketDisabled &&
!this.isNew &&
- !!this.getHelper().webSocketManager &&
+ this.webSocketManager.isEnabled() &&
this.getMetadata().get(['scopes', this.entityType, 'object'])
) {
this.subscribeToWebSocket();
@@ -3573,7 +3581,7 @@ class DetailRecordView extends BaseRecordView {
this.recordUpdateWebSocketTopic = topic;
this.isSubscribedToWebSocket = true;
- this.getHelper().webSocketManager.subscribe(topic, () => this._webSocketDebounceHelper.process());
+ this.webSocketManager.subscribe(topic, () => this._webSocketDebounceHelper.process());
}
/**
@@ -3584,7 +3592,7 @@ class DetailRecordView extends BaseRecordView {
return;
}
- this.getHelper().webSocketManager.unsubscribe(this.recordUpdateWebSocketTopic);
+ this.webSocketManager.unsubscribe(this.recordUpdateWebSocketTopic);
this.isSubscribedToWebSocket = false;
}
diff --git a/client/src/views/record/panels/relationship.js b/client/src/views/record/panels/relationship.js
index 39496079bd..b3910c91b3 100644
--- a/client/src/views/record/panels/relationship.js
+++ b/client/src/views/record/panels/relationship.js
@@ -33,6 +33,8 @@ import SearchManager from 'search-manager';
import RecordModal from 'helpers/record-modal';
import CreateRelatedHelper from 'helpers/record/create-related';
import SelectRelatedHelper from 'helpers/record/select-related';
+import {inject} from 'di';
+import WebSocketManager from 'web-socket-manager';
/**
* A relationship panel.
@@ -103,6 +105,16 @@ class RelationshipPanelView extends BottomPanelView {
*/
listLayoutName
+ /**
+ * Also used by the stream panel view.
+ *
+ * @protected
+ * @type {WebSocketManager}
+ * @since 9.2.0
+ */
+ @inject(WebSocketManager)
+ webSocketManager
+
setup() {
super.setup();
@@ -909,7 +921,7 @@ class RelationshipPanelView extends BottomPanelView {
* @protected
*/
processSyncBack() {
- if (!this.defs.syncBackWithModel || this.getHelper().webSocketManager) {
+ if (!this.defs.syncBackWithModel || this.webSocketManager.isEnabled()) {
return;
}
diff --git a/client/src/views/stream/panel.js b/client/src/views/stream/panel.js
index d394c8617b..a6f2a58097 100644
--- a/client/src/views/stream/panel.js
+++ b/client/src/views/stream/panel.js
@@ -323,7 +323,7 @@ class PanelStreamView extends RelationshipPanelView {
}
subscribeToWebSocket() {
- if (!this.getHelper().webSocketManager) {
+ if (!this.webSocketManager.isEnabled()) {
return;
}
@@ -336,7 +336,7 @@ class PanelStreamView extends RelationshipPanelView {
this.isSubscribedToWebSocket = true;
- this.getHelper().webSocketManager.subscribe(topic, (t, /** Record */data) => {
+ this.webSocketManager.subscribe(topic, (t, /** Record */data) => {
if (data.createdById === this.getUser().id && this._justPosted) {
return;
}
@@ -359,7 +359,7 @@ class PanelStreamView extends RelationshipPanelView {
}
unsubscribeFromWebSocket() {
- this.getHelper().webSocketManager.unsubscribe(this.streamUpdateWebSocketTopic);
+ this.webSocketManager.unsubscribe(this.streamUpdateWebSocketTopic);
}
setupTitle() {
diff --git a/client/src/web-socket-manager.js b/client/src/web-socket-manager.js
index 1d10d7c9d2..ebecf59201 100644
--- a/client/src/web-socket-manager.js
+++ b/client/src/web-socket-manager.js
@@ -53,12 +53,18 @@ class WebSocketManager {
pingTimeout
/**
- * @param {module:models/settings} config A config.
+ * @private
+ * @type {boolean}
+ */
+ enabled = false
+
+ /**
+ * @param {import('models/settings').default} config A config.
*/
constructor(config) {
/**
* @private
- * @type {module:models/settings}
+ * @type {import('models/settings').default}
*/
this.config = config;
@@ -109,20 +115,17 @@ class WebSocketManager {
if (url.indexOf('wss://') === 0) {
this.url = url.substring(6);
this.protocolPart = 'wss://';
- }
- else {
+ } else {
this.url = url.substring(5);
this.protocolPart = 'ws://';
}
- }
- else {
+ } else {
const siteUrl = this.config.get('siteUrl') || '';
if (siteUrl.indexOf('https://') === 0) {
this.url = siteUrl.substring(8);
this.protocolPart = 'wss://';
- }
- else {
+ } else {
this.url = siteUrl.substring(7);
this.protocolPart = 'ws://';
}
@@ -137,8 +140,7 @@ class WebSocketManager {
if (~si) {
this.url = this.url.substring(0, si) + ':' + port;
- }
- else {
+ } else {
this.url += ':' + port;
}
@@ -162,8 +164,7 @@ class WebSocketManager {
try {
this.connectInternal(auth, userId, url);
- }
- catch (e) {
+ } catch (e) {
console.error(e.message);
this.connection = null;
@@ -265,12 +266,10 @@ class WebSocketManager {
category: category,
callback: callback,
});
- }
- catch (e) {
+ } catch (e) {
if (e.message) {
console.error(e.message);
- }
- else {
+ } else {
console.error("WebSocket: Could not subscribe to "+category+".");
}
}
@@ -305,12 +304,10 @@ class WebSocketManager {
try {
this.connection.unsubscribe(category, callback);
- }
- catch (e) {
+ } catch (e) {
if (e.message) {
console.error(e.message);
- }
- else {
+ } else {
console.error("WebSocket: Could not unsubscribe from "+category+".");
}
}
@@ -331,8 +328,7 @@ class WebSocketManager {
try {
this.connection.close();
- }
- catch (e) {
+ } catch (e) {
console.error(e.message);
}
@@ -367,6 +363,24 @@ class WebSocketManager {
this.schedulePing();
}, this.pingInterval * 1000);
}
+
+ /**
+ * @internal
+ * @since 9.2.0
+ */
+ setEnabled() {
+ this.enabled = true;
+ }
+
+ /**
+ * Is enabled.
+ *
+ * @return {boolean}
+ * @since 9.2.0
+ */
+ isEnabled() {
+ return this.enabled;
+ }
}
export default WebSocketManager;