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

[🐛 Bug]: Can't setup a websocket in an endpoint #894

Open
1 task done
sassanh opened this issue Oct 17, 2024 · 2 comments
Open
1 task done

[🐛 Bug]: Can't setup a websocket in an endpoint #894

sassanh opened this issue Oct 17, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@sassanh
Copy link

sassanh commented Oct 17, 2024

next-on-pages environment related information

System:
        Platform: darwin
        Arch: arm64
        Version: Darwin Kernel Version 24.1.0: Mon Sep 30 00:10:31 PDT 2024; root:xnu-11215.40.63~39/RELEASE_ARM64_T6000
        CPU: (10) arm64 Apple M1 Pro
        Memory: 32 GB
        Shell: /bin/bash
Package Manager Used: npm (10.8.2)

Relevant Packages:
        @cloudflare/next-on-pages: 1.13.5
        vercel: N/A
        next: 14.2.15

Description

I can't setup websocket with the @cloudflare/next-on-pages after version 0.10.0

@dario-piotrowicz kindly provided this sample repository for running websocket inside next-on-pages projects:
https://github.com/dario-piotrowicz/next-app-websocket-pages.git

It was provided here

But it doesn't work with next-on-pages 0.10.0 or later locally and it doesn't work at all when deployed.

Reproduction

  1. Clone this repository: https://github.com/dario-piotrowicz/next-app-websocket-pages.git

  2. Change dependencies in package.json like this:

      "dependencies": {
        "@types/node": "22.7.6",
        "@types/react": "18.3.11",
        "@types/react-dom": "18.3.1",
        "next": "14.0.0",
        "react": "18.3.1",
        "react-dom": "18.3.1",
        "typescript": "5.6.3"
      },
      "devDependencies": {
        "@cloudflare/workers-types": "^4.20241011.0",
        "@cloudflare/next-on-pages": "==0.10.0",
        "vercel": "^28.0.0"
      }
  3. Run npx @cloudflare/next-on-pages

  4. Run wrangler pages dev .vercel/output/static --compatibility-flag=nodejs_compat

  5. Try to connect to the websocket, for example with websocat ws://localhost:8788/api/socket

  6. Now try the same with @cloudflare/next-on-pages==0.9.0 and it works.

  7. Try deploying both and none of them works when deployed with error:

{
"stack": " at ke (bundledWorker-0.8185137090843237.mjs:702:50)",
"name": "RangeError",
"message": "Responses may only be constructed with status codes in the range 200 to 599, inclusive.",
"timestamp": 1729141252885
}

Pages Deployment Method

None

Pages Deployment ID

https://b22ca0c8.next-app-websocket-pages.pages.dev

Additional Information

No response

Would you like to help?

  • Would you like to help fixing this bug?
@sassanh sassanh added the bug Something isn't working label Oct 17, 2024
@sassanh
Copy link
Author

sassanh commented Oct 17, 2024

Also the version that works locally, fails when deployed to cloudflare infrastructure with this error:

{
"stack": " at ke (bundledWorker-0.8185137090843237.mjs:702:50)",
"name": "RangeError",
"message": "Responses may only be constructed with status codes in the range 200 to 599, inclusive.",
"timestamp": 1729141252885
}

The version that doesn't work locally, generates the exact same error when deployed to cloudflare infrastructure.

It would be nice if we can have a sample repository that works (both locally and when deployed)

@sassanh
Copy link
Author

sassanh commented Nov 2, 2024

This seems to be where the error is being raised:

Just an observation: I can connect to the websocket if I remove resp = new Response(... and directly return resp, I can connect to the websocket.

Another observation: in a normal worker, if I try to recreate the response returned by DurableObject instance with new Response(response, {...response, status: response.status}) it fails with the same error, but if I remove status: response.status it will return status 200, which is not expected by websocket clients.

This is the stack of the error in the runner:
RangeError: Responses may only be constructed with status codes in the range 200 to 599, inclusive.
at handleApiRequest (index.js:4599:14)
at async index.js:4557:18
at async handleErrors (index.js:4228:12)
at async Object.fetch (index.js:4527:12)

It seems like creating responses with new Response(response.body, {...response, status: 101}) was something workers supported at some point but it has changed. Workers are able to return the responses with status code 101 returned by the DurableObjects without manipulation, but they can't create a Response object with that code.

I will create an issue in the workers repository too

relevant: https://www.answeroverflow.com/m/1166443215029809162
I don't know how it got resolved there, I don't think I can update anything on my side, it's probably something in Cloudflare infrastructure that needs to be updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant