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

Socket.io 4.2.x broken with Vite #4798

Closed
7 tasks done
tcastelly opened this issue Aug 31, 2021 · 6 comments · Fixed by #10314
Closed
7 tasks done

Socket.io 4.2.x broken with Vite #4798

tcastelly opened this issue Aug 31, 2021 · 6 comments · Fixed by #10314
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@tcastelly
Copy link

Describe the bug

Hello,

After an upgrade from socket.io 4.1.x to 4.2.x, it's not possible anymore to listen events.

Reproduction

I've created a project with three branches to reproduce the error:

With Webpack it works
https://github.com/shenron/issue-vue-vite-socket

With Vite it works only with socket.io 4.1.3
https://github.com/shenron/issue-vue-vite-socket/tree/vite-socket-4.1.3

With the last version of socket it's broken
https://github.com/shenron/issue-vue-vite-socket/tree/vite

System Info

System:
    OS: Linux 5.10 Arch Linux
    CPU: (32) x64 AMD Ryzen 9 5950X 16-Core Processor
    Memory: 22.09 GB / 31.34 GB
    Container: Yes
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.0 - ~/.nvm/versions/node/v14.17.0/bin/node
    Yarn: 1.22.10 - ~/.nvm/versions/node/v14.17.0/bin/yarn
    npm: 6.14.13 - ~/.nvm/versions/node/v14.17.0/bin/npm
  Browsers:
    Chromium: 92.0.4515.159

Used Package Manager

npm

Logs

No response

Validations

@tcastelly tcastelly changed the title Vite + Socket.io 4.2.x Socket.io 4.2.x broken with Vite Aug 31, 2021
@holygekko
Copy link

holygekko commented Sep 14, 2021

I have a similar problem. When using socket.io-client 4.2.x running vite in test/dev mode (vite --host --mode test) it can't use polling. After downgrading to 4.1.3 it works. Also, if I build my project (with 4.2.x) and deploy it also works!

A different solution could als be to create a connection setting the transport option to 'websocket' first:
const socket = io("https://example.com", { transports: ["websocket"] });

That it skips the polling and it works with 4.2.x, but you don't have a fallback option than.

@tcastelly
Copy link
Author

Thank you @holygekko for our feedback. Changing the transports tor websocket works for me,

@darrachequesne
Copy link

I could indeed reproduce the issue, it seems the browser field is ignored here: https://github.com/socketio/engine.io-client/blob/c7e27b0b1dc778e1fd79af2140641e745cfd6270/package.json#L73

So the xmlhttprequest-ssl module, which is an implementation of XMLHttpRequest for Node.js, is loaded in the browser:

// node_modules/xmlhttprequest-ssl/lib/XMLHttpRequest.js
var require_XMLHttpRequest = __commonJS({
  "node_modules/xmlhttprequest-ssl/lib/XMLHttpRequest.js"(exports, module) {
    var fs = (init_fs(), fs_exports);
    var Url = (init_url(), url_exports);
    var spawn = (init_child_process(), child_process_exports).spawn;
    module.exports = XMLHttpRequest;
    XMLHttpRequest.XMLHttpRequest = XMLHttpRequest;
    function XMLHttpRequest(opts) {
      "use strict";
      opts = opts || {};
      var self2 = this;
      var http = (init_http(), http_exports);
      var https = (init_https(), https_exports);
[...]

Which explains:

  • why it breaks in socket.io-client@4.2.0: socketio/engine.io-client@2de8b4e
  • why switching to WebSocket only works too (xmlhttprequest-ssl is only used for HTTP long-polling)

What's weird is that the other fields in the browser object seems to be taken in account. Can someone shed some light on this?

@darrachequesne
Copy link

As a workaround, it is possible to force the resolution in vite.config.js:

export default {
  resolve: {
    alias: {
      "xmlhttprequest-ssl": "./node_modules/engine.io-client/lib/xmlhttprequest.js"
    }
  }
}

I don't understand why the "xmlhttprequest-ssl": "./lib/xmlhttprequest.js" alias in the browser section is not taken in account though. It only happens during development, it is properly resolved when building for production.

@sodatea sodatea added p3-minor-bug An edge case that only affects very specific usage (priority) p2-edge-case Bug, but has workaround or limited in scope (priority) and removed pending triage p3-minor-bug An edge case that only affects very specific usage (priority) labels Sep 24, 2021
kiito1000 added a commit to hichain/web_apps that referenced this issue Sep 30, 2021
@darrachequesne
Copy link

darrachequesne commented Oct 11, 2021

After a little digging in the resolvePackageEntry method here, it seems that only the browser field of the current package is taken in account, and not its ancestor.

Reproducible case: https://github.com/darrachequesne/vite-browser-issue

Directly using esbuild seems to work though 👀

@marcinczenko
Copy link

I had problem with version 2.5.0. Nice to see there are some workarounds to the problem. In the meantime I managed to upgrade socket.io (together with the client) to the most recent version 4.5.1, and there everything seems to be working fine without any extra config. Just reporting.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
6 participants