@@ -14,42 +14,40 @@ redirect_from:
1414** There are a large number of obsolete (and copied from one another)
1515examples for these libraries that use SockJS.
1616In reality, there is very little chance that you need SockJS.
17- Unless you know, for sure, you do not need SockJS.**
17+ Unless you know for sure, you do not need SockJS.**
1818
19- This guide covers how to use [ SockJS client] instead of WebSockets as underlying transport.
19+ This guide covers how to use [ SockJS client] instead of WebSockets as the underlying transport.
2020
2121** For Spring STOMP users:**
22- _ There are few tutorials/guides that implicitly suggest that you need SockJS to use STOMP.
23- That is incorrect, you only need SockJS if you need to support old browsers._
22+ _ Some tutorials/guides implicitly suggest that you need SockJS to use STOMP.
23+ That is incorrect — you only need SockJS if you need to support ancient browsers._
2424
2525## Do you need SockJS?
2626
27- As of 2018, WebSocket support in browsers is ubiquitous.
27+ WebSocket support in modern browsers is ubiquitous.
2828Please check [ https://caniuse.com/#feat=websockets ] ( https://caniuse.com/#feat=websockets ) .
2929Depending on your user base, you can skip this page.
3030
3131You can use [ SockJS client] to support browsers that do not support WebSockets natively.
3232
33- You would need to consider the following:
33+ You should consider the following:
3434
35- - URL protocol conventions are different for WebSockets (` ws: ` /` wss: ` ) and SockJS (` http: ` or ` https: ` ).
36- - Internal handshake sequences are different — so, some brokers will use different end points for
37- both protocols.
38- - Neither of these allows custom headers to be set during the HTTP handshake.
39- - SockJS internally supports different transport mechanisms. You might face specific limitations
40- depending on actual transport in use.
41- - Auto reconnect is not quite reliable with SockJS.
35+ - URL protocol conventions are different for WebSockets (` ws: ` /` wss: ` ) and SockJS (` http: ` /` https: ` ).
36+ - Handshake sequences differ — some brokers expose different endpoints for both protocols.
37+ - Neither protocol allows custom headers during the initial HTTP handshake.
38+ - SockJS uses multiple transport mechanisms; you may face limitations depending on the transport in use.
39+ - Auto-reconnect may be less reliable with SockJS.
4240- Heartbeats may not be supported over SockJS by some brokers.
43- - SockJS does not allow more than one simultaneous connection to the same broker.
44- This usually is not a problem for most of the applications.
41+ - SockJS typically does not allow more than one simultaneous connection to the same broker.
42+ This is usually not a problem for most applications.
4543
46- It is advised to use WebSockets by default and then fall back to SockJS if the browser does not support.
44+ Prefer native WebSockets by default and fall back to SockJS only if the browser lacks WebSocket support or your deployment requires HTTP-only transport .
4745
4846## Basic installation
4947
5048### In Node.js environments
5149
52- Please install the latest[ SockJS client] :
50+ Please install the latest [ SockJS client] :
5351
5452``` bash
5553$ npm install sockjs-client --save
@@ -60,7 +58,10 @@ $ npm install sockjs-client --save
6058Depending on your programming language/environment, you may have to ` import ` or ` require ` it:
6159
6260``` typescript
63- import * as SockJS from ' sockjs-client' ;
61+ // ESM
62+ import SockJS from ' sockjs-client' ;
63+ // or (older TypeScript configs)
64+ // import * as SockJS from 'sockjs-client';
6465```
6566
6667### In the browser
@@ -73,15 +74,17 @@ Add the following to include directly in the browser:
7374
7475## Implement a webSocketFactory
7576
76- Create a function that returns an object similar to WebSocket (typically SockJS instance).
77+ Create a function that returns an object similar to WebSocket (typically a SockJS instance).
7778
7879``` typescript
80+ import SockJS from ' sockjs-client' ;
81+
7982export function mySocketFactory() {
8083 return new SockJS (' http://127.0.0.1:15674/stomp' );
8184}
8285```
8386
84- This function should return a WebSocket compatible object.
87+ This function should return a WebSocket- compatible object.
8588
8689** Note: this function may be invoked multiple times.
8790Each time a broker (re)connects, it needs a new instance of WebSocket/SockJS.**
@@ -90,16 +93,19 @@ Each time a broker (re)connects, it needs a new instance of WebSocket/SockJS.**
9093
9194You should set [ webSocketFactory] instead of [ brokerURL] in your configuration.
9295
93- You can even check if WebSocket is available and accordingly use SockJS as a fallback.
96+ You can also detect WebSocket availability and use SockJS as a fallback.
9497See the example below.
9598
96- ** Note: if you set both [ webSocketFactory] takes precedence.**
99+ ** Note: if you set both, [ webSocketFactory] takes precedence.**
97100
98101## Example with stompjs
99102
100103``` javascript
101- const client = new StompJs.Client ({
102- brokerURL: ' ws://localhost:15674/ws' ,
104+ // ESM usage recommended
105+ import { Client } from ' @stomp/stompjs' ;
106+
107+ const client = new Client ({
108+ brokerURL: ' ws://localhost:15674/ws' , // Native WebSocket URL (if available)
103109 connectHeaders: {
104110 login: ' user' ,
105111 passcode: ' password' ,
@@ -114,7 +120,7 @@ const client = new StompJs.Client({
114120
115121// Fallback code
116122if (typeof WebSocket !== ' function' ) {
117- // For SockJS you need to set a factory that creates a new SockJS instance
123+ // For SockJS, set a factory that creates a new SockJS instance
118124 // to be used for each (re)connect
119125 client .webSocketFactory = function () {
120126 // Note that the URL is different from the WebSocket URL
@@ -123,15 +129,15 @@ if (typeof WebSocket !== 'function') {
123129}
124130
125131client .onConnect = function (frame ) {
126- // Do something, all subscribes must be done is this callback
127- // This is needed because this will be executed after a (re)connect
128- };
132+ // Do something; all subscribes must be done in this callback
133+ // This is needed because it executes after a (re)connect
134+ };
129135
130136client .onStompError = function (frame ) {
131- // Will be invoked in case of error encountered at Broker
132- // Bad login/passcode typically will cause an error
133- // Complaint brokers will set `message` header with a brief message. Body may contain details.
134- // Compliant brokers will terminate the connection after any error
137+ // Invoked in case of an error reported by the broker
138+ // Bad login/passcode typically causes an error
139+ // Compliant brokers set the `message` header with a brief message; the body may contain details.
140+ // Compliant brokers terminate the connection after any error
135141 console .log (' Broker reported error: ' + frame .headers [' message' ]);
136142 console .log (' Additional details: ' + frame .body );
137143};
@@ -140,14 +146,31 @@ client.activate();
140146```
141147
142148Compare the above against the sample in [ using StompJS] ({% link _ posts/2018-06-29-using-stompjs-v5.md %}),
143- only addition is the fallback code trying to use SockJS if WebSocket is unavailable.
144- You will need to include latest [ SockJS client] in your web page.
149+ the only addition is the fallback code to use SockJS if WebSocket is unavailable.
150+ You will need to include the latest [ SockJS client] in your web page when using the UMD build.
151+
152+ ### Always using SockJS (when your server exposes only HTTP endpoints)
153+
154+ If your server provides only the SockJS endpoint (no native WebSocket), configure the factory unconditionally:
155+
156+ ``` javascript
157+ import { Client } from ' @stomp/stompjs' ;
158+ import SockJS from ' sockjs-client' ;
159+
160+ const client = new Client ({
161+ webSocketFactory : () => new SockJS (' https://example.com/stomp' ),
162+ connectHeaders: { login: ' user' , passcode: ' password' },
163+ reconnectDelay: 5000 ,
164+ });
165+
166+ client .activate ();
167+ ```
145168
146- ## SockJS in Angular6
169+ ## SockJS in Angular (Angular 6+)
147170
148171See: [ ng2-stompjs/issues/70] .
149172
150- When you are using SockJS in an Angular6 project you might get ** "global is not defined"** .
173+ When you are using SockJS in an Angular project you might get ** "global is not defined"** .
151174
152175The underlying issue can only be fixed by SockJS or Angular teams.
153176Try any of the following workarounds (from [ ng2-stompjs/issues/70] ):
@@ -160,7 +183,7 @@ Try any of the following workarounds (from [ng2-stompjs/issues/70]):
160183 </script >
161184```
162185
163- - Add following to your ` polyfill .ts` :
186+ - Add the following to your ` polyfills .ts` :
164187
165188``` typescript
166189(window as any ).global = window ;
0 commit comments