You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: _posts/2018-06-29-using-stompjs-v5.md
+27-5Lines changed: 27 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,12 +52,24 @@ You can use these classes directly without prefixing them with `StompJs.`.
52
52
All options can be set or read directly on the client instance:
53
53
54
54
```javascript
55
-
constclient=newStompJs.Client();
55
+
// ES modules (Node.js/modern bundlers)
56
+
import { Client } from'@stomp/stompjs';
57
+
58
+
constclient=newClient();
56
59
client.brokerURL='ws://localhost:15674/ws';
57
60
58
61
console.log(client.brokerURL);
59
62
```
60
63
64
+
If using the UMD bundle in a browser, create with the global namespace:
65
+
66
+
```javascript
67
+
// UMD (browser via <script>)
68
+
constclient=newStompJs.Client();
69
+
client.brokerURL='ws://localhost:15674/ws';
70
+
console.log(client.brokerURL);
71
+
```
72
+
61
73
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).
62
74
63
75
## Create a STOMP client
@@ -101,7 +113,8 @@ client.activate();
101
113
To deactivate a client, call [Client#deactivate](/api-docs/latest/classes/Client.html#deactivate). It stops reconnection attempts and disconnects any active connection.
102
114
103
115
```javascript
104
-
client.deactivate();
116
+
// Prefer awaiting deactivation to ensure the client fully disconnects
117
+
awaitclient.deactivate();
105
118
```
106
119
107
120
## Send messages
@@ -153,14 +166,16 @@ The `subscribe` method returns an object with an `id` (the client subscription I
153
166
Each time the broker sends a message, the client invokes the callback with a [Message](/api-docs/latest/interfaces/IMessage.html) object.
154
167
155
168
```javascript
156
-
callback=function (message) {
169
+
constonMessage=function (message) {
157
170
// Called when the client receives a STOMP message from the server
@@ -311,6 +326,8 @@ client.heartbeatOutgoing = 20000; // client will send heartbeats every 20000ms
311
326
client.heartbeatIncoming=0; // client does not want to receive heartbeats from the server
312
327
```
313
328
329
+
Very small heartbeat intervals can increase server load; tune with care in production.
330
+
314
331
## Auto Reconnect
315
332
316
333
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.
0 commit comments