Skip to content

Commit

Permalink
[IMPROVE] Connection stability (RocketChat#3531)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolmello authored Dec 7, 2021
1 parent d4e86aa commit 9fbb22a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 46 deletions.
6 changes: 0 additions & 6 deletions app/ee/omnichannel/lib/subscriptions/inquiry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { inquiryQueueAdd, inquiryQueueRemove, inquiryQueueUpdate, inquiryRequest
const removeListener = listener => listener.stop();

let connectedListener;
let disconnectedListener;
let queueListener;

const streamTopic = 'stream-livechat-inquiry-queue-observer';
Expand Down Expand Up @@ -48,18 +47,13 @@ export default function subscribeInquiry() {
connectedListener.then(removeListener);
connectedListener = false;
}
if (disconnectedListener) {
disconnectedListener.then(removeListener);
disconnectedListener = false;
}
if (queueListener) {
queueListener.then(removeListener);
queueListener = false;
}
};

connectedListener = RocketChat.onStreamData('connected', handleConnection);
disconnectedListener = RocketChat.onStreamData('close', handleConnection);
queueListener = RocketChat.onStreamData(streamTopic, handleQueueMessageReceived);

try {
Expand Down
17 changes: 0 additions & 17 deletions app/lib/methods/subscriptions/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import messagesStatus from '../../../constants/messagesStatus';
import log from '../../../utils/log';
import random from '../../../utils/random';
import store from '../../createStore';
import { roomsRequest } from '../../../actions/rooms';
import { handlePayloadUserInteraction } from '../actions';
import buildMessage from '../helpers/buildMessage';
import RocketChat from '../../rocketchat';
Expand All @@ -21,8 +20,6 @@ import { E2E_MESSAGE_TYPE } from '../../encryption/constants';

const removeListener = listener => listener.stop();

let connectedListener;
let disconnectedListener;
let streamListener;
let subServer;
let queue = {};
Expand Down Expand Up @@ -255,10 +252,6 @@ const debouncedUpdate = subscription => {
};

export default function subscribeRooms() {
const handleConnection = () => {
store.dispatch(roomsRequest());
};

const handleStreamMessageReceived = protectedFunction(async ddpMessage => {
const db = database.active;

Expand Down Expand Up @@ -388,14 +381,6 @@ export default function subscribeRooms() {
});

const stop = () => {
if (connectedListener) {
connectedListener.then(removeListener);
connectedListener = false;
}
if (disconnectedListener) {
disconnectedListener.then(removeListener);
disconnectedListener = false;
}
if (streamListener) {
streamListener.then(removeListener);
streamListener = false;
Expand All @@ -407,8 +392,6 @@ export default function subscribeRooms() {
}
};

connectedListener = this.sdk.onStreamData('connected', handleConnection);
// disconnectedListener = this.sdk.onStreamData('close', handleConnection);
streamListener = this.sdk.onStreamData('stream-notify-user', handleStreamMessageReceived);

try {
Expand Down
41 changes: 19 additions & 22 deletions app/lib/rocketchat.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,37 +239,34 @@ const RocketChat = {
this.code = null;
}

this.sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl: useSsl(server) });
// The app can't reconnect if reopen interval is 5s while in development
this.sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl: useSsl(server), reopen: __DEV__ ? 20000 : 5000 });
this.getSettings();

const sdkConnect = () =>
this.sdk
.connect()
.then(() => {
const { server: currentServer } = reduxStore.getState().server;
if (user && user.token && server === currentServer) {
reduxStore.dispatch(loginRequest({ resume: user.token }, logoutOnError));
}
})
.catch(err => {
console.log('connect error', err);

// when `connect` raises an error, we try again in 10 seconds
this.connectTimeout = setTimeout(() => {
if (this.sdk?.client?.host === server) {
sdkConnect();
}
}, 10000);
});

sdkConnect();
this.sdk
.connect()
.then(() => {
console.log('connected');
})
.catch(err => {
console.log('connect error', err);
});

this.connectingListener = this.sdk.onStreamData('connecting', () => {
reduxStore.dispatch(connectRequest());
});

this.connectedListener = this.sdk.onStreamData('connected', () => {
const { connected } = reduxStore.getState().meteor;
if (connected) {
return;
}
reduxStore.dispatch(connectSuccess());
const { server: currentServer } = reduxStore.getState().server;
const { user } = reduxStore.getState().login;
if (user?.token && server === currentServer) {
reduxStore.dispatch(loginRequest({ resume: user.token }, logoutOnError));
}
});

this.closeListener = this.sdk.onStreamData('close', () => {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3760,7 +3760,7 @@

"@rocket.chat/sdk@RocketChat/Rocket.Chat.js.SDK#mobile":
version "1.1.0-mobile"
resolved "https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/0ee2ded22b08b34ce7ab62b26e42a713dca0d1ac"
resolved "https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/c64e69ea22514ae3bbe24e36ca77868fdae76157"
dependencies:
js-sha256 "^0.9.0"
lru-cache "^4.1.1"
Expand Down

0 comments on commit 9fbb22a

Please sign in to comment.