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

Websocket is undefined #25

Closed
Ahershel opened this issue May 18, 2018 · 8 comments
Closed

Websocket is undefined #25

Ahershel opened this issue May 18, 2018 · 8 comments

Comments

@Ahershel
Copy link

Hi, i have a problem when testing my component using mocha & chai, it says:

ReferenceError: WebSocket is not defined
    at module.exports.r.open (/Users/bcw-001/dev/sofia_fe_react/node_modules/sockette/src/index.js:25:32)
    at new module.exports (/Users/bcw-001/dev/sofia_fe_react/node_modules/sockette/src/index.js:48:7)

i use mock-websocket. anyone know what did i miss?

@lukeed
Copy link
Owner

lukeed commented May 18, 2018

Hi, this isn't to do with Sockette.

There's no WebSocket class in node.js, so you have to add one to the global scope.

Not sure if mock-socket does that or if you have to do it manually. However, I did notice in the first paragraph it says it's"unstable" so maybe you should find another solution?

I've answered elsewhere in these issues my recommendation. Eventually I'll include tests in this repo so that there's an example to follow, but no time now =\

Good luck!

@lukeed lukeed closed this as completed May 18, 2018
@Ahershel
Copy link
Author

Ahershel commented May 21, 2018

Thanks Lukeed,
Just info for those who have the same problem. I solve it by installing ws and import it (see below) in the first line of node_modules/sockette/dist/sockette.js
const WebSocket = require('ws');

@lukeed
Copy link
Owner

lukeed commented May 21, 2018

No problem~!

You shouldn't modify any node_modules/** code -- all your changes will be immediately lost anytime you install another dependency or if/when a coworker tries to replicate your setup.

Instead, you should do this in your testing setup file, or at the top of test files (repetitive):

// test/setup.js
global.WebSocket = require('ws');

@Ahershel
Copy link
Author

it works! thanks again ^^

@tinchoz49
Copy link

tinchoz49 commented Mar 14, 2019

Hi @lukeed how are you?

There's no WebSocket class in node.js, so you have to add one to the global scope.

Would you accept a PR to change the behavior so we can do something like this:

const ws = new Sockette('ws://localhost:3000', {
  ws: require('ws')
});

By default would be the window.WebSocket if is the browser and throw an error if you don't set a ws for nodejs

@lukeed
Copy link
Owner

lukeed commented Mar 14, 2019

Hey @tinchoz49 👋

No I would not, sorry. It would be meaningless to Sockette. You should simply do this instead:

global.WebSocket = require('ws');

This would only be set in your Node.js environment, leaving window.WebSocket intact for your browser bundle(s). If that's not the case, then you will need to take a closer look at your build config because you should never have server code/deps in your front-end bundles. I can maybe help with this part if you have questions or issues.

The above will give you full support 👍

@tinchoz49
Copy link

Don't worry 😃 I understand. Thanks for the quick response!

@lukeed
Copy link
Owner

lukeed commented Mar 14, 2019

Of course! Feel free to ping me if you have issues with setting that up~

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

3 participants