@amityco/ts-sdk-react-native 6.35.2 → 6.35.3-ab24b11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amityco/ts-sdk-react-native",
3
- "version": "6.35.2",
3
+ "version": "6.35.3-ab24b11.0",
4
4
  "license": "CC-BY-ND-4.0",
5
5
  "author": "amity.co <developers@amity.co> (https://amity.co)",
6
6
  "description": "Amity Social Cloud Typescript SDK",
@@ -34,6 +34,8 @@ declare global {
34
34
  type MqttClient = {
35
35
  connect: (params: { accessToken: string; userId: string }) => Promise<void>;
36
36
  disconnect: () => Promise<void>;
37
+ reconnect: () => void;
38
+ listen(callback: (state: Amity.RTEConnectionState) => void): () => void;
37
39
  get connected(): boolean;
38
40
  on: <T extends keyof Amity.MqttEvents>(
39
41
  event: T,
@@ -31,6 +31,16 @@ declare global {
31
31
  TERMINATED = 'terminated',
32
32
  }
33
33
 
34
+ enum RTEConnectionStateEnum {
35
+ CONNECTING = 'connecting', // actively establishing a connection (initial or reconnection)
36
+ CONNECTED = 'connected', // connected to broker
37
+ RECONNECTING = 'reconnecting', // lost connection and attempting to reconnect
38
+ DISCONNECTED = 'disconnected', // disconnected (not currently connected, and not automatically reconnecting)
39
+ DISCONNECTING = 'disconnecting', // disconnected (not currently connected, and not automatically reconnecting)
40
+ }
41
+
42
+ type RTEConnectionState = `${Amity.RTEConnectionStateEnum}`;
43
+
34
44
  type Client = {
35
45
  version: string;
36
46
 
@@ -60,6 +70,10 @@ declare global {
60
70
 
61
71
  accessTokenExpiryWatcher: (sessionHandler: Amity.SessionHandler) => Amity.Unsubscriber;
62
72
 
73
+ onRTEConnectionStateChange: (
74
+ callback: (state: Amity.RTEConnectionState) => void,
75
+ ) => (() => void) | undefined;
76
+
63
77
  getFeedSettings: () => Promise<Amity.FeedSettings>;
64
78
  getSocialSettings: () => Promise<Amity.SocialSettings>;
65
79
  getMessagePreviewSetting: (refresh?: boolean) => Promise<Amity.MessagePreviewSetting>;
@@ -21,6 +21,7 @@ import { getFeedSettings } from './getFeedSettings';
21
21
 
22
22
  import { accessTokenExpiryWatcher } from './accessTokenExpiryWatcher';
23
23
  import { getMarkerSyncConsistentMode } from '../utils/markerSyncEngine';
24
+ import { onRTEConnectionStateChange } from './onRTEConnectionStateChange';
24
25
 
25
26
  const DEFAULT_DEBUG_SESSION = 'amity';
26
27
 
@@ -121,6 +122,8 @@ export const createClient = (
121
122
  getSocialSettings,
122
123
  getMessagePreviewSetting,
123
124
 
125
+ onRTEConnectionStateChange,
126
+
124
127
  use: () => setActiveClient(client),
125
128
 
126
129
  isUnreadCountEnabled,
@@ -21,3 +21,5 @@ export * from './setUploadedFileAccessType';
21
21
 
22
22
  export * from './registerPushNotification';
23
23
  export * from './unregisterPushNotification';
24
+
25
+ export * from './onRTEConnectionStateChange';
@@ -0,0 +1,24 @@
1
+ import { getActiveClient } from './activeClient';
2
+
3
+ /**
4
+ * ```js
5
+ * import { Client } from '@amityco/ts-sdk'
6
+ * const unsubscribe = Client.onRTEConnectionStateChange((state) => console.log(state))
7
+ * unsubscribe()
8
+ * ```
9
+ * @param callback The function to call when the event was fired
10
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
11
+ *
12
+ * @category Client API
13
+ */
14
+ export const onRTEConnectionStateChange = (
15
+ callback: (state: Amity.RTEConnectionState) => void,
16
+ ): (() => void) | undefined => {
17
+ const client = getActiveClient();
18
+
19
+ if (client.mqtt) {
20
+ return client.mqtt?.listen(callback);
21
+ }
22
+
23
+ return undefined;
24
+ };
@@ -50,11 +50,25 @@ export function getMqttOptions(params: {
50
50
  */
51
51
  export const createMqttTransport = (endpoint: string): Amity.MqttClient => {
52
52
  let mqttClient: MqttClient;
53
+ let currentState: Amity.RTEConnectionState = 'disconnected';
54
+ const connectionStateListeners: Set<(state: Amity.RTEConnectionState) => void> = new Set();
55
+
56
+ const updateConnectionState = (state: Amity.RTEConnectionState) => {
57
+ if (currentState === state) return;
58
+
59
+ currentState = state;
60
+
61
+ connectionStateListeners.forEach(listener => {
62
+ listener(state);
63
+ });
64
+ };
53
65
 
54
66
  async function connect(params: { accessToken: string; userId: string }): Promise<void> {
55
67
  const clientId = await getMQTTClientId(params.userId);
68
+ updateConnectionState('connecting');
56
69
 
57
70
  if (mqttClient) {
71
+ updateConnectionState('disconnecting');
58
72
  mqttClient.removeAllListeners();
59
73
  mqttClient.end(true);
60
74
  }
@@ -70,7 +84,7 @@ export const createMqttTransport = (endpoint: string): Amity.MqttClient => {
70
84
 
71
85
  mqttClient.on('connect', () => {
72
86
  mqttClient.options.reconnectPeriod = RETRY_BASE_TIMEOUT;
73
-
87
+ updateConnectionState('connected');
74
88
  subscribeGlobalTopic();
75
89
  });
76
90
 
@@ -81,10 +95,12 @@ export const createMqttTransport = (endpoint: string): Amity.MqttClient => {
81
95
  case MqttError.BAD_USERNAME_OR_PASSWORD:
82
96
  case MqttError.NOT_AUTHORIZED:
83
97
  mqttClient.end();
98
+ updateConnectionState('disconnected');
84
99
  }
85
100
  });
86
101
 
87
102
  mqttClient.on('reconnect', () => {
103
+ updateConnectionState('reconnecting');
88
104
  // Double the reconnect period for each attempt
89
105
  mqttClient.options.reconnectPeriod = Math.min(
90
106
  (mqttClient.options.reconnectPeriod || RETRY_BASE_TIMEOUT) * 2,
@@ -92,6 +108,14 @@ export const createMqttTransport = (endpoint: string): Amity.MqttClient => {
92
108
  );
93
109
  });
94
110
 
111
+ mqttClient.on('close', () => {
112
+ updateConnectionState('disconnected');
113
+ });
114
+
115
+ mqttClient.on('disconnect', () => {
116
+ updateConnectionState('disconnected');
117
+ });
118
+
95
119
  return new Promise(resolve => mqttClient!.once('connect', () => resolve()));
96
120
  }
97
121
 
@@ -102,6 +126,21 @@ export const createMqttTransport = (endpoint: string): Amity.MqttClient => {
102
126
  return new Promise(resolve => mqttClient?.end(true, undefined, () => resolve()));
103
127
  }
104
128
  },
129
+ async reconnect(): Promise<void> {
130
+ return new Promise(resolve => {
131
+ mqttClient?.reconnect();
132
+ resolve();
133
+ });
134
+ },
135
+ listen(callback: (state: Amity.RTEConnectionState) => void): () => void {
136
+ connectionStateListeners.add(callback);
137
+
138
+ if (currentState) callback(currentState);
139
+
140
+ return () => {
141
+ connectionStateListeners.delete(callback);
142
+ };
143
+ },
105
144
  get connected() {
106
145
  return !!mqttClient?.connected;
107
146
  },