Open Code2Life opened 5 years ago
It would be awesome if there is an out of box pure ts/js solution for node.js + TS project, without any preconditions.
Please check https://stomp-js.github.io/guide/stompjs/rx-stomp/ng2-stompjs/2018/06/28/pollyfils-for-stompjs-v5.html. All the test cases successfully pass with this setup. The suggested setup is similar to what you have used.
If you are bundling with Webpack to run at browsers, please include "lib": ["es5", "es2015", "dom"]
in your tsconfig. You will be able to skip the polyfills - depending on your target browsers.
This library does need TextEncoder/Decoder
as it needs to convert internally between String and Bytes. This is built in as default in most of the Web Browsers. It seems it is planned to be distributed by default in future Node versions (https://nodejs.org/api/util.html#util_class_util_textencoder).
I just now checked - if I use Node v11.2.0, I did not need text-encoding
, please check if it works for you.
Thanks a lot ! In my case, the project is supposed to run in node.js env with dynamic js file downloaded, so I use 'ws' instead of 'websocket' to eliminate c++ add on. It works pretty fine.
Thanks, will add this into documentation 😄
Good Solution, works in Google firebase
Nice solution, would be good to have it in the documentation for TS+nodejs. There is nothing in docs and it was a great pain to find this page to understand which solution would work for text encoder.
I added this to the top of my .ts
files but it doesn't work:
import WebSocket from 'ws';
Object.assign(global, { WebSocket: WebSocket});
I assume you are using Node JS.
require
is not necessarily equivalent to import
in Node JS. Please try the following:
Object.assign(global, { WebSocket: require('ws') });
Please enable debug and attach console output if it does not work.
@kum-deepak Now it works, thanks! My code runs at server side instead of browser.
https://github.com/stomp-js/stompjs/issues/28#issuecomment-554984094 This worked for me in NodeJS, thank you @kum-deepak . below is my code.
const WebSocket = require('ws');
Object.assign(global, { WebSocket: require('ws') });
new WebSocket.Server({
port: 8080
})
Leaving this open - so that others can use it as documentation.
Hi, I found some issues when using this lib in my TS project.
Here is my workaround in TS to avoid errors from compiler