Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Proxy websocket connections when using authenticated (realish) preview #2135

Merged
merged 2 commits into from
Nov 19, 2021

Conversation

jyn514
Copy link
Contributor

@jyn514 jyn514 commented Nov 16, 2021

Previously, Wrangler would return a "101 Switching Protocols" response
and then immediately close the TCP connection. This changes it to instead
continue proxying the connection to the remote worker.

This is simpler than wrangler dev --inspect (and uses a different codepath) because it only
proxies the TCP connection directly rather than trying to inspect each websocket message.

How do I use this?

  1. Create a worker that uses WebSockets. I used https://github.com/cloudflare/websocket-template with the protocol in template.js changed from wss to ws.
  2. Run wrangler dev http http.

Note that wrangler dev (i.e. with the default of https for the upstream connection) is currently broken. I am looking into this internally; it's an issue with the Cloudflare edge network and not with wrangler.

Unauthenticated preview is not currently supported.

Helps with #2079 and #1910.

Returning a `Request` instead of a future allows passing the request to tungstenite to open a
websocket.  This also changes the `Request` parameter to be a parts and body, to avoid needless
`Request::from_parts/to_parts` calls.
@jyn514 jyn514 requested a review from a team as a code owner November 16, 2021 21:12
Previously, Wrangler would return a "101 Switching Protocols" request
and then immediately close the TCP connection. This changes it to instead
continue proxying the connection to the remote worker.

This is simpler than `wrangler dev --inspect` (and uses a different codepath) because it only
proxies the TCP connection directly rather than trying to inspect each websocket message.

 # How do I use this?

1. Create a worker that uses WebSockets. I used https://github.com/cloudflare/websocket-template with the `protocol` in template.js changed from `wss` to `ws`.
2. Run `wrangler dev http http`.

Note that `wrangler dev` (i.e. with the default of `https` for the upstream connection) requires you
to first deploy the worker at least once. Without deploying, the remote worker won't receive the
`Connection: Upgrade` and `Upgrade: Websocket` headers.

Unauthenticated preview is not currently supported.
Copy link
Contributor

@Electroid Electroid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉
Note: this will only work with HTTP, not HTTPS.

@Electroid Electroid merged commit e44ab18 into master Nov 19, 2021
@delete-merged-branch delete-merged-branch bot deleted the jnelson/websockets branch November 19, 2021 00:06
@threepointone threepointone mentioned this pull request Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants