obs-websocket-community-projects / obs-websocket-js

Consumes https://github.com/obsproject/obs-websocket
MIT License
667 stars 96 forks source link

Support as a CommonJS module in browsers #172

Closed emiax closed 4 years ago

emiax commented 4 years ago

Hi,

We are considering using obs-websocket-js as a npm package in a browser-based application. Currently, it seems that the recommended way to import the obs-websocket-js package into a web application is to refer to the pre-built js bundle from a html file. Instead, we would like to be able to import 'obs-websocket-js', and do the bundling ourselves using Webpack.

import 'obs-websocket-js' currently results in ws being used instead of the built-in browser WebSocket implementation, so this fails in browsers. To fix this, we suggest using the isomorphic-ws, package which uses ws in node and global.WebSocket in browsers.

If we do this, we can also remove the Webpack externals configuration that replaces ws with WebSocket and rely on isomorphic-ws to do that for us.

I think these changes would keep both the NodeJS and pre-built browser bundle use cases intact, but there may be some use case that I have not thought of? Please let me know your thoughs, and thanks for providing this as an open source library.

Best, Emil

haganbmj commented 4 years ago

Merged this in with https://github.com/haganbmj/obs-websocket-js/commit/5521364a8dc68c5638817b68cb24dd8797208665 after rebasing. Didn't want to push to yours as it looked like you had tagged a release for your own use. Did some cursory checks in browser and didn't see any issues. I'll tag a release in a little bit.

Sorry for the delay. I lost interest in the project a year ago and it's fallen into a bit of disarray.

haganbmj commented 4 years ago

v4.0.0 is available in npm and bower now.

Have a look at https://github.com/haganbmj/obs-websocket-js/pull/171 as well as it deals with exporting for commonjs too.