stompjs icon indicating copy to clipboard operation
stompjs copied to clipboard

Polyfills for node.js + TS + webpack project

Open Code2Life opened this issue 7 years ago • 13 comments

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

  • pre installed 'websocket' lib needs .node c++ compiled module, it's not compatible in webpack, I tried 'ws' instead of 'websocket', and it seems work.
  • another dependency in node env is 'text-encoding', it's too large, I want compiled .js to be smaller. thus, is there an alternative to 'text-encoding' ?

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 avatar Nov 25 '18 12:11 Code2Life

It would be awesome if there is an out of box pure ts/js solution for node.js + TS project, without any preconditions.

Code2Life avatar Nov 25 '18 12:11 Code2Life

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 avatar Nov 25 '18 13:11 kum-deepak

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

kum-deepak avatar Nov 25 '18 14:11 kum-deepak

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.

Code2Life avatar Nov 26 '18 01:11 Code2Life

Thanks, will add this into documentation 😄

kum-deepak avatar Nov 26 '18 04:11 kum-deepak

Good Solution, works in Google firebase

fintecheand0 avatar Mar 17 '19 06:03 fintecheand0

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.

shakura avatar Apr 05 '19 14:04 shakura

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

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

soulmachine avatar Nov 18 '19 09:11 soulmachine

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 avatar Nov 18 '19 11:11 kum-deepak

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

kum-deepak avatar Nov 18 '19 11:11 kum-deepak

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

soulmachine avatar Nov 21 '19 10:11 soulmachine

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

UmamaheshMaxwell avatar May 27 '20 09:05 UmamaheshMaxwell

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

kum-deepak avatar May 28 '20 14:05 kum-deepak