Skip to content

EspoCRM – Open Source CRM Application

websocket reconnect subscribe

Yuri Kuznetsov73593675695c
diff --git a/client/src/views/notification/badge.js b/client/src/views/notification/badge.js
index 8284317f50..98e712d292 100644
--- a/client/src/views/notification/badge.js
+++ b/client/src/views/notification/badge.js
@@ -304,7 +304,13 @@ class NotificationBadgeView extends View {
}, this.waitInterval * 1000);
};
- this.getHelper().webSocketManager.subscribe('newNotification', () => onWebSocketNewNotification());
+ const webSocketManager = this.getHelper().webSocketManager;
+
+ webSocketManager.subscribe('newNotification', () => onWebSocketNewNotification());
+ webSocketManager.subscribeToReconnect(onWebSocketNewNotification);
+
+ this.once('remove', () => webSocketManager.unsubscribe('newNotification'));
+ this.once('remove', () => webSocketManager.unsubscribeFromReconnect(onWebSocketNewNotification));
}
/**
diff --git a/client/src/web-socket-manager.js b/client/src/web-socket-manager.js
index 21cd81adb8..1d10d7c9d2 100644
--- a/client/src/web-socket-manager.js
+++ b/client/src/web-socket-manager.js
@@ -62,6 +62,12 @@ class WebSocketManager {
*/
this.config = config;
+ /**
+ * @private
+ * @type {Function[]}
+ */
+ this.subscribeToReconnectQueue = [];
+
/**
* @private
* @type {{category: string, callback: Function}[]}
@@ -171,6 +177,8 @@ class WebSocketManager {
* @param {string} url
*/
connectInternal(auth, userId, url) {
+ let wasConnected = false;
+
this.connection = new ab.Session(
url,
() => {
@@ -182,7 +190,13 @@ class WebSocketManager {
this.subscribeQueue = [];
+ if (wasConnected) {
+ this.subscribeToReconnectQueue.forEach(callback => callback());
+ }
+
this.schedulePing();
+
+ wasConnected = true;
},
code => {
if (code === ab.CONNECTION_CLOSED) {
@@ -204,6 +218,26 @@ class WebSocketManager {
);
}
+ /**
+ * Subscribe to reconnecting.
+ *
+ * @param {function(): void} callback A callback.
+ * @since 9.1.1
+ */
+ subscribeToReconnect(callback) {
+ this.subscribeToReconnectQueue.push(callback);
+ }
+
+ /**
+ * Unsubscribe from reconnecting.
+ *
+ * @param {function(): void} callback A callback.
+ * @since 9.1.1
+ */
+ unsubscribeFromReconnect(callback) {
+ this.subscribeToReconnectQueue = this.subscribeToReconnectQueue.filter(it => it !== callback);
+ }
+
/**
* Subscribe to a topic.
*
@@ -254,11 +288,19 @@ class WebSocketManager {
}
this.subscribeQueue = this.subscribeQueue.filter(item => {
- return item.category !== category && item.callback !== callback;
+ if (callback === undefined) {
+ return item.category !== category;
+ }
+
+ return item.category !== category || item.callback !== callback;
});
this.subscriptions = this.subscriptions.filter(item => {
- return item.category !== category && item.callback !== callback;
+ if (callback === undefined) {
+ return item.category !== category;
+ }
+
+ return item.category !== category || item.callback !== callback;
});
try {