Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web3 -- web worker environment isn't supported #2211

Closed
7-of-9 opened this issue Jan 23, 2019 · 5 comments
Closed

web3 -- web worker environment isn't supported #2211

7-of-9 opened this issue Jan 23, 2019 · 5 comments

Comments

@7-of-9
Copy link

7-of-9 commented Jan 23, 2019

Hi all,

Beta 34 (and/or its dependencies, e.g. any-promise) seems to have dependencies on "window" instead of "self" which prevents it working in a web worker context. I've not yet tested on later betas, but suspect they suffer from the same.

Related: #1774
Related: https://ethereum.stackexchange.com/questions/56405/integrating-web3-from-metamask-in-react

@nivida
Copy link
Contributor

nivida commented Jan 25, 2019

This issue got solved in the version 1.0.0-beta.38 which I will release within the next days.

@nivida nivida closed this as completed Jan 25, 2019
@7-of-9
Copy link
Author

7-of-9 commented Feb 9, 2019

@nivida fyi -- re. beta41: I'm seeing "window is not defined" when creating a new Web3.providers.WebsocketProvider(...) inside A web worker context.

(< beta38 threw much earlier: at "import Web3...", so it's closer to working!)

UPDATE: self.window = self
In web worker before new WebsockProvider() seems to fix this.

Fix at source would be line 1162 of web3-provider-umd.js (beta41) - just remove "Window."

@naturalfreak
Copy link

same problem with version 1.2.4

@timothywangdev
Copy link

still broken

@keirongulrajani
Copy link

keirongulrajani commented Feb 5, 2020

When I've tried importScripts in a Web Worker using web3.js instead of web3.min.js from the node module i can see the window is not defined error being thrown when register-shim is being loaded; this is a file inside any-promise, and the any-promise library is used by web3-core-promievent in the current version.

The loadImplementation() inside register-shim.js is attempting to grab window.Promise when there is no window:

/**
 * Browser specific loadImplementation.  Always uses `window.Promise`
 *
 * To register a custom implementation, must register with `Promise` option.
 */
function loadImplementation(){
  if(typeof window.Promise === 'undefined'){
    throw new Error("any-promise browser requires a polyfill or explicit registration"+
      " e.g: require('any-promise/register/bluebird')")
  }
  return {
    Promise: window.Promise,
    implementation: 'window.Promise'
  }
}

perhaps doing as it says to register a custom implementation with a Promise option is the solution?

EDIT: for those experiencing this issue with the window being undefined, I found a solution here about setting window as self first before the importScripts statements so that it has the correct environment (effectively tricking any-promise under the hood while loading to treat the worker as window:

var window = self;
importScripts(/*path to web 3 goes here*/);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants