THRIFT-5003: Websocket Connection in Browsers with nodejs code
* changed this to self in forEach callback
* updated minimum node version to 8.16.2 (Maintenance LTS until December 2019)
changed ws_connection.js to work in the browser, with isomorphic-ws
added exports for `wsConnection`, `createWSConnection`, `createWSClient`
* added exports for WSConnection to browser.js
* extended the sample of nodejs code in the browser with webpack
* tested and updated node version to LTS 10.18.0 Dubnium
discussion based: https://github.com/apache/thrift/pull/1927#discussion_r358140463
diff --git a/lib/nodejs/README.md b/lib/nodejs/README.md
index ed306c1..c087440 100644
--- a/lib/nodejs/README.md
+++ b/lib/nodejs/README.md
@@ -75,8 +75,8 @@
thrift --gen js:node,ts,es6,with_ns
-```
-import * as thrift from 'thrift/browser';
+```javascript
+import * as thrift from 'thrift';
import { MyServiceClient } from '../gen-nodejs/MyService';
let host = window.location.hostname;
@@ -108,4 +108,35 @@
});
```
-Note that thrift/index.js must be renamed or skipped for browsers.
+Bundlers, like webpack, will use thrift/browser.js by default because of the
+`"browser": "./lib/nodejs/lib/thrift/browser.js"` field in package.json.
+
+### Browser example with WebSocket, BufferedTransport and BinaryProtocol
+```javascript
+import thrift from 'thrift';
+import { MyServiceClient } from '../gen-nodejs/MyService';
+
+const host = window.location.hostname;
+const port = 9090;
+const opts = {
+ transport: thrift.TBufferedTransport,
+ protocol: thrift.TBinaryProtocol
+}
+const connection = thrift.createWSConnection(host, port, opts);
+connection.open();
+const thriftClient = thrift.createWSClient(MyServiceClient, connection);
+
+connection.on('error', (err) => {
+ console.error(err);
+});
+
+thriftClient.myService(param)
+ .then((result) => {
+ console.log(result);
+ })
+ .catch((err) => {
+ ....
+ });
+```
+
+