Skip to content

Commit c0bca86

Browse files
author
Deepak Kumar
committed
Improved STOMP.js documentation with examples for ES modules and UMD, clarified deactivation and subscription usage, and added guidance on heartbeat tuning and exponential backoff configuration.
1 parent bbf3025 commit c0bca86

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

_posts/2018-06-29-using-stompjs-v5.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,24 @@ You can use these classes directly without prefixing them with `StompJs.`.
5252
All options can be set or read directly on the client instance:
5353

5454
```javascript
55-
const client = new StompJs.Client();
55+
// ES modules (Node.js/modern bundlers)
56+
import { Client } from '@stomp/stompjs';
57+
58+
const client = new Client();
5659
client.brokerURL = 'ws://localhost:15674/ws';
5760

5861
console.log(client.brokerURL);
5962
```
6063

64+
If using the UMD bundle in a browser, create with the global namespace:
65+
66+
```javascript
67+
// UMD (browser via <script>)
68+
const client = new StompJs.Client();
69+
client.brokerURL = 'ws://localhost:15674/ws';
70+
console.log(client.brokerURL);
71+
```
72+
6173
You can also pass them as key–value pairs to the [Client constructor](/api-docs/latest/classes/Client.html#constructor) or to [Client#configure](/api-docs/latest/classes/Client.html#configure).
6274

6375
## Create a STOMP client
@@ -101,7 +113,8 @@ client.activate();
101113
To deactivate a client, call [Client#deactivate](/api-docs/latest/classes/Client.html#deactivate). It stops reconnection attempts and disconnects any active connection.
102114

103115
```javascript
104-
client.deactivate();
116+
// Prefer awaiting deactivation to ensure the client fully disconnects
117+
await client.deactivate();
105118
```
106119

107120
## Send messages
@@ -153,14 +166,16 @@ The `subscribe` method returns an object with an `id` (the client subscription I
153166
Each time the broker sends a message, the client invokes the callback with a [Message](/api-docs/latest/interfaces/IMessage.html) object.
154167

155168
```javascript
156-
callback = function (message) {
169+
const onMessage = function (message) {
157170
// Called when the client receives a STOMP message from the server
158171
if (message.body) {
159172
alert('Got message with body ' + message.body);
160173
} else {
161174
alert('Got empty message');
162175
}
163176
};
177+
178+
const subscription = client.subscribe('/queue/test', onMessage);
164179
```
165180

166181
You can pass optional headers when subscribing:
@@ -311,6 +326,8 @@ client.heartbeatOutgoing = 20000; // client will send heartbeats every 20000ms
311326
client.heartbeatIncoming = 0; // client does not want to receive heartbeats from the server
312327
```
313328

329+
Very small heartbeat intervals can increase server load; tune with care in production.
330+
314331
## Auto Reconnect
315332

316333
The client supports automatic reconnection after a connection failure. It is controlled by the [Client#reconnectDelay](/api-docs/latest/classes/Client.html#reconnectDelay) option. The default is 5000 ms, meaning the client will attempt to reconnect 5 seconds after a drop.
@@ -325,13 +342,18 @@ You can set `reconnectDelay` to a small value.
325342
**Reconnect with Exponential Backoff**
326343

327344
```javascript
345+
// ES modules
346+
import { ReconnectionTimeMode } from '@stomp/stompjs';
347+
328348
client.configure({
329-
reconnectTimeMode: StompJs.ReconnectionTimeMode.EXPONENTIAL,
349+
reconnectTimeMode: ReconnectionTimeMode.EXPONENTIAL,
330350
reconnectDelay: 200, // It will wait 200, 400, 800 ms...
331351
maxReconnectDelay: 10000, // Optional: when provided, it will not wait more than this
332-
})
352+
});
333353
```
334354

355+
For UMD usage in the browser, use `StompJs.ReconnectionTimeMode.EXPONENTIAL`.
356+
335357
## Debug
336358

337359
On a busy system, the volume of logs can be overwhelming. Therefore, debug messages are ignored by default.

0 commit comments

Comments
 (0)