You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A while ago I forked the browser-based CometD JS client to add support to NodeJS, with WebSocket. My general approach is very similar to what you have here: I defined window.setTimeout & friends, but I did a bit more of a hack-y solution regarding the window.location and document objects - it works perfectly well for my specific purposes. But I'm glad to see it officially done/supported now!
I actually build an "isomorphic" solution - that is, it works both on the browser and in NodeJS.
The code to support WebSocket in node is actually very simple. All you have to do is npm install --save ws, and then in the code simply add:
window.WebSocket = require('ws')
As a "nice to have", in case you want to make this isomorphic (yay!), here's how that can be accomplished:
That's it, done. The main code does require('./ws-wrapper.js'). In NodeJS, it resolves to the real ws-wrapper.js file which, in turn, requires the real ws module.
In a packer for a Web-based, the packer reads the "browser": {...} key in package.json and performs the replacement: when it see require('./ws-wrapper.js'), it actually reads the ws-wrapper-browser.js file. The "ws": false key is useful to prevent browser packing tools from even trying to import and include in the bundle the ws dependency, because that would completely break the browser-based build.
The solution works on Node, on the Browser, and even in React Native running on a mobile phone!
Would be awesome if you could add this - or at least the trivial support for WebSocket in node, without the isomorphic feature.
Thanks!
Bruno
The text was updated successfully, but these errors were encountered:
Hunga1
added a commit
to Hunga1/cometd-nodejs-client
that referenced
this issue
Sep 13, 2018
Hello,
A while ago I forked the browser-based CometD JS client to add support to NodeJS, with WebSocket. My general approach is very similar to what you have here: I defined window.setTimeout & friends, but I did a bit more of a hack-y solution regarding the
window.location
anddocument
objects - it works perfectly well for my specific purposes. But I'm glad to see it officially done/supported now!I actually build an "isomorphic" solution - that is, it works both on the browser and in NodeJS.
The code to support WebSocket in node is actually very simple. All you have to do is
npm install --save ws
, and then in the code simply add:As a "nice to have", in case you want to make this isomorphic (yay!), here's how that can be accomplished:
package.json:
ws-wrapper.js:
ws-wrapper-browser.js
That's it, done. The main code does
require('./ws-wrapper.js')
. In NodeJS, it resolves to the realws-wrapper.js
file which, in turn, requires the realws
module.In a packer for a Web-based, the packer reads the
"browser": {...}
key inpackage.json
and performs the replacement: when it seerequire('./ws-wrapper.js')
, it actually reads thews-wrapper-browser.js
file. The"ws": false
key is useful to prevent browser packing tools from even trying to import and include in the bundle the ws dependency, because that would completely break the browser-based build.The solution works on Node, on the Browser, and even in React Native running on a mobile phone!
Would be awesome if you could add this - or at least the trivial support for WebSocket in node, without the isomorphic feature.
Thanks!
Bruno
The text was updated successfully, but these errors were encountered: