@stomp/stompjs 7.1.0 → 7.2.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/src/client.ts CHANGED
@@ -7,6 +7,7 @@ import {
7
7
  ActivationState,
8
8
  closeEventCallbackType,
9
9
  debugFnType,
10
+ emptyCallbackType,
10
11
  frameCallbackType,
11
12
  IPublishParams,
12
13
  IStompSocket,
@@ -30,173 +31,297 @@ declare const WebSocket: {
30
31
  * STOMP Client Class.
31
32
  *
32
33
  * Part of `@stomp/stompjs`.
34
+ *
35
+ * This class provides a robust implementation for connecting to and interacting with a
36
+ * STOMP-compliant messaging broker over WebSocket. It supports STOMP versions 1.2, 1.1, and 1.0.
37
+ *
38
+ * Features:
39
+ * - Handles automatic reconnections.
40
+ * - Supports heartbeat mechanisms to detect and report communication failures.
41
+ * - Allows customization of connection and WebSocket behaviors through configurations.
42
+ * - Compatible with both browser environments and Node.js with polyfill support for WebSocket.
33
43
  */
34
44
  export class Client {
35
45
  /**
36
46
  * The URL for the STOMP broker to connect to.
37
- * Typically like `"ws://broker.329broker.com:15674/ws"` or `"wss://broker.329broker.com:15674/ws"`.
38
- *
39
- * Only one of this or [Client#webSocketFactory]{@link Client#webSocketFactory} need to be set.
40
- * If both are set, [Client#webSocketFactory]{@link Client#webSocketFactory} will be used.
41
- *
42
- * If your environment does not support WebSockets natively, please refer to
43
- * [Polyfills]{@link https://stomp-js.github.io/guide/stompjs/rx-stomp/ng2-stompjs/pollyfils-for-stompjs-v5.html}.
47
+ * Example: `"ws://broker.domain.com:15674/ws"` or `"wss://broker.domain.com:15674/ws"`.
48
+ *
49
+ * Use this property to define the broker's WebSocket endpoint.
50
+ * Note:
51
+ * - Only one of `brokerURL` or [Client#webSocketFactory]{@link Client#webSocketFactory} needs to be set.
52
+ * - If both are provided, [Client#webSocketFactory]{@link Client#webSocketFactory} takes precedence.
53
+ * - When targeting environments without native WebSocket support, refer to
54
+ * [Polyfills]{@link https://stomp-js.github.io/guide/stompjs/rx-stomp/ng2-stompjs/pollyfils-for-stompjs-v5.html}.
44
55
  */
45
56
  public brokerURL: string | undefined;
46
57
 
47
58
  /**
48
- * STOMP versions to attempt during STOMP handshake. By default, versions `1.2`, `1.1`, and `1.0` are attempted.
59
+ * STOMP protocol versions to use during the handshake. By default, the client will attempt
60
+ * versions `1.2`, `1.1`, and `1.0` in descending order of preference.
49
61
  *
50
62
  * Example:
51
63
  * ```javascript
52
- * // Try only versions 1.1 and 1.0
53
- * client.stompVersions = new Versions(['1.1', '1.0'])
64
+ * // Configure the client to only use versions 1.1 and 1.0
65
+ * client.stompVersions = new Versions(['1.1', '1.0']);
54
66
  * ```
55
67
  */
56
68
  public stompVersions = Versions.default;
57
69
 
58
70
  /**
59
- * This function should return a WebSocket or a similar (e.g. SockJS) object.
60
- * If your environment does not support WebSockets natively, please refer to
61
- * [Polyfills]{@link https://stomp-js.github.io/guide/stompjs/rx-stomp/ng2-stompjs/pollyfils-for-stompjs-v5.html}.
62
- * If your STOMP Broker supports WebSockets, prefer setting [Client#brokerURL]{@link Client#brokerURL}.
71
+ * A function that returns a WebSocket or a similar object (e.g., SockJS) to establish connections.
63
72
  *
64
- * If both this and [Client#brokerURL]{@link Client#brokerURL} are set, this will be used.
73
+ * This is an alternative to [Client#brokerURL]{@link Client#brokerURL}.
74
+ * Using this allows finer control over WebSocket creation, especially for custom wrappers
75
+ * or when working in non-standard environments.
65
76
  *
66
77
  * Example:
67
78
  * ```javascript
68
- * // use a WebSocket
69
- * client.webSocketFactory= function () {
70
- * return new WebSocket("wss://broker.329broker.com:15674/ws");
71
- * };
72
- *
73
- * // Typical usage with SockJS
74
- * client.webSocketFactory= function () {
75
- * return new SockJS("http://broker.329broker.com/stomp");
76
- * };
79
+ * client.webSocketFactory = function () {
80
+ * return new WebSocket("ws://my-custom-websocket-endpoint");
81
+ * };
82
+ *
83
+ * // Typical usage with SockJS
84
+ * client.webSocketFactory= function () {
85
+ * return new SockJS("http://broker.329broker.com/stomp");
86
+ * };
77
87
  * ```
88
+ *
89
+ * Note:
90
+ * - If both [Client#brokerURL]{@link Client#brokerURL} and this property are set, the factory will be used.
91
+ * - Refer to [Polyfills Guide]{@link https://stomp-js.github.io/guide/stompjs/rx-stomp/ng2-stompjs/pollyfils-for-stompjs-v5.html}
92
+ * when running in environments without native WebSocket support.
78
93
  */
79
94
  public webSocketFactory: (() => IStompSocket) | undefined;
80
95
 
81
96
  /**
82
- * Will retry if Stomp connection is not established in specified milliseconds.
83
- * Default 0, which switches off automatic reconnection.
97
+ * Timeout for establishing STOMP connection, in milliseconds.
98
+ *
99
+ * If the connection is not established within this period, the attempt will fail.
100
+ * The default is `0`, meaning no timeout is set for connection attempts.
101
+ *
102
+ * Example:
103
+ * ```javascript
104
+ * client.connectionTimeout = 5000; // Fail connection if not established in 5 seconds
105
+ * ```
84
106
  */
85
107
  public connectionTimeout: number = 0;
86
108
 
87
- // As per https://stackoverflow.com/questions/45802988/typescript-use-correct-version-of-settimeout-node-vs-window/56239226#56239226
88
- private _connectionWatcher: ReturnType<typeof setTimeout> | undefined; // Timer
109
+ // Internal timer for handling connection timeout, if set.
110
+ // See https://stackoverflow.com/questions/45802988/typescript-use-correct-version-of-settimeout-node-vs-window/56239226#56239226
111
+ private _connectionWatcher: ReturnType<typeof setTimeout> | undefined;
89
112
 
90
113
  /**
91
- * automatically reconnect with delay in milliseconds, set to 0 to disable.
114
+ * Delay (in milliseconds) between reconnection attempts if the connection drops.
115
+ *
116
+ * Set to `0` to disable automatic reconnections. The default value is `5000` ms (5 seconds).
117
+ *
118
+ * Example:
119
+ * ```javascript
120
+ * client.reconnectDelay = 3000; // Attempt reconnection every 3 seconds
121
+ * client.reconnectDelay = 0; // Disable automatic reconnection
122
+ * ```
92
123
  */
93
124
  public reconnectDelay: number = 5000;
94
125
 
95
126
  /**
96
- * tracking the time to the next reconnection. Initialized to [Client#reconnectDelay]{@link Client#reconnectDelay}'s value and it may
97
- * change depending on the [Client#reconnectTimeMode]{@link Client#reconnectTimeMode} setting
127
+ * The next reconnection delay, used internally.
128
+ * Initialized to the value of [Client#reconnectDelay]{@link Client#reconnectDelay}, and it may
129
+ * dynamically change based on [Client#reconnectTimeMode]{@link Client#reconnectTimeMode}.
98
130
  */
99
131
  private _nextReconnectDelay: number = 0;
100
132
 
101
133
  /**
102
- * Maximum time to wait between reconnects, in milliseconds. Defaults to 15 minutes.
103
- * Only relevant when reconnectTimeMode not LINEAR (e.g. EXPONENTIAL).
104
- * Set to 0 to wait indefinitely.
134
+ * Maximum delay (in milliseconds) between reconnection attempts when using exponential backoff.
135
+ *
136
+ * Default is 15 minutes (`15 * 60 * 1000` milliseconds). If `0`, there will be no upper limit.
137
+ *
138
+ * Example:
139
+ * ```javascript
140
+ * client.maxReconnectDelay = 10000; // Maximum wait time is 10 seconds
141
+ * ```
105
142
  */
106
- public maxReconnectDelay: number = 15 * 60 * 1000; // 15 minutes in ms
143
+ public maxReconnectDelay: number = 15 * 60 * 1000;
107
144
 
108
145
  /**
109
- * Reconnection wait time mode, either linear (default) or exponential.
110
- * Note: See [Client#maxReconnectDelay]{@link Client#maxReconnectDelay} for setting the maximum delay when exponential
146
+ * Mode for determining the time interval between reconnection attempts.
147
+ *
148
+ * Available modes:
149
+ * - `ReconnectionTimeMode.LINEAR` (default): Fixed delays between reconnection attempts.
150
+ * - `ReconnectionTimeMode.EXPONENTIAL`: Delay doubles after each attempt, capped by [maxReconnectDelay]{@link Client#maxReconnectDelay}.
151
+ *
152
+ * Example:
153
+ * ```javascript
154
+ * client.reconnectTimeMode = ReconnectionTimeMode.EXPONENTIAL;
155
+ * client.reconnectDelay = 200; // Initial delay of 200 ms, doubles with each attempt
156
+ * client.maxReconnectDelay = 2 * 60 * 1000; // Cap delay at 10 minutes
157
+ * ```
111
158
  */
112
159
  public reconnectTimeMode: ReconnectionTimeMode = ReconnectionTimeMode.LINEAR;
113
160
 
114
161
  /**
115
- * Incoming heartbeat interval in milliseconds. Set to 0 to disable.
162
+ * Interval (in milliseconds) for receiving heartbeat signals from the server.
163
+ *
164
+ * Specifies the expected frequency of heartbeats sent by the server. Set to `0` to disable.
165
+ *
166
+ * Example:
167
+ * ```javascript
168
+ * client.heartbeatIncoming = 10000; // Expect a heartbeat every 10 seconds
169
+ * ```
116
170
  */
117
171
  public heartbeatIncoming: number = 10000;
118
172
 
119
173
  /**
120
- * Outgoing heartbeat interval in milliseconds. Set to 0 to disable.
174
+ * Multiplier for adjusting tolerance when processing heartbeat signals.
175
+ *
176
+ * Tolerance level is calculated using the multiplier:
177
+ * `tolerance = heartbeatIncoming * heartbeatToleranceMultiplier`.
178
+ * This helps account for delays in network communication or variations in timings.
179
+ *
180
+ * Default value is `2`.
181
+ *
182
+ * Example:
183
+ * ```javascript
184
+ * client.heartbeatToleranceMultiplier = 2.5; // Tolerates longer delays
185
+ * ```
186
+ */
187
+ public heartbeatToleranceMultiplier: number = 2;
188
+
189
+ /**
190
+ * Interval (in milliseconds) for sending heartbeat signals to the server.
191
+ *
192
+ * Specifies how frequently heartbeats should be sent to the server. Set to `0` to disable.
193
+ *
194
+ * Example:
195
+ * ```javascript
196
+ * client.heartbeatOutgoing = 5000; // Send a heartbeat every 5 seconds
197
+ * ```
121
198
  */
122
199
  public heartbeatOutgoing: number = 10000;
123
200
 
124
201
  /**
125
- * Outgoing heartbeat strategy.
126
- * See https://github.com/stomp-js/stompjs/pull/579
127
- *
128
- * Can be worker or interval strategy, but will always use `interval`
129
- * if web workers are unavailable, for example, in a non-browser environment.
130
- *
131
- * Using Web Workers may work better on long-running pages
132
- * and mobile apps, as the browser may suspend Timers in the main page.
133
- * Try the `Worker` mode if you discover disconnects when the browser tab is in the background.
134
- *
135
- * When used in a JS environment, use 'worker' or 'interval' as valid values.
136
- *
137
- * Defaults to `interval` strategy.
202
+ * Strategy for sending outgoing heartbeats.
203
+ *
204
+ * Options:
205
+ * - `TickerStrategy.Worker`: Uses Web Workers for sending heartbeats (recommended for long-running or background sessions).
206
+ * - `TickerStrategy.Interval`: Uses standard JavaScript `setInterval` (default).
207
+ *
208
+ * Note:
209
+ * - If Web Workers are unavailable (e.g., in Node.js), the `Interval` strategy is used automatically.
210
+ * - Web Workers are preferable in browsers for reducing disconnects when tabs are in the background.
211
+ *
212
+ * Example:
213
+ * ```javascript
214
+ * client.heartbeatStrategy = TickerStrategy.Worker;
215
+ * ```
138
216
  */
139
217
  public heartbeatStrategy: TickerStrategy = TickerStrategy.Interval;
140
218
 
141
219
  /**
142
- * This switches on a non-standard behavior while sending WebSocket packets.
143
- * It splits larger (text) packets into chunks of [maxWebSocketChunkSize]{@link Client#maxWebSocketChunkSize}.
144
- * Only Java Spring brokers seem to support this mode.
220
+ * Enables splitting of large text WebSocket frames into smaller chunks.
145
221
  *
146
- * WebSockets, by itself, split large (text) packets,
147
- * so it is not needed with a truly compliant STOMP/WebSocket broker.
148
- * Setting it for such a broker will cause large messages to fail.
222
+ * This setting is enabled for brokers that support only chunked messages (e.g., Java Spring-based brokers).
223
+ * Default is `false`.
149
224
  *
150
- * `false` by default.
225
+ * Warning:
226
+ * - Should not be used with WebSocket-compliant brokers, as chunking may cause large message failures.
227
+ * - Binary WebSocket frames are never split.
151
228
  *
152
- * Binary frames are never split.
229
+ * Example:
230
+ * ```javascript
231
+ * client.splitLargeFrames = true;
232
+ * client.maxWebSocketChunkSize = 4096; // Allow chunks of 4 KB
233
+ * ```
153
234
  */
154
235
  public splitLargeFrames: boolean = false;
155
236
 
156
237
  /**
157
- * See [splitLargeFrames]{@link Client#splitLargeFrames}.
158
- * This has no effect if [splitLargeFrames]{@link Client#splitLargeFrames} is `false`.
238
+ * Maximum size (in bytes) for individual WebSocket chunks if [splitLargeFrames]{@link Client#splitLargeFrames} is enabled.
239
+ *
240
+ * Default is 8 KB (`8 * 1024` bytes). This value has no effect if [splitLargeFrames]{@link Client#splitLargeFrames} is `false`.
159
241
  */
160
242
  public maxWebSocketChunkSize: number = 8 * 1024;
161
243
 
162
244
  /**
163
- * Usually the
164
- * [type of WebSocket frame]{@link https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send#Parameters}
165
- * is automatically decided by type of the payload.
166
- * Default is `false`, which should work with all compliant brokers.
245
+ * Forces all WebSocket frames to use binary transport, irrespective of payload type.
167
246
  *
168
- * Set this flag to force binary frames.
247
+ * Default behavior determines frame type based on payload (e.g., binary data for ArrayBuffers).
248
+ *
249
+ * Example:
250
+ * ```javascript
251
+ * client.forceBinaryWSFrames = true;
252
+ * ```
169
253
  */
170
254
  public forceBinaryWSFrames: boolean = false;
171
255
 
172
256
  /**
173
- * A bug in ReactNative chops a string on occurrence of a NULL.
174
- * See issue [https://github.com/stomp-js/stompjs/issues/89]{@link https://github.com/stomp-js/stompjs/issues/89}.
175
- * This makes incoming WebSocket messages invalid STOMP packets.
176
- * Setting this flag attempts to reverse the damage by appending a NULL.
177
- * If the broker splits a large message into multiple WebSocket messages,
178
- * this flag will cause data loss and abnormal termination of connection.
257
+ * Workaround for a React Native WebSocket bug, where messages containing `NULL` are chopped.
179
258
  *
180
- * This is not an ideal solution, but a stop gap until the underlying issue is fixed at ReactNative library.
259
+ * Enabling this appends a `NULL` character to incoming frames to ensure they remain valid STOMP packets.
260
+ *
261
+ * Warning:
262
+ * - For brokers that split large messages, this may cause data loss or connection termination.
263
+ *
264
+ * Example:
265
+ * ```javascript
266
+ * client.appendMissingNULLonIncoming = true;
267
+ * ```
181
268
  */
182
269
  public appendMissingNULLonIncoming: boolean = false;
183
270
 
184
271
  /**
185
- * Underlying WebSocket instance, READONLY.
272
+ * Provides access to the underlying WebSocket instance.
273
+ * This property is **read-only**.
274
+ *
275
+ * Example:
276
+ * ```javascript
277
+ * const webSocket = client.webSocket;
278
+ * if (webSocket) {
279
+ * console.log('WebSocket is connected:', webSocket.readyState === WebSocket.OPEN);
280
+ * }
281
+ * ```
282
+ *
283
+ * **Caution:**
284
+ * Directly interacting with the WebSocket instance (e.g., sending or receiving frames)
285
+ * can interfere with the proper functioning of this library. Such actions may cause
286
+ * unexpected behavior, disconnections, or invalid state in the library's internal mechanisms.
287
+ *
288
+ * Instead, use the library's provided methods to manage STOMP communication.
289
+ *
290
+ * @returns The WebSocket instance used by the STOMP handler, or `undefined` if not connected.
186
291
  */
187
292
  get webSocket(): IStompSocket | undefined {
188
293
  return this._stompHandler?._webSocket;
189
294
  }
190
295
 
191
296
  /**
192
- * Connection headers, important keys - `login`, `passcode`, `host`.
193
- * Though STOMP 1.2 standard marks these keys to be present, check your broker documentation for
194
- * details specific to your broker.
297
+ * Connection headers to be sent during the connection handshake.
298
+ *
299
+ * Keys like `login`, `passcode`, and `host` are commonly expected for most brokers.
300
+ * Although STOMP 1.2 specifies these keys as mandatory, consult your broker's documentation
301
+ * for additional requirements or alternative header usage.
302
+ *
303
+ * Example:
304
+ * ```javascript
305
+ * client.connectHeaders = {
306
+ * login: 'my-username',
307
+ * passcode: 'my-password',
308
+ * host: 'my-vhost'
309
+ * };
310
+ * ```
195
311
  */
196
312
  public connectHeaders: StompHeaders;
197
313
 
198
314
  /**
199
- * Disconnection headers.
315
+ * Allows customization of the disconnection headers.
316
+ *
317
+ * Any changes made during an active session will also be applied immediately.
318
+ *
319
+ * Example:
320
+ * ```javascript
321
+ * client.disconnectHeaders = {
322
+ * receipt: 'custom-receipt-id'
323
+ * };
324
+ * ```
200
325
  */
201
326
  get disconnectHeaders(): StompHeaders {
202
327
  return this._disconnectHeaders;
@@ -211,139 +336,272 @@ export class Client {
211
336
  private _disconnectHeaders: StompHeaders;
212
337
 
213
338
  /**
214
- * This function will be called for any unhandled messages.
215
- * It is useful for receiving messages sent to RabbitMQ temporary queues.
339
+ * Callback invoked for any unhandled messages received from the broker.
216
340
  *
217
- * It can also get invoked with stray messages while the server is processing
218
- * a request to [Client#unsubscribe]{@link Client#unsubscribe}
219
- * from an endpoint.
341
+ * This is particularly useful for handling messages sent to RabbitMQ temporary queues
342
+ * or other queues where no explicit subscription exists. It can also be triggered
343
+ * by stray messages received while a subscription is being unsubscribed.
220
344
  *
221
- * The actual {@link IMessage} will be passed as parameter to the callback.
345
+ * Usage:
346
+ * ```javascript
347
+ * client.onUnhandledMessage = (message) => {
348
+ * console.log('Unhandled message:', message);
349
+ * };
350
+ * ```
351
+ *
352
+ * @param message The actual {@link IMessage} received.
222
353
  */
223
354
  public onUnhandledMessage: messageCallbackType;
224
355
 
225
356
  /**
226
- * STOMP brokers can be requested to notify when an operation is actually completed.
227
- * Prefer using [Client#watchForReceipt]{@link Client#watchForReceipt}. See
228
- * [Client#watchForReceipt]{@link Client#watchForReceipt} for examples.
357
+ * Callback invoked when the broker sends a receipt indicating the completion
358
+ * of an operation. Receipts are typically requested using the
359
+ * [Client#watchForReceipt]{@link Client#watchForReceipt} function.
229
360
  *
230
- * The actual {@link IFrame} will be passed as parameter to the callback.
361
+ * Usage Example:
362
+ * See [Client#watchForReceipt]{@link Client#watchForReceipt}.
363
+ *
364
+ * @param frame The actual {@link IFrame} received from the broker.
231
365
  */
232
366
  public onUnhandledReceipt: frameCallbackType;
233
367
 
234
368
  /**
235
- * Will be invoked if {@link IFrame} of an unknown type is received from the STOMP broker.
369
+ * Callback invoked when a frame of an unknown or unexpected type is received
370
+ * from the broker.
371
+ *
372
+ * This is intended as a fallback for handling unexpected or unsupported frames
373
+ * sent by the broker.
374
+ *
375
+ * Usage:
376
+ * ```javascript
377
+ * client.onUnhandledFrame = (frame) => {
378
+ * console.warn('Unhandled frame received:', frame);
379
+ * };
380
+ * ```
236
381
  *
237
- * The actual {@link IFrame} will be passed as parameter to the callback.
382
+ * @param frame The actual {@link IFrame} received from the broker.
238
383
  */
239
384
  public onUnhandledFrame: frameCallbackType;
240
385
 
241
386
  /**
242
- * `true` if there is an active connection to STOMP Broker
387
+ * Callback invoked when a heartbeat message is received from the STOMP broker.
388
+ *
389
+ * Heartbeats ensure that the connection remains active and responsive. This callback
390
+ * is executed on every received heartbeat. It is useful for monitoring connection health
391
+ * or logging heartbeat activity.
392
+ *
393
+ * **Note**: The library handles heartbeats internally to maintain and verify connection status.
394
+ * Implementing this callback is optional and primarily for custom monitoring or debugging.
395
+ *
396
+ * Usage:
397
+ * ```javascript
398
+ * client.onHeartbeatReceived = () => {
399
+ * console.log('Heartbeat received');
400
+ * };
401
+ * ```
402
+ */
403
+ public onHeartbeatReceived: emptyCallbackType;
404
+
405
+ /**
406
+ * Callback invoked when no heartbeat is received from the broker within
407
+ * the acceptable interval, indicating a potential communication issue or connection failure.
408
+ *
409
+ * This callback is triggered when the heartbeat interval defined by `heartbeatIncoming`
410
+ * elapses without a received heartbeat.
411
+ *
412
+ * **Note**: The library handles this condition internally and takes appropriate
413
+ * actions, such as marking the connection as failed. This callback is available
414
+ * for implementing custom recovery strategies or additional notifications.
415
+ *
416
+ * Usage:
417
+ * ```javascript
418
+ * client.onHeartbeatLost = () => {
419
+ * console.error('Lost connection to the broker');
420
+ * };
421
+ * ```
422
+ */
423
+ public onHeartbeatLost: emptyCallbackType;
424
+
425
+ /**
426
+ * Indicates whether there is an active connection to the STOMP broker.
427
+ *
428
+ * Usage:
429
+ * ```javascript
430
+ * if (client.connected) {
431
+ * console.log('Client is connected to the broker.');
432
+ * } else {
433
+ * console.log('No connection to the broker.');
434
+ * }
435
+ * ```
436
+ *
437
+ * @returns `true` if the client is currently connected, `false` otherwise.
243
438
  */
244
439
  get connected(): boolean {
245
440
  return !!this._stompHandler && this._stompHandler.connected;
246
441
  }
247
442
 
248
443
  /**
249
- * Callback, invoked on before a connection to the STOMP broker.
444
+ * Callback executed before initiating a connection to the STOMP broker.
445
+ *
446
+ * This callback allows users to modify connection options dynamically,
447
+ * such as updating credentials or connection parameters, before the connection is made.
250
448
  *
251
- * You can change options on the client, which will impact the immediate connecting.
252
- * It is valid to call [Client#decativate]{@link Client#deactivate} in this callback.
449
+ * As of version 5.1, this callback supports `async/await`, enabling seamless integration
450
+ * with asynchronous operations, such as fetching tokens or credentials.
253
451
  *
254
- * As of version 5.1, this callback can be
255
- * [async](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)
256
- * (i.e., it can return a
257
- * [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)).
258
- * In that case, connect will be called only after the Promise is resolved.
259
- * This can be used to reliably fetch credentials, access token etc. from some other service
260
- * in an asynchronous way.
452
+ * Example:
453
+ * ```javascript
454
+ * client.beforeConnect = async () => {
455
+ * const token = await fetchToken();
456
+ * client.connectHeaders = { Authorization: `Bearer ${token}` };
457
+ * };
458
+ * ```
261
459
  */
262
460
  public beforeConnect: (client: Client) => void | Promise<void>;
263
461
 
264
462
  /**
265
- * Callback, invoked on every successful connection to the STOMP broker.
463
+ * Callback executed upon every successful connection to the STOMP broker.
464
+ *
465
+ * This callback is invoked after the connection is established and the CONNECTED frame
466
+ * is received from the broker. It provides access to the broker's response frame,
467
+ * allowing users to parse its headers or other data.
266
468
  *
267
- * The actual {@link IFrame} will be passed as parameter to the callback.
268
- * Sometimes clients will like to use headers from this frame.
469
+ * Example:
470
+ * ```javascript
471
+ * client.onConnect = (frame) => {
472
+ * console.log('Connected to broker, session ID:', frame.headers['session']);
473
+ * };
474
+ * ```
269
475
  */
270
476
  public onConnect: frameCallbackType;
271
477
 
272
478
  /**
273
- * Callback, invoked on every successful disconnection from the STOMP broker. It will not be invoked if
274
- * the STOMP broker disconnected due to an error.
479
+ * Callback executed upon successful disconnection from the STOMP broker.
275
480
  *
276
- * The actual Receipt {@link IFrame} acknowledging the DISCONNECT will be passed as parameter to the callback.
481
+ * The callback is invoked when the DISCONNECT receipt is received from the broker.
482
+ * Note that due to the design of the STOMP protocol or communication interrupts, the
483
+ * DISCONNECT receipt may not always be received. For handling such cases, use
484
+ * [Client#onWebSocketClose]{@link Client#onWebSocketClose}.
277
485
  *
278
- * The way STOMP protocol is designed, the connection may close/terminate without the client
279
- * receiving the Receipt {@link IFrame} acknowledging the DISCONNECT.
280
- * You might find [Client#onWebSocketClose]{@link Client#onWebSocketClose} more appropriate to watch
281
- * STOMP broker disconnects.
486
+ * Example:
487
+ * ```javascript
488
+ * client.onDisconnect = (frame) => {
489
+ * console.log('Disconnected successfully');
490
+ * };
491
+ * ```
282
492
  */
283
493
  public onDisconnect: frameCallbackType;
284
494
 
285
495
  /**
286
- * Callback, invoked on an ERROR frame received from the STOMP Broker.
287
- * A compliant STOMP Broker will close the connection after this type of frame.
288
- * Please check broker specific documentation for exact behavior.
496
+ * Callback executed when an ERROR frame is received from the STOMP broker.
497
+ *
498
+ * Receiving an ERROR frame typically indicates a problem with the subscription,
499
+ * message format, or protocol violation. The broker will usually close the connection
500
+ * after sending an ERROR frame.
289
501
  *
290
- * The actual {@link IFrame} will be passed as parameter to the callback.
502
+ * Example:
503
+ * ```javascript
504
+ * client.onStompError = (frame) => {
505
+ * console.error('Broker reported an error:', frame.body);
506
+ * };
507
+ * ```
291
508
  */
292
509
  public onStompError: frameCallbackType;
293
510
 
294
511
  /**
295
- * Callback, invoked when underlying WebSocket is closed.
512
+ * Callback executed when the underlying WebSocket is closed.
513
+ *
514
+ * This can occur due to various reasons, such as network interruptions or broker shutdown.
515
+ * The callback provides the WebSocket [CloseEvent]{@link https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent},
516
+ * which contains details about the closure.
296
517
  *
297
- * Actual [CloseEvent]{@link https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent}
298
- * is passed as parameter to the callback.
518
+ * Example:
519
+ * ```javascript
520
+ * client.onWebSocketClose = (event) => {
521
+ * console.log('WebSocket closed. Code:', event.code);
522
+ * };
523
+ * ```
299
524
  */
300
525
  public onWebSocketClose: closeEventCallbackType;
301
526
 
302
527
  /**
303
- * Callback, invoked when underlying WebSocket raises an error.
528
+ * Callback executed when the underlying WebSocket raises an error.
304
529
  *
305
- * Actual [Event]{@link https://developer.mozilla.org/en-US/docs/Web/API/Event}
306
- * is passed as parameter to the callback.
530
+ * This callback provides an [Event]{@link https://developer.mozilla.org/en-US/docs/Web/API/Event}
531
+ * representing the error raised by the WebSocket.
532
+ *
533
+ * Example:
534
+ * ```javascript
535
+ * client.onWebSocketError = (event) => {
536
+ * console.error('WebSocket error:', event);
537
+ * };
538
+ * ```
307
539
  */
308
540
  public onWebSocketError: wsErrorCallbackType;
309
541
 
310
542
  /**
311
- * Set it to log the actual raw communication with the broker.
312
- * When unset, it logs headers of the parsed frames.
543
+ * Enable or disable logging of the raw communication with the broker.
313
544
  *
314
- * Changes effect from the next broker reconnect.
545
+ * When enabled, it logs the raw frames exchanged with the broker. If disabled,
546
+ * only the headers of the parsed frames will be logged.
315
547
  *
316
- * **Caution: this assumes that frames only have valid UTF8 strings.**
548
+ * **Caution**: Raw communication frames must contain valid UTF-8 strings,
549
+ * as any non-compliant data can cause errors in the logging process.
550
+ *
551
+ * Changes to this setting will take effect during the next broker reconnect.
552
+ *
553
+ * Example:
554
+ * ```javascript
555
+ * client.logRawCommunication = true; // Enable logging raw communication
556
+ * ```
317
557
  */
318
558
  public logRawCommunication: boolean;
319
559
 
320
560
  /**
321
- * By default, debug messages are discarded. To log to `console` following can be used:
561
+ * Set a custom debug function to capture debug messages.
322
562
  *
563
+ * By default, debug messages are discarded. To log messages to the console, you can use:
323
564
  * ```javascript
324
- * client.debug = function(str) {
325
- * console.log(str);
326
- * };
565
+ * client.debug = (str) => {
566
+ * console.log(str);
567
+ * };
327
568
  * ```
328
569
  *
329
- * Currently this method does not support levels of log. Be aware that the
330
- * output can be quite verbose
331
- * and may contain sensitive information (like passwords, tokens etc.).
570
+ * **Note**: This method does not support configurable log levels, and the output can be
571
+ * verbose. Be cautious as debug messages may contain sensitive information, such as
572
+ * credentials or tokens.
332
573
  */
333
574
  public debug: debugFnType;
334
575
 
335
576
  /**
336
- * Browsers do not immediately close WebSockets when `.close` is issued.
337
- * This may cause reconnection to take a significantly long time in case
338
- * of some types of failures.
339
- * In case of incoming heartbeat failure, this experimental flag instructs
340
- * the library to discard the socket immediately
341
- * (even before it is actually closed).
577
+ * Instruct the library to immediately terminate the socket on communication failures, even
578
+ * before the WebSocket is completely closed.
579
+ *
580
+ * This is particularly useful in browser environments where WebSocket closure may get delayed,
581
+ * causing prolonged reconnection intervals under certain failure conditions.
582
+ *
583
+ *
584
+ * Example:
585
+ * ```javascript
586
+ * client.discardWebsocketOnCommFailure = true; // Enable aggressive closing of WebSocket
587
+ * ```
588
+ *
589
+ * Default value: `false`.
342
590
  */
343
591
  public discardWebsocketOnCommFailure: boolean = false;
344
592
 
345
593
  /**
346
- * version of STOMP protocol negotiated with the server, READONLY
594
+ * The version of the STOMP protocol negotiated with the server during connection.
595
+ *
596
+ * This is a **read-only** property and reflects the negotiated protocol version after
597
+ * a successful connection.
598
+ *
599
+ * Example:
600
+ * ```javascript
601
+ * console.log('Connected STOMP version:', client.connectedVersion);
602
+ * ```
603
+ *
604
+ * @returns The negotiated STOMP protocol version or `undefined` if not connected.
347
605
  */
348
606
  get connectedVersion(): string | undefined {
349
607
  return this._stompHandler ? this._stompHandler.connectedVersion : undefined;
@@ -352,16 +610,38 @@ export class Client {
352
610
  private _stompHandler: StompHandler | undefined;
353
611
 
354
612
  /**
355
- * if the client is active (connected or going to reconnect)
613
+ * Indicates whether the client is currently active.
614
+ *
615
+ * A client is considered active if it is connected or actively attempting to reconnect.
616
+ *
617
+ * Example:
618
+ * ```javascript
619
+ * if (client.active) {
620
+ * console.log('The client is active.');
621
+ * } else {
622
+ * console.log('The client is inactive.');
623
+ * }
624
+ * ```
625
+ *
626
+ * @returns `true` if the client is active, otherwise `false`.
356
627
  */
357
628
  get active(): boolean {
358
629
  return this.state === ActivationState.ACTIVE;
359
630
  }
360
631
 
361
632
  /**
362
- * It will be called on state change.
633
+ * Callback invoked whenever the client's state changes.
634
+ *
635
+ * This callback can be used to monitor transitions between various states, such as `ACTIVE`,
636
+ * `INACTIVE`, or `DEACTIVATING`. Note that in some scenarios, the client may transition
637
+ * directly from `ACTIVE` to `INACTIVE` without entering the `DEACTIVATING` state.
363
638
  *
364
- * When deactivating, it may go from ACTIVE to INACTIVE without entering DEACTIVATING.
639
+ * Example:
640
+ * ```javascript
641
+ * client.onChangeState = (state) => {
642
+ * console.log(`Client state changed to: ${state}`);
643
+ * };
644
+ * ```
365
645
  */
366
646
  public onChangeState: (state: ActivationState) => void;
367
647
 
@@ -371,17 +651,35 @@ export class Client {
371
651
  }
372
652
 
373
653
  /**
374
- * Activation state.
654
+ * Current activation state of the client.
655
+ *
656
+ * Possible states:
657
+ * - `ActivationState.ACTIVE`: Client is connected or actively attempting to connect.
658
+ * - `ActivationState.INACTIVE`: Client is disconnected and not attempting to reconnect.
659
+ * - `ActivationState.DEACTIVATING`: Client is in the process of disconnecting.
375
660
  *
376
- * It will usually be ACTIVE or INACTIVE.
377
- * When deactivating, it may go from ACTIVE to INACTIVE without entering DEACTIVATING.
661
+ * Note: The client may transition directly from `ACTIVE` to `INACTIVE` without entering
662
+ * the `DEACTIVATING` state.
378
663
  */
379
664
  public state: ActivationState = ActivationState.INACTIVE;
380
665
 
381
666
  private _reconnector: any;
382
667
 
383
668
  /**
384
- * Create an instance.
669
+ * Constructs a new STOMP client instance.
670
+ *
671
+ * The constructor initializes default values and sets up no-op callbacks for all events.
672
+ * Configuration can be passed during construction, or updated later using `configure`.
673
+ *
674
+ * Example:
675
+ * ```javascript
676
+ * const client = new Client({
677
+ * brokerURL: 'wss://broker.example.com',
678
+ * reconnectDelay: 5000
679
+ * });
680
+ * ```
681
+ *
682
+ * @param conf Optional configuration object to initialize the client with.
385
683
  */
386
684
  constructor(conf: StompConfig = {}) {
387
685
  // No op callbacks
@@ -393,6 +691,8 @@ export class Client {
393
691
  this.onUnhandledMessage = noOp;
394
692
  this.onUnhandledReceipt = noOp;
395
693
  this.onUnhandledFrame = noOp;
694
+ this.onHeartbeatReceived = noOp;
695
+ this.onHeartbeatLost = noOp;
396
696
  this.onStompError = noOp;
397
697
  this.onWebSocketClose = noOp;
398
698
  this.onWebSocketError = noOp;
@@ -408,8 +708,24 @@ export class Client {
408
708
  }
409
709
 
410
710
  /**
411
- * Update configuration.
711
+ * Updates the client's configuration.
712
+ *
713
+ * All properties in the provided configuration object will override the current settings.
714
+ *
715
+ * Additionally, a warning is logged if `maxReconnectDelay` is configured to a
716
+ * value lower than `reconnectDelay`, and `maxReconnectDelay` is adjusted to match `reconnectDelay`.
717
+ *
718
+ * Example:
719
+ * ```javascript
720
+ * client.configure({
721
+ * reconnectDelay: 3000,
722
+ * maxReconnectDelay: 10000
723
+ * });
724
+ * ```
725
+ *
726
+ * @param conf Configuration object containing the new settings.
412
727
  */
728
+
413
729
  public configure(conf: StompConfig): void {
414
730
  // bulk assign all properties to this
415
731
  (Object as any).assign(this, conf);
@@ -427,12 +743,20 @@ export class Client {
427
743
  }
428
744
 
429
745
  /**
430
- * Initiate the connection with the broker.
431
- * If the connection breaks, as per [Client#reconnectDelay]{@link Client#reconnectDelay},
432
- * it will keep trying to reconnect. If the [Client#reconnectTimeMode]{@link Client#reconnectTimeMode}
433
- * is set to EXPONENTIAL it will increase the wait time exponentially
746
+ * Activates the client, initiating a connection to the STOMP broker.
747
+ *
748
+ * On activation, the client attempts to connect and sets its state to `ACTIVE`. If the connection
749
+ * is lost, it will automatically retry based on `reconnectDelay` or `maxReconnectDelay`. If
750
+ * `reconnectTimeMode` is set to `EXPONENTIAL`, the reconnect delay increases exponentially.
751
+ *
752
+ * To stop reconnection attempts and disconnect, call [Client#deactivate]{@link Client#deactivate}.
753
+ *
754
+ * Example:
755
+ * ```javascript
756
+ * client.activate(); // Connect to the broker
757
+ * ```
434
758
  *
435
- * Call [Client#deactivate]{@link Client#deactivate} to disconnect and stop reconnection attempts.
759
+ * If the client is currently `DEACTIVATING`, connection is delayed until the deactivation process completes.
436
760
  */
437
761
  public activate(): void {
438
762
  const _activate = () => {
@@ -505,6 +829,7 @@ export class Client {
505
829
  connectHeaders: this.connectHeaders,
506
830
  disconnectHeaders: this._disconnectHeaders,
507
831
  heartbeatIncoming: this.heartbeatIncoming,
832
+ heartbeatGracePeriods: this.heartbeatToleranceMultiplier,
508
833
  heartbeatOutgoing: this.heartbeatOutgoing,
509
834
  heartbeatStrategy: this.heartbeatStrategy,
510
835
  splitLargeFrames: this.splitLargeFrames,
@@ -564,6 +889,12 @@ export class Client {
564
889
  onUnhandledFrame: frame => {
565
890
  this.onUnhandledFrame(frame);
566
891
  },
892
+ onHeartbeatReceived: () => {
893
+ this.onHeartbeatReceived();
894
+ },
895
+ onHeartbeatLost: () => {
896
+ this.onHeartbeatLost();
897
+ },
567
898
  });
568
899
 
569
900
  this._stompHandler.start();
@@ -611,27 +942,36 @@ export class Client {
611
942
  }
612
943
 
613
944
  /**
614
- * Disconnect if connected and stop auto reconnect loop.
615
- * Appropriate callbacks will be invoked if there is an underlying STOMP connection.
945
+ * Disconnects the client and stops the automatic reconnection loop.
616
946
  *
617
- * This call is async. It will resolve immediately if there is no underlying active websocket,
618
- * otherwise, it will resolve after the underlying websocket is properly disposed of.
947
+ * If there is an active STOMP connection at the time of invocation, the appropriate callbacks
948
+ * will be triggered during the shutdown sequence. Once deactivated, the client will enter the
949
+ * `INACTIVE` state, and no further reconnection attempts will be made.
619
950
  *
620
- * It is not an error to invoke this method more than once.
621
- * Each of those would resolve on completion of deactivation.
951
+ * **Behavior**:
952
+ * - If there is no active WebSocket connection, this method resolves immediately.
953
+ * - If there is an active connection, the method waits for the underlying WebSocket
954
+ * to properly close before resolving.
955
+ * - Multiple calls to this method are safe. Each invocation resolves upon completion.
956
+ * - To reactivate, call [Client#activate]{@link Client#activate}.
622
957
  *
623
- * To reactivate, you can call [Client#activate]{@link Client#activate}.
958
+ * **Experimental Option:**
959
+ * - By specifying the `force: true` option, the WebSocket connection is discarded immediately,
960
+ * bypassing both the STOMP and WebSocket shutdown sequences.
961
+ * - **Caution:** Using `force: true` may leave the WebSocket in an inconsistent state,
962
+ * and brokers may not immediately detect the termination.
624
963
  *
625
- * Experimental: pass `force: true` to immediately discard the underlying connection.
626
- * This mode will skip both the STOMP and the Websocket shutdown sequences.
627
- * In some cases, browsers take a long time in the Websocket shutdown
628
- * if the underlying connection had gone stale.
629
- * Using this mode can speed up.
630
- * When this mode is used, the actual Websocket may linger for a while
631
- * and the broker may not realize that the connection is no longer in use.
964
+ * Example:
965
+ * ```javascript
966
+ * // Graceful disconnect
967
+ * await client.deactivate();
632
968
  *
633
- * It is possible to invoke this method initially without the `force` option
634
- * and subsequently, say after a wait, with the `force` option.
969
+ * // Forced disconnect to speed up shutdown when the connection is stale
970
+ * await client.deactivate({ force: true });
971
+ * ```
972
+ *
973
+ * @param options Configuration options for deactivation. Use `force: true` for immediate shutdown.
974
+ * @returns A Promise that resolves when the deactivation process completes.
635
975
  */
636
976
  public async deactivate(options: { force?: boolean } = {}): Promise<void> {
637
977
  const force: boolean = options.force || false;
@@ -684,10 +1024,18 @@ export class Client {
684
1024
  }
685
1025
 
686
1026
  /**
687
- * Force disconnect if there is an active connection by directly closing the underlying WebSocket.
688
- * This is different from a normal disconnect where a DISCONNECT sequence is carried out with the broker.
689
- * After forcing disconnect, automatic reconnect will be attempted.
690
- * To stop further reconnects call [Client#deactivate]{@link Client#deactivate} as well.
1027
+ * Forces a disconnect by directly closing the WebSocket.
1028
+ *
1029
+ * Unlike a normal disconnect, this does not send a DISCONNECT sequence to the broker but
1030
+ * instead closes the WebSocket connection directly. After forcing a disconnect, the client
1031
+ * will automatically attempt to reconnect based on its `reconnectDelay` configuration.
1032
+ *
1033
+ * **Note:** To prevent further reconnect attempts, call [Client#deactivate]{@link Client#deactivate}.
1034
+ *
1035
+ * Example:
1036
+ * ```javascript
1037
+ * client.forceDisconnect();
1038
+ * ```
691
1039
  */
692
1040
  public forceDisconnect() {
693
1041
  if (this._stompHandler) {
@@ -703,39 +1051,38 @@ export class Client {
703
1051
  }
704
1052
 
705
1053
  /**
706
- * Send a message to a named destination. Refer to your STOMP broker documentation for types
707
- * and naming of destinations.
708
- *
709
- * STOMP protocol specifies and suggests some headers and also allows broker-specific headers.
710
- *
711
- * `body` must be String.
712
- * You will need to covert the payload to string in case it is not string (e.g. JSON).
1054
+ * Sends a message to the specified destination on the STOMP broker.
713
1055
  *
714
- * To send a binary message body, use `binaryBody` parameter. It should be a
715
- * [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).
716
- * Sometimes brokers may not support binary frames out of the box.
717
- * Please check your broker documentation.
1056
+ * The `body` must be a `string`. For non-string payloads (e.g., JSON), encode it as a string before sending.
1057
+ * If sending binary data, use the `binaryBody` parameter as a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).
718
1058
  *
719
- * `content-length` header is automatically added to the STOMP Frame sent to the broker.
720
- * Set `skipContentLengthHeader` to indicate that `content-length` header should not be added.
721
- * For binary messages, `content-length` header is always added.
1059
+ * **Content-Length Behavior**:
1060
+ * - For non-binary messages, the `content-length` header is added by default.
1061
+ * - The `content-length` header can be skipped for text frames by setting `skipContentLengthHeader: true` in the parameters.
1062
+ * - For binary messages, the `content-length` header is always included.
722
1063
  *
723
- * Caution: The broker will, most likely, report an error and disconnect
724
- * if the message body has NULL octet(s) and `content-length` header is missing.
1064
+ * **Notes**:
1065
+ * - Ensure that brokers support binary frames before using `binaryBody`.
1066
+ * - Sending messages with NULL octets and missing `content-length` headers can cause brokers to disconnect and throw errors.
725
1067
  *
1068
+ * Example:
726
1069
  * ```javascript
727
- * client.publish({destination: "/queue/test", headers: {priority: 9}, body: "Hello, STOMP"});
728
- *
729
- * // Only destination is mandatory parameter
730
- * client.publish({destination: "/queue/test", body: "Hello, STOMP"});
731
- *
732
- * // Skip content-length header in the frame to the broker
733
- * client.publish({"/queue/test", body: "Hello, STOMP", skipContentLengthHeader: true});
734
- *
735
- * var binaryData = generateBinaryData(); // This need to be of type Uint8Array
736
- * // setting content-type header is not mandatory, however a good practice
737
- * client.publish({destination: '/topic/special', binaryBody: binaryData,
738
- * headers: {'content-type': 'application/octet-stream'}});
1070
+ * // Basic text message
1071
+ * client.publish({ destination: "/queue/test", body: "Hello, STOMP" });
1072
+ *
1073
+ * // Text message with additional headers
1074
+ * client.publish({ destination: "/queue/test", headers: { priority: 9 }, body: "Hello, STOMP" });
1075
+ *
1076
+ * // Skip content-length header
1077
+ * client.publish({ destination: "/queue/test", body: "Hello, STOMP", skipContentLengthHeader: true });
1078
+ *
1079
+ * // Binary message
1080
+ * const binaryData = new Uint8Array([1, 2, 3, 4]);
1081
+ * client.publish({
1082
+ * destination: '/topic/special',
1083
+ * binaryBody: binaryData,
1084
+ * headers: { 'content-type': 'application/octet-stream' }
1085
+ * });
739
1086
  * ```
740
1087
  */
741
1088
  public publish(params: IPublishParams) {
@@ -751,39 +1098,27 @@ export class Client {
751
1098
  }
752
1099
 
753
1100
  /**
754
- * STOMP brokers may carry out operation asynchronously and allow requesting for acknowledgement.
755
- * To request an acknowledgement, a `receipt` header needs to be sent with the actual request.
756
- * The value (say receipt-id) for this header needs to be unique for each use.
757
- * Typically, a sequence, a UUID, a random number or a combination may be used.
758
- *
759
- * A complaint broker will send a RECEIPT frame when an operation has actually been completed.
760
- * The operation needs to be matched based on the value of the receipt-id.
1101
+ * Monitors for a receipt acknowledgment from the broker for specific operations.
761
1102
  *
762
- * This method allows watching for a receipt and invoking the callback
763
- * when the corresponding receipt has been received.
1103
+ * Add a `receipt` header to the operation (like subscribe or publish), and use this method with
1104
+ * the same receipt ID to detect when the broker has acknowledged the operation's completion.
764
1105
  *
765
- * The actual {@link IFrame} will be passed as parameter to the callback.
1106
+ * The callback is invoked with the corresponding {@link IFrame} when the receipt is received.
766
1107
  *
767
1108
  * Example:
768
1109
  * ```javascript
769
- * // Subscribing with acknowledgement
770
- * let receiptId = randomText();
771
- *
772
- * client.watchForReceipt(receiptId, function() {
773
- * // Will be called after server acknowledges
774
- * });
1110
+ * const receiptId = "unique-receipt-id";
775
1111
  *
776
- * client.subscribe(TEST.destination, onMessage, {receipt: receiptId});
1112
+ * client.watchForReceipt(receiptId, (frame) => {
1113
+ * console.log("Operation acknowledged by the broker:", frame);
1114
+ * });
777
1115
  *
778
- *
779
- * // Publishing with acknowledgement
780
- * receiptId = randomText();
781
- *
782
- * client.watchForReceipt(receiptId, function() {
783
- * // Will be called after server acknowledges
784
- * });
785
- * client.publish({destination: TEST.destination, headers: {receipt: receiptId}, body: msg});
1116
+ * // Attach the receipt header to an operation
1117
+ * client.publish({ destination: "/queue/test", headers: { receipt: receiptId }, body: "Hello" });
786
1118
  * ```
1119
+ *
1120
+ * @param receiptId Unique identifier for the receipt.
1121
+ * @param callback Callback function invoked on receiving the RECEIPT frame.
787
1122
  */
788
1123
  public watchForReceipt(receiptId: string, callback: frameCallbackType): void {
789
1124
  this._checkConnection();
@@ -792,28 +1127,33 @@ export class Client {
792
1127
  }
793
1128
 
794
1129
  /**
795
- * Subscribe to a STOMP Broker location. The callback will be invoked for each
796
- * received message with the {@link IMessage} as argument.
1130
+ * Subscribes to a destination on the STOMP broker.
1131
+ *
1132
+ * The callback is triggered for each message received from the subscribed destination. The message
1133
+ * is passed as an {@link IMessage} instance.
797
1134
  *
798
- * Note: The library will generate a unique ID if there is none provided in the headers.
799
- * To use your own ID, pass it using the `headers` argument.
1135
+ * **Subscription ID**:
1136
+ * - If no `id` is provided in `headers`, the library generates a unique subscription ID automatically.
1137
+ * - Provide an explicit `id` in `headers` if you wish to manage the subscription ID manually.
800
1138
  *
1139
+ * Example:
801
1140
  * ```javascript
802
- * callback = function(message) {
803
- * // called when the client receives a STOMP message from the server
804
- * if (message.body) {
805
- * alert("got message with body " + message.body)
806
- * } else {
807
- * alert("got empty message");
808
- * }
809
- * });
1141
+ * const callback = (message) => {
1142
+ * console.log("Received message:", message.body);
1143
+ * };
810
1144
  *
811
- * var subscription = client.subscribe("/queue/test", callback);
1145
+ * // Auto-generated subscription ID
1146
+ * const subscription = client.subscribe("/queue/test", callback);
812
1147
  *
813
- * // Explicit subscription id
814
- * var mySubId = 'my-subscription-id-001';
815
- * var subscription = client.subscribe(destination, callback, { id: mySubId });
1148
+ * // Explicit subscription ID
1149
+ * const mySubId = "my-subscription-id";
1150
+ * const subscription = client.subscribe("/queue/test", callback, { id: mySubId });
816
1151
  * ```
1152
+ *
1153
+ * @param destination Destination to subscribe to.
1154
+ * @param callback Function invoked for each received message.
1155
+ * @param headers Optional headers for subscription, such as `id`.
1156
+ * @returns A {@link StompSubscription} which can be used to manage the subscription.
817
1157
  */
818
1158
  public subscribe(
819
1159
  destination: string,
@@ -826,16 +1166,24 @@ export class Client {
826
1166
  }
827
1167
 
828
1168
  /**
829
- * It is preferable to unsubscribe from a subscription by calling
830
- * `unsubscribe()` directly on {@link StompSubscription} returned by `client.subscribe()`:
1169
+ * Unsubscribes from a subscription on the STOMP broker.
831
1170
  *
1171
+ * Prefer using the `unsubscribe` method directly on the {@link StompSubscription} returned from `subscribe` for cleaner management:
832
1172
  * ```javascript
833
- * var subscription = client.subscribe(destination, onmessage);
834
- * // ...
835
- * subscription.unsubscribe();
1173
+ * const subscription = client.subscribe("/queue/test", callback);
1174
+ * // Unsubscribe using the subscription object
1175
+ * subscription.unsubscribe();
836
1176
  * ```
837
1177
  *
838
- * See: https://stomp.github.com/stomp-specification-1.2.html#UNSUBSCRIBE UNSUBSCRIBE Frame
1178
+ * This method can also be used directly with the subscription ID.
1179
+ *
1180
+ * Example:
1181
+ * ```javascript
1182
+ * client.unsubscribe("my-subscription-id");
1183
+ * ```
1184
+ *
1185
+ * @param id Subscription ID to unsubscribe.
1186
+ * @param headers Optional headers to pass for the UNSUBSCRIBE frame.
839
1187
  */
840
1188
  public unsubscribe(id: string, headers: StompHeaders = {}): void {
841
1189
  this._checkConnection();
@@ -844,10 +1192,21 @@ export class Client {
844
1192
  }
845
1193
 
846
1194
  /**
847
- * Start a transaction, the returned {@link ITransaction} has methods - [commit]{@link ITransaction#commit}
848
- * and [abort]{@link ITransaction#abort}.
1195
+ * Starts a new transaction. The returned {@link ITransaction} object provides
1196
+ * methods for [commit]{@link ITransaction#commit} and [abort]{@link ITransaction#abort}.
1197
+ *
1198
+ * If `transactionId` is not provided, the library generates a unique ID internally.
1199
+ *
1200
+ * Example:
1201
+ * ```javascript
1202
+ * const tx = client.begin(); // Auto-generated ID
1203
+ *
1204
+ * // Or explicitly specify a transaction ID
1205
+ * const tx = client.begin("my-transaction-id");
1206
+ * ```
849
1207
  *
850
- * `transactionId` is optional, if not passed the library will generate it internally.
1208
+ * @param transactionId Optional transaction ID.
1209
+ * @returns An instance of {@link ITransaction}.
851
1210
  */
852
1211
  public begin(transactionId?: string): ITransaction {
853
1212
  this._checkConnection();
@@ -856,16 +1215,19 @@ export class Client {
856
1215
  }
857
1216
 
858
1217
  /**
859
- * Commit a transaction.
860
- *
861
- * It is preferable to commit a transaction by calling [commit]{@link ITransaction#commit} directly on
862
- * {@link ITransaction} returned by [client.begin]{@link Client#begin}.
863
- *
1218
+ * Commits a transaction.
1219
+ *
1220
+ * It is strongly recommended to call [commit]{@link ITransaction#commit} on
1221
+ * the transaction object returned by [client#begin]{@link Client#begin}.
1222
+ *
1223
+ * Example:
864
1224
  * ```javascript
865
- * var tx = client.begin(txId);
866
- * //...
867
- * tx.commit();
1225
+ * const tx = client.begin();
1226
+ * // Perform operations under this transaction
1227
+ * tx.commit();
868
1228
  * ```
1229
+ *
1230
+ * @param transactionId The ID of the transaction to commit.
869
1231
  */
870
1232
  public commit(transactionId: string): void {
871
1233
  this._checkConnection();
@@ -874,15 +1236,19 @@ export class Client {
874
1236
  }
875
1237
 
876
1238
  /**
877
- * Abort a transaction.
878
- * It is preferable to abort a transaction by calling [abort]{@link ITransaction#abort} directly on
879
- * {@link ITransaction} returned by [client.begin]{@link Client#begin}.
880
- *
1239
+ * Aborts a transaction.
1240
+ *
1241
+ * It is strongly recommended to call [abort]{@link ITransaction#abort} directly
1242
+ * on the transaction object returned by [client#begin]{@link Client#begin}.
1243
+ *
1244
+ * Example:
881
1245
  * ```javascript
882
- * var tx = client.begin(txId);
883
- * //...
884
- * tx.abort();
1246
+ * const tx = client.begin();
1247
+ * // Perform operations under this transaction
1248
+ * tx.abort(); // Abort the transaction
885
1249
  * ```
1250
+ *
1251
+ * @param transactionId The ID of the transaction to abort.
886
1252
  */
887
1253
  public abort(transactionId: string): void {
888
1254
  this._checkConnection();
@@ -891,17 +1257,23 @@ export class Client {
891
1257
  }
892
1258
 
893
1259
  /**
894
- * ACK a message. It is preferable to acknowledge a message by calling [ack]{@link IMessage#ack} directly
895
- * on the {@link IMessage} handled by a subscription callback:
896
- *
1260
+ * Acknowledges receipt of a message. Typically, this should be done by calling
1261
+ * [ack]{@link IMessage#ack} directly on the {@link IMessage} instance passed
1262
+ * to the subscription callback.
1263
+ *
1264
+ * Example:
897
1265
  * ```javascript
898
- * var callback = function (message) {
899
- * // process the message
900
- * // acknowledge it
901
- * message.ack();
902
- * };
903
- * client.subscribe(destination, callback, {'ack': 'client'});
1266
+ * const callback = (message) => {
1267
+ * // Process the message
1268
+ * message.ack(); // Acknowledge the message
1269
+ * };
1270
+ *
1271
+ * client.subscribe("/queue/example", callback, { ack: "client" });
904
1272
  * ```
1273
+ *
1274
+ * @param messageId The ID of the message to acknowledge.
1275
+ * @param subscriptionId The ID of the subscription.
1276
+ * @param headers Optional headers for the acknowledgment frame.
905
1277
  */
906
1278
  public ack(
907
1279
  messageId: string,
@@ -914,17 +1286,25 @@ export class Client {
914
1286
  }
915
1287
 
916
1288
  /**
917
- * NACK a message. It is preferable to acknowledge a message by calling [nack]{@link IMessage#nack} directly
918
- * on the {@link IMessage} handled by a subscription callback:
919
- *
1289
+ * Rejects a message (negative acknowledgment). Like acknowledgments, this should
1290
+ * typically be done by calling [nack]{@link IMessage#nack} directly on the {@link IMessage}
1291
+ * instance passed to the subscription callback.
1292
+ *
1293
+ * Example:
920
1294
  * ```javascript
921
- * var callback = function (message) {
922
- * // process the message
923
- * // an error occurs, nack it
924
- * message.nack();
925
- * };
926
- * client.subscribe(destination, callback, {'ack': 'client'});
1295
+ * const callback = (message) => {
1296
+ * // Process the message
1297
+ * if (isError(message)) {
1298
+ * message.nack(); // Reject the message
1299
+ * }
1300
+ * };
1301
+ *
1302
+ * client.subscribe("/queue/example", callback, { ack: "client" });
927
1303
  * ```
1304
+ *
1305
+ * @param messageId The ID of the message to negatively acknowledge.
1306
+ * @param subscriptionId The ID of the subscription.
1307
+ * @param headers Optional headers for the NACK frame.
928
1308
  */
929
1309
  public nack(
930
1310
  messageId: string,