question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I run this inside a webworker?

See original GitHub issue

it’s looking for the document inside the webworker code, presumably to check if it’s supported.

Uncaught ReferenceError: document is not defined

Is there a way of avoiding this, I would like to run the reconnectingwebsocket inside a webworker

Thanks!

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Deewaicommented, Jan 26, 2018

i don’t know if this problem has been fixed. But you cannot access DOM in a webworker thats why you can’t use document. But there is a workaround. You can create a fake DOM in a js file the use importScripts to import the js file to your webworker.This will enable you use most DOM objects.Below is the code for the fake DOM

var document = self.document = { parentNode: null, nodeType: 9, toString: function () { return "FakeDocument" } };
var window = self.window = self;
var fakeElement = Object.create(document);
fakeElement.nodeType = 1;
fakeElement.toString = function () { return "FakeElement" };
fakeElement.parentNode = fakeElement.firstChild = fakeElement.lastChild = fakeElement;
fakeElement.ownerDocument = document;

document.head = document.body = fakeElement;
document.ownerDocument = document.documentElement = document;
document.getElementById = document.createElement = function () { return fakeElement; };
document.createDocumentFragment = function () { return this; };
document.getElementsByTagName = document.getElementsByClassName = function () { return [fakeElement]; };
document.getAttribute = document.setAttribute = document.removeChild =
    document.addEventListener = document.removeEventListener =
    function () { return null; };
document.cloneNode = document.appendChild = function () { return this; };
document.appendChild = function (child) { return child; };
document.childNodes = [];
document.implementation = {
    createHTMLDocument: function () { return document; }
}
2reactions
nathanboktaecommented, Oct 14, 2016

robust-websocket runs in a webworker (it uses CustomEvent for eventing).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Web Workers - Web APIs - MDN Web Docs
Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering ......
Read more >
HTML Web Workers API - W3Schools
When executing scripts in an HTML page, the page becomes unresponsive until the script is finished. A web worker is a JavaScript that...
Read more >
How Web Workers Work in JavaScript – With a Practical JS ...
A web worker is a piece of browser functionality. It is the real OS threads that can be spawned in the background of...
Read more >
A Simple Introduction to Web Workers in JavaScript - Medium
The web worker feature revolves around an object called Worker . When you want to run something in the background, you create a...
Read more >
How to execute a function with a web worker on a different ...
When we run code in a worker we can't manipulate DOM elements, use window objects, etc. The context in which workers run are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found