Skip to content

Commit f1b1b51

Browse files
akanshaaa19kurund
andauthored
Enhance WebSocket Connection Reliability and Error Handling (#3336)
* enhancement (subscriptions): add error handling for subscriptions and enhance WebSocket configuration * fix: enhance WebSocket error handling and logging * refactor: remove WebSocket retry logic and attempts configuration --------- Co-authored-by: Kurund Jalmi <kurund.jalmi@gmail.com>
1 parent ce03859 commit f1b1b51

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

src/common/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,12 @@ export const formatString = (str: string) =>
294294
.replace(/_/g, ' ')
295295
.replace(/([a-z])([0-9])/gi, '$1 $2')
296296
.replace(/\b\w/g, (char) => char.toUpperCase());
297+
298+
export const handleSubscriptionError = (error: any, subscriptionType: string, refetch: any) => {
299+
setLogs(`Subscription error for ${subscriptionType}: ${error}`, 'error');
300+
301+
if (error?.message?.includes('Socket closed')) {
302+
// Trigger a refetch if we lose connection
303+
refetch(true);
304+
}
305+
};

src/config/apolloclient.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ const gqlClient = (navigate: any) => {
143143
authToken: getAuthSession('access_token'),
144144
userId: getUserSession('id'),
145145
},
146+
keepAlive: 30000,
147+
on: {
148+
closed: (event: any) => {
149+
setLogs(`WebSocket closed with code ${event.code} and reason: ${event.reason}`, 'error');
150+
},
151+
error: (error) => {
152+
setLogs(`WebSocket error: ${error}`, 'error');
153+
},
154+
},
146155
})
147156
);
148157

src/containers/Chat/ChatSubscription/ChatSubscription.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
SEARCH_QUERY_VARIABLES,
1111
} from 'common/constants';
1212
import { setErrorMessage } from 'common/notification';
13-
import { randomIntFromInterval, addLogs } from 'common/utils';
13+
import { randomIntFromInterval, addLogs, handleSubscriptionError } from 'common/utils';
1414
import { saveConversation } from 'services/ChatService';
1515
import { getUserSession } from 'services/AuthService';
1616
import { SEARCH_QUERY } from 'graphql/queries/Search';
@@ -206,6 +206,7 @@ export const ChatSubscription = ({ setDataLoaded }: ChatSubscriptionProps) => {
206206
document: COLLECTION_SENT_SUBSCRIPTION,
207207
variables: subscriptionVariables,
208208
updateQuery: (prev, { subscriptionData }) => updateConversations(prev, subscriptionData, 'COLLECTION'),
209+
onError: (error) => handleSubscriptionError(error, 'COLLECTION', setTriggerRefetch),
209210
});
210211
}
211212
}, [collectionSubscribe]);
@@ -218,21 +219,23 @@ export const ChatSubscription = ({ setDataLoaded }: ChatSubscriptionProps) => {
218219
document: MESSAGE_RECEIVED_SUBSCRIPTION,
219220
variables: subscriptionVariables,
220221
updateQuery: (prev, { subscriptionData }) => updateConversations(prev, subscriptionData, 'RECEIVED'),
222+
onError: (error) => handleSubscriptionError(error, 'MESSAGE_RECEIVED', setTriggerRefetch),
221223
});
222224

223225
// message sent subscription
224226
subscribeToMore({
225227
document: MESSAGE_SENT_SUBSCRIPTION,
226228
variables: subscriptionVariables,
227229
updateQuery: (prev, { subscriptionData }) => updateConversations(prev, subscriptionData, 'SENT'),
230+
onError: (error) => handleSubscriptionError(error, 'SENT', setTriggerRefetch),
228231
});
229232

230233
// message status subscription
231234
subscribeToMore({
232235
document: MESSAGE_STATUS_SUBSCRIPTION,
233236
variables: subscriptionVariables,
234237
updateQuery: (prev, { subscriptionData }) => updateConversations(prev, subscriptionData, 'STATUS'),
235-
onError: () => {},
238+
onError: (error) => handleSubscriptionError(error, 'STATUS', setTriggerRefetch),
236239
});
237240
}
238241
}, [subscribeToMore]);

src/containers/WaGroups/GroupMessageSubscription.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
REFETCH_RANDOM_TIME_MIN,
99
} from 'common/constants';
1010
import { setErrorMessage } from 'common/notification';
11-
import { randomIntFromInterval, addLogs } from 'common/utils';
11+
import { randomIntFromInterval, addLogs, handleSubscriptionError } from 'common/utils';
1212
import { GROUP_SEARCH_QUERY } from 'graphql/queries/WaGroups';
1313
import {
1414
SENT_MESSAGE_WA_GROUP_COLLECTION,
@@ -193,20 +193,22 @@ export const GroupMessageSubscription = ({ setDataLoaded }: GroupMessageProps) =
193193
document: WA_MESSAGE_RECEIVED_SUBSCRIPTION,
194194
variables: subscriptionVariables,
195195
updateQuery: (prev, { subscriptionData }) => updateConversations(prev, subscriptionData, 'RECEIVED'),
196+
onError: (error) => handleSubscriptionError(error, 'WA_RECEIVED', setTriggerRefetch),
196197
});
197198

198199
// message sent subscription
199200
subscribeToMore({
200201
document: WA_MESSAGE_SENT_SUBSCRIPTION,
201202
variables: subscriptionVariables,
202203
updateQuery: (prev, { subscriptionData }) => updateConversations(prev, subscriptionData, 'SENT'),
204+
onError: (error) => handleSubscriptionError(error, 'WA_SENT', setTriggerRefetch),
203205
});
204206

205207
subscribeToMore({
206208
document: UPDATE_WA_MESSAGE_STATUS,
207209
variables: subscriptionVariables,
208-
onError: (error) => console.log(error),
209210
updateQuery: (prev, { subscriptionData }) => updateConversations(prev, subscriptionData, 'STATUS'),
211+
onError: (error) => handleSubscriptionError(error, 'WA_STATUS', setTriggerRefetch),
210212
});
211213
}
212214
}, [subscribeToMore]);

src/containers/WalletBalance/WalletBalance.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getUserSession } from 'services/AuthService';
1111
import { GUPSHUP_ENTERPRISE_SHORTCODE } from 'common/constants';
1212
import { ProviderContext } from 'context/session';
1313
import styles from './WalletBalance.module.css';
14+
import { handleSubscriptionError } from 'common/utils';
1415

1516
export interface WalletBalanceProps {
1617
fullOpen: boolean;
@@ -104,6 +105,7 @@ export const WalletBalance = ({ fullOpen }: WalletBalanceProps) => {
104105
updateBalanceValue(balance.balance);
105106
}
106107
},
108+
onError: (error) => handleSubscriptionError(error, 'WALLET_BALANCE', setRetried),
107109
});
108110
}
109111
}, [subscribeToMore]);

0 commit comments

Comments
 (0)