@@ -11,66 +11,106 @@ redirect_from:
1111 - /guide/stompjs/rx-stomp/ng2-stompjs/pollyfils-for-stompjs-v5.html
1212---
1313
14- This guide covers critical dependencies and polyfills for version 5+ of ` @stomp/stompjs ` ;
14+ This guide covers critical dependencies and polyfills for version 5+ of ` @stomp/stompjs ` ,
1515which is internally used by ` @stomp/rx-stomp ` as well.
1616
17- ## In NodeJS
17+ Use polyfills only when the runtime does not provide the required APIs.
18+
19+ - Browsers: no WebSocket polyfill is usually needed (use native WebSocket).
20+ - Node.js: add a WebSocket implementation (see below).
21+ - React Native: add TextEncoder/TextDecoder; WebSocket is provided by RN.
22+
23+ ## In Node.js
1824
1925### WebSocket
2026
21- There are two alternate libraries ` websocket ` and ` ws ` which have been reported to work.
27+ Two alternative libraries have been reported to work well: ` ws ` and ` websocket ` .
2228
23- #### websocket
29+ #### Using ` ws ` (recommended)
2430
25- - Add ` websocket ` npm module :
31+ - Install :
2632
2733 ``` bash
28- $ npm install websocket
34+ npm install ws
2935 ```
3036
31- - Using ` import ` syntax
37+ - ESM import and expose as global:
3238
3339 ``` javascript
34- import websocket from ' websocket ' ;
35-
36- Object . assign ( global , { WebSocket : websocket . w3cwebsocket }) ;
40+ import WebSocket from ' ws ' ;
41+ // Make it available where @stomp/stompjs expects a global WebSocket
42+ globalThis . WebSocket = WebSocket ;
3743 ```
3844
39- #### ws
45+ - TypeScript tip: install types if your setup needs them (ws bundles its own types in modern versions).
46+
47+ #### Using ` websocket `
4048
41- - Instead of ` websocket ` lib ` ws ` has also been reported to work.
42- See: [ stompjs/issues/28] ( https://github.com/stomp-js/stompjs/issues/28 ) .
43- - Add ` ws ` npm module:
49+ - Install:
4450
4551 ``` bash
46- $ npm install ws
52+ npm install websocket
4753 ```
4854
49- - Require the module and expose it through ` global `
55+ - ESM import and expose as global:
5056
5157 ``` javascript
52- import { WebSocket } from ' ws' ;
53-
54- Object .assign (global , { WebSocket });
58+ import { w3cwebsocket as W3CWebSocket } from ' websocket' ;
59+ globalThis .WebSocket = W3CWebSocket;
5560 ```
5661
62+ ### TextEncoder/TextDecoder (Node.js)
63+
64+ Modern Node.js provides ` TextEncoder ` /` TextDecoder ` . If your runtime does not, you can polyfill:
65+
66+ ``` javascript
67+ // Option A: from 'util' (available on many Node versions)
68+ import { TextEncoder , TextDecoder } from ' util' ;
69+ if (! globalThis .TextEncoder ) globalThis .TextEncoder = TextEncoder;
70+ if (! globalThis .TextDecoder ) globalThis .TextDecoder = TextDecoder;
71+
72+ // Option B: use a dedicated polyfill package
73+ // npm install fast-text-encoding
74+ import ' fast-text-encoding' ;
75+ // This package attaches TextEncoder/TextDecoder to the global scope
76+ ```
77+
5778## In React Native
5879
80+ React Native provides a WebSocket implementation; you usually do not need to polyfill it.
81+
5982### TextEncoder/TextDecoder
6083
61- - React Native makes it deceptive.
62- When an application is executed in debug mode, it works, as it is executed on an actual browser
63- where TextEncoder/TextDecoder is available.
64- However, when executed in production mode, it fails as TextEncoder/TextDecoder is not available.
65- - Please install ` text-encoding `
84+ - In debug mode, the app runs in a browser-like environment where these APIs exist.
85+ In release builds, they may be missing.
86+ - Install a polyfill:
87+
6688 ``` bash
67- $ npm install text-encoding
89+ npm install text-encoding
6890 ```
69- - Add the following to your ` index.js ` or ` App.js ` :
91+
92+ - Add to your entry file (e.g., ` index.js ` or ` App.js ` ):
7093
7194 ``` javascript
7295 import * as encoding from ' text-encoding' ;
7396 ```
7497
7598- There are additional issues with React Native in some device/version combinations. Please see:
76- [ React Native - Additional Notes] ( /workaround/stompjs/rx-stomp/ng2-stompjs/react-native-additional-notes.html )
99+ [ React Native - Additional Notes] ({% link _ posts/2019-06-10-react-native-additional-notes.md %})
100+
101+ ## Quick verification (any environment)
102+
103+ After setting up required polyfills, a minimal connectivity check should work:
104+
105+ ``` javascript
106+ import { Client } from ' @stomp/stompjs' ;
107+
108+ const client = new Client ({ brokerURL: ' ws://localhost:15674/ws' });
109+ client .onConnect = () => {
110+ console .log (' Connected!' );
111+ client .deactivate ();
112+ };
113+ client .onStompError = (f ) => console .error (' Broker error:' , f .headers [' message' ]);
114+ client .onWebSocketError = (e ) => console .error (' WS error:' , e);
115+ client .activate ();
116+ ```
0 commit comments