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

Use WebSocket from within worker? #9

Open
FreeLiveTutor opened this issue Apr 18, 2013 · 9 comments
Open

Use WebSocket from within worker? #9

FreeLiveTutor opened this issue Apr 18, 2013 · 9 comments

Comments

@FreeLiveTutor
Copy link

Hi

Firstly, great work. I have a huge OCD for not using 3rd party libraries, but your 'node-webworker-threads' feels like it should be part of the core node.

As context, I've written a browser based wep-app that relies on WebWorkers, and now I'm able to use your library to simulate a headless client at the server side with node, with almost no coding changes to the App. A real breakthrough for me!

I've come up against a challenge though, as I also require WebSockets - initiated from within the WebWorker.

Inside my worker code I have :
connection = new WebSocket( url, protocol);

I've run some successfull tests from a node main thread using:
var WebSocket = require('websocket').client; [from https://github.com/Worlize/WebSocket-Node]

However I can't use require to pull in modules into a node-webworker.

Can you let me know your recommended approach to get around this?

Many thanks.

@audreyt
Copy link
Owner

audreyt commented Apr 25, 2013

Yeah. Currently we recommend the use of OneJS ( https://github.com/azer/onejs ) or similar packagers to pack node modules into a single script that can be passed to importScripts to require from.

However, that only pertains to computation-only node modules, and currently we don't expose Node's network stack to Thread-local usage. https://github.com/rob333/thread.js contains the bits we need (indeed our console and native_fs_ implementations are adapted from its jslib), so if someone would like to volunteer to port the rest of thread-specific modules over, that'd be very very helpful.

@FreeLiveTutor
Copy link
Author

Thanks for the links. Until you get a volunteer (I'd try but probably not up to the task...!) it might be worth a quick section in the intro / docs to quickly highlight what's not exposed from Node or W3C Worker spec. A lot of the Web Worker tutorials suggested 'use cases' mention handling network activity in the background so its probably quite a common need for a Web Worker.
Either way - keep up the great work!

@FreeLiveTutor
Copy link
Author

Hi - did you get any volunteers on exposing node's network stack?
If you could let me know either way if this is likely to be added soon, as I'll change the architecture of my application to suit otherwise.

(currently I'm still with my original design of WebSocket command handlers being handled from within the WebWorker, but as a workaround for the above I added another layer of message routing back between the MainThread - however now the app is getting fairly complex I could do with removing this extra layer of complexity, either by rebuilding the WS handlers solely in the main thread, or (hopefully!) waiting for nodes network stack to be exposed)

Many thanks!

@audreyt
Copy link
Owner

audreyt commented Sep 20, 2013

Understood. No volunteers have stepped forward yet. I'm still hopeful though! :-)

@tyduptyler13
Copy link

I am trying to do something similar. I need to be able to use the perl inside of the worker function. Is it possible to pass the constructed object to the worker?

@saadnaji
Copy link

saadnaji commented Feb 2, 2015

Hi audreyt,

Can you explain a little bit in detail in why WebSocket implementation using node JS , for example this one:

https://github.com/websockets/ws

does not work in webwoker threads?.

I am trying to use ws with webwoker threads to debug and test some project that involve using these two. Although I am new NodeJS , I might offer to help in implementing the missing stuff that web worker needs to get WebSocket needed for my own project if you explain a little bit.

Thank you.

@audreyt
Copy link
Owner

audreyt commented Feb 2, 2015

Hi @saadnaji , the main issue is that, as described #9 (comment) workers run in a "pure" v8 Isolate, and does not have access to Node's net module, which the ws implementation would rely on.

@saadnaji
Copy link

saadnaji commented Feb 2, 2015

Thank you for the reply,

I have knowledge in C programming which webwoker threads uses part of it in its implementation. What is the best way to start contributing to help expose 'net' module?. Can you provide directions?. Like I said above, I started learning NodeJS last month and JS 3 months ago for my undergraduate CS project.

@audreyt
Copy link
Owner

audreyt commented Feb 2, 2015

Yes. However, currently unless you need to pass very large amount of data between threads, or plan to spawn a huge amount of threads, the process-based https://www.npmjs.com/package/workerjs should already work with ws and is a more practical solution with the same API.

Similar discussions: nodejs/node#180 node-forward/discussions#25

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

No branches or pull requests

4 participants