stackblitz / webcontainer-core

Dev environments. In your web app.
https://webcontainers.io
MIT License
3.93k stars 172 forks source link

WC to WC WebSocket connections failing with `400 BadRequest` errors; breaking unit tests, REPLs, server-side CLI tools, multi-process optimizations, Server SB to Client SB use and dependencies #719

Closed FossPrime closed 1 year ago

FossPrime commented 2 years ago

Describe the bug

Chat bots, unit tests, DB connections, multi-process optimizations, dependencies and integrations

Describe the bug Trying to connect to a WebSockets server from WebContainers causes a 400 Bad Request error. This happens even when connecting to a Socket.io server hosted in the same WebContainer or other WebContainers in other StackBlitz Sandboxes.

Connecting to WebSockets servers from the browser works normally, as long as CORS is configured. this issue is not about connections from the browser. This issue is exclusively about connecting from WebContainers to WebContainers running WS servers, that is, NOT to external Node.js server. No browser code, baremetal code or external servers are necessary to encounter this issue.

This affects:

Link to the blitz that caused the error

Simplest socket.io example: https://stackblitz.com/edit/sb-ws-bug?file=vite.config.ts,ReadMe.md

Steps to reproduce

  1. run this code on SB, aka npm run dev

Expected behavior

All posted "Chatbot" messages will print to the console.

Actual Behavior

Link to the blitz that caused the error

https://stackblitz.com/edit/sb-ws-bug?file=vite.config.ts,ReadMe.md

Steps to reproduce

  1. open the sand box and watch the errors pop up every 3 seconds.

Expected behavior

Parity with Local

Screenshots

From reproduction sandbox: {912B029F-8E8E-47BA-A788-84F081622A99}

Same code in bare-metal Node.js: {26BB7518-1A14-4A18-A331-B923EBDCEDD3}

Platform

Additional context

Workarounds

Current workaround is to run the server baremetal if possible, or host it externally in something like CodeSandbox Containers or Kubernetes.

d3lm commented 1 year ago

Hey there 👋 Just wanted to inform you that WebSocket support has been added for inter-process communication. This means that you can now have a WebSocket client in one process and a server in another process. I tested your provided chat example and it seems to work as expected. I am closing this issue for now. If you still experience issues, feel free to open a new issue.