reachlo 1.4.2 → 1.4.5

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/README.md CHANGED
@@ -96,11 +96,13 @@ Deliver real-time metrics, alerts, and notifications at scale.
96
96
 
97
97
  ## Advanced Configuration
98
98
 
99
- For self-hosted or custom deployments:
99
+ For custom deployments or to tune retry behavior:
100
100
 
101
101
  ```js
102
102
  const client = new Reachlo('YOUR_API_KEY', {
103
- url: 'wss://your-custom-backend.com'
103
+ url: 'wss://your-custom-backend.com', // custom backend URL
104
+ maxInitialRetries: 3, // retries before first successful connect (auth failures)
105
+ maxNetworkRetries: 50 // retries after a drop (network failures). 0 = unlimited
104
106
  });
105
107
  ```
106
108
 
package/index.d.ts CHANGED
@@ -2,6 +2,8 @@ export interface ReachloOptions {
2
2
  url?: string;
3
3
  heartbeatInterval?: number;
4
4
  reconnectInterval?: number;
5
+ maxInitialRetries?: number;
6
+ maxNetworkRetries?: number;
5
7
  }
6
8
 
7
9
  export default class Reachlo {
package/index.js CHANGED
@@ -14,6 +14,8 @@ class Reachlo {
14
14
  this.heartbeatInterval = options.heartbeatInterval || 20000;
15
15
  this.reconnectBaseDelay = options.reconnectBaseDelay || 1000;
16
16
  this.reconnectMaxDelay = options.reconnectMaxDelay || 30000;
17
+ this.maxInitialRetries = options.maxInitialRetries ?? 3; // never opened: give up fast
18
+ this.maxNetworkRetries = options.maxNetworkRetries ?? 50; // ~25 mins of attempts at max backoff
17
19
  this.batchInterval = options.batchInterval || 0; // 0 = disabled
18
20
 
19
21
  // Batch state
@@ -82,6 +84,7 @@ class Reachlo {
82
84
 
83
85
  connect() {
84
86
  this.shouldReconnect = true;
87
+ this._connectionEstablished = false;
85
88
  return new Promise((resolve, reject) => {
86
89
  if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
87
90
 
@@ -95,6 +98,7 @@ class Reachlo {
95
98
  this.socket = new WebSocket(this.url, protocols);
96
99
 
97
100
  this.socket.onopen = () => {
101
+ this._connectionEstablished = true;
98
102
  this._log("Reachlo: Connected");
99
103
  this.reconnecting = false;
100
104
  this._reconnectAttempt = 0; // Reset backoff on successful connect
@@ -115,17 +119,24 @@ class Reachlo {
115
119
  });
116
120
  this.pendingSubs.clear();
117
121
 
118
- // Flush Offline Buffer (if enabled)
119
- if (this.bufferPublish && this.offlineBuffer.length > 0) {
122
+ // Flush Offline Buffer (non-publish always, publish if enabled)
123
+ if (this.offlineBuffer.length > 0) {
120
124
  const toFlush = [...this.offlineBuffer];
121
125
  this.offlineBuffer = [];
122
- toFlush.forEach(msg => this._send(msg));
126
+ toFlush.forEach(msg => {
127
+ if (msg.type !== 'publish' || this.bufferPublish) {
128
+ this._send(msg);
129
+ }
130
+ });
123
131
  }
124
132
 
125
133
  resolve();
126
134
  };
127
135
 
128
136
  this.socket.onclose = (event) => {
137
+ const connectionWasEstablished = this._connectionEstablished;
138
+ this._connectionEstablished = false;
139
+
129
140
  this._log("Reachlo: Connection closed", event.code, event.reason);
130
141
  this._stopHeartbeat();
131
142
 
@@ -153,6 +164,24 @@ class Reachlo {
153
164
  }
154
165
 
155
166
  if (this.shouldReconnect) {
167
+ if (!connectionWasEstablished) {
168
+ // Never connected — likely bad key, give up after maxInitialRetries
169
+ if (this.maxInitialRetries > 0 && this._reconnectAttempt >= this.maxInitialRetries) {
170
+ this._error("Reachlo: Could not establish connection. Check your API key.");
171
+ this.shouldReconnect = false;
172
+ this._emitLifecycle('error');
173
+ return;
174
+ }
175
+ } else {
176
+ // Was connected before — genuine network issue
177
+ if (this.maxNetworkRetries > 0 && this._reconnectAttempt >= this.maxNetworkRetries) {
178
+ this._error("Reachlo: Network retry limit reached, giving up.");
179
+ this.shouldReconnect = false;
180
+ this._emitLifecycle('error');
181
+ return;
182
+ }
183
+ }
184
+
156
185
  if (!this.reconnecting) {
157
186
  this.reconnecting = true;
158
187
  this._emitLifecycle('reconnecting');
@@ -240,7 +269,12 @@ class Reachlo {
240
269
 
241
270
 
242
271
  if (this.socket) {
243
- this.socket.close();
272
+ // Close with code 1000 (Normal Closure) to prevent reconnect logic from misinterpreting
273
+ if (this.socket.readyState === 1) {
274
+ this.socket.close(1000, "Client disconnected explicitly");
275
+ } else {
276
+ this.socket.close();
277
+ }
244
278
  this.socket = null;
245
279
  }
246
280
  this.pendingSubs.clear();
@@ -275,7 +309,9 @@ class Reachlo {
275
309
  this.socket.send(JSON.stringify(payload));
276
310
  return true;
277
311
  } else {
278
- if (this.bufferPublish && payload.type === 'publish') {
312
+ // Drop back to offline buffer to ensure sync requests or others make it when reconnecting!
313
+ // E.g. user calls sync() before connect() resolves.
314
+ if (this.bufferPublish || payload.type !== 'publish') {
279
315
  this.offlineBuffer.push(payload);
280
316
  return true; // Queued
281
317
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reachlo",
3
- "version": "1.4.2",
3
+ "version": "1.4.5",
4
4
  "description": "Streaming-native real-time infrastructure. Ordered channels, durable replay, presence, backpressure, and ACK delivery.",
5
5
  "main": "index.js",
6
6
  "type": "module",