reconnecting-websocket icon indicating copy to clipboard operation
reconnecting-websocket copied to clipboard

How do I run this inside a webworker?

Open vgoklani opened this issue 9 years ago • 13 comments

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!

vgoklani avatar Apr 14 '15 16:04 vgoklani

@drewnoakes Any thoughts on how to do this without document?

joewalnes avatar Apr 21 '15 14:04 joewalnes

Can't think of anything, no.

Maybe just disable the event code if document is undefined.

drewnoakes avatar Apr 21 '15 19:04 drewnoakes

have you tried passing document the worker?

r3wt avatar Apr 21 '15 19:04 r3wt

the worker can't see the document.

vgoklani avatar Apr 21 '15 19:04 vgoklani

have you tried passing in document with postMessage()

r3wt avatar Apr 21 '15 19:04 r3wt

could you please give me an example of what you mean

vgoklani avatar Apr 21 '15 19:04 vgoklani

You cannot pass a document to a worker. Only serializable types (basically JSON structures).

The problem is it's using the DOM event mechanism for registering multiple event handlers. The solution will be break its dependence on the DOM event mechanism and implement a custom event emitter.

joewalnes avatar Apr 21 '15 19:04 joewalnes

How about using a new XMLHttpRequest? XMLHttpRequest is a type of EventTarget, and it's available in workers ...

asppsa avatar Dec 02 '15 04:12 asppsa

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

nathanboktae avatar Oct 14 '16 05:10 nathanboktae

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; }
}

Deewai avatar Jan 26 '18 15:01 Deewai

In my page error is also fixed. thanks for the code.

SivanKarthick avatar Sep 11 '18 09:09 SivanKarthick

@Deewai how to use it inside web worker? I know how to import, but not how to use

rof20004 avatar Apr 21 '21 02:04 rof20004

Once you import the custom DOM, you can go about using the websocket like you will normally do in a web page

var socket = new ReconnectingWebSocket(url, protocols, options);

Deewai avatar Apr 21 '21 13:04 Deewai