@djangocfg/centrifugo 2.1.80 → 2.1.82

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": "@djangocfg/centrifugo",
3
- "version": "2.1.80",
3
+ "version": "2.1.82",
4
4
  "description": "Production-ready Centrifugo WebSocket client for React with real-time subscriptions, RPC patterns, and connection state management",
5
5
  "keywords": [
6
6
  "centrifugo",
@@ -51,9 +51,9 @@
51
51
  "centrifuge": "^5.2.2"
52
52
  },
53
53
  "peerDependencies": {
54
- "@djangocfg/api": "^2.1.80",
55
- "@djangocfg/ui-nextjs": "^2.1.80",
56
- "@djangocfg/layouts": "^2.1.80",
54
+ "@djangocfg/api": "^2.1.82",
55
+ "@djangocfg/ui-nextjs": "^2.1.82",
56
+ "@djangocfg/layouts": "^2.1.82",
57
57
  "consola": "^3.4.2",
58
58
  "lucide-react": "^0.545.0",
59
59
  "moment": "^2.30.1",
@@ -61,7 +61,7 @@
61
61
  "react-dom": "^19.1.0"
62
62
  },
63
63
  "devDependencies": {
64
- "@djangocfg/typescript-config": "^2.1.80",
64
+ "@djangocfg/typescript-config": "^2.1.82",
65
65
  "@types/react": "^19.1.0",
66
66
  "@types/react-dom": "^19.1.0",
67
67
  "moment": "^2.30.1",
@@ -115,6 +115,7 @@ function CentrifugoProviderInner({
115
115
  const connectRef = useRef<(() => Promise<void>) | null>(null);
116
116
  const disconnectRef = useRef<(() => void) | null>(null);
117
117
  const wasConnectedBeforeHiddenRef = useRef(false); // Track connection state before page hidden
118
+ const clientInstanceRef = useRef<CentrifugoRPCClient | null>(null); // Stable client reference for reconnection
118
119
 
119
120
  const centrifugoToken: CentrifugoToken | undefined = user?.centrifugo;
120
121
  const hasCentrifugoToken = !!centrifugoToken?.token;
@@ -222,6 +223,40 @@ function CentrifugoProviderInner({
222
223
  setError(null);
223
224
 
224
225
  try {
226
+ // Check if we can reuse existing client (reconnection scenario)
227
+ if (clientInstanceRef.current) {
228
+ logger.info('Reconnecting to WebSocket server (reusing client)...');
229
+ await clientInstanceRef.current.connect();
230
+
231
+ if (!isMountedRef.current) {
232
+ isConnectingRef.current = false;
233
+ return;
234
+ }
235
+
236
+ hasConnectedRef.current = true;
237
+ isConnectingRef.current = false;
238
+
239
+ // Clear any pending reconnect timeout
240
+ if (reconnectTimeoutRef.current) {
241
+ clearTimeout(reconnectTimeoutRef.current);
242
+ reconnectTimeoutRef.current = null;
243
+ }
244
+
245
+ // Use existing client reference - NO setClient() call to keep reference stable
246
+ setIsConnected(true);
247
+ setConnectionTime(new Date());
248
+ setError(null);
249
+
250
+ logger.success('WebSocket reconnected');
251
+
252
+ // Reset reconnect state on successful connection
253
+ reconnectAttemptRef.current = 0;
254
+ devWarningShownRef.current = false;
255
+ reconnectStoppedRef.current = false;
256
+ return;
257
+ }
258
+
259
+ // First connection - create new client
225
260
  logger.info('Connecting to WebSocket server...');
226
261
 
227
262
  if (!centrifugoToken?.token) {
@@ -265,6 +300,8 @@ function CentrifugoProviderInner({
265
300
  reconnectTimeoutRef.current = null;
266
301
  }
267
302
 
303
+ // Store client in ref for future reconnections
304
+ clientInstanceRef.current = rpcClient;
268
305
  setClient(rpcClient);
269
306
  setIsConnected(true);
270
307
  setConnectionTime(new Date());