stomp-js / stompjs

Javascript and Typescript Stomp client for Web browsers and node.js apps
Apache License 2.0
773 stars 81 forks source link

Polyfills for node.js + TS + webpack project #28

Open Code2Life opened 5 years ago

Code2Life commented 5 years ago

Hi, I found some issues when using this lib in my TS project.

Here is my workaround in TS to avoid errors from compiler

  Object.assign(global, { WebSocket: require('ws') });

  if (typeof TextEncoder !== 'function') {
    const TextEncodingPolyfill = require('text-encoding');
    Object.assign(global, { TextEncoder: TextEncodingPolyfill.TextEncoder});
    Object.assign(global, { TextDecoder: TextEncodingPolyfill.TextDecoder});
  }
Code2Life commented 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.

kum-deepak commented 5 years ago

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).

kum-deepak commented 5 years ago

I just now checked - if I use Node v11.2.0, I did not need text-encoding, please check if it works for you.

Code2Life commented 5 years ago

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.

kum-deepak commented 5 years ago

Thanks, will add this into documentation 😄

fintecheand0 commented 5 years ago

Good Solution, works in Google firebase

shakura commented 5 years ago

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.

soulmachine commented 5 years ago

I added this to the top of my .ts files but it doesn't work:

import WebSocket from 'ws';
Object.assign(global, { WebSocket:  WebSocket});
kum-deepak commented 5 years ago

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') });
kum-deepak commented 5 years ago

Please enable debug and attach console output if it does not work.

soulmachine commented 4 years ago

@kum-deepak Now it works, thanks! My code runs at server side instead of browser.

UmamaheshMaxwell commented 4 years ago

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
})
kum-deepak commented 4 years ago

Leaving this open - so that others can use it as documentation.