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

Allow WebSockets connections to (re)use the HTTP(S) ports #578

Closed
essenciary opened this issue Sep 11, 2020 · 3 comments
Closed

Allow WebSockets connections to (re)use the HTTP(S) ports #578

essenciary opened this issue Sep 11, 2020 · 3 comments
Labels

Comments

@essenciary
Copy link

As discussed on Slack, this would be beneficial as:
1 - RFC 6455 states that WebSocket "is designed to work over HTTP ports 443 and 80 as well as to support HTTP proxies and intermediaries," thus making it compatible with the HTTP protocol. To achieve compatibility, the WebSocket handshake uses the HTTP Upgrade header[1] to change from the HTTP protocol to the WebSocket protocol.
2 - a different port on the same hostname is considered a different origin in CORS policy.
3 - no need to open additional ports through the firewall (especially tricky in corporate environments).

@fonsp
Copy link
Member

fonsp commented Mar 16, 2022

It is possible to reuse the same socket (i.e. port) for both HTTP traffic and websockets, by running a regular HTTP.serve(...) do http, and inside the body, use HTTP.WebSockets.is_upgrade(http.message) to check that it is an incoming WS connection. If so, call HTTP.WebSockets.upgrade(http) do ws.

Example from Pluto's source code (but a smaller minimal example would be great!): https://github.com/fonsp/Pluto.jl/blob/ac9e70af00b6fe72766adde034a0bfd70cd8d117/src/webserver/WebServer.jl#L154-L164

@quinnj
Copy link
Member

quinnj commented Jun 11, 2022

Yes @fonsp is right here; there's nothing preventing an HTTP server from serving both HTTP and websocket connections from the same port. On HTTP#master, this is done by calling WebSockets.isupgrade(http.message) and if true, calling WebSockets.upgrade(websocket_handler, http) to initialize the websocket connection.

@likev
Copy link

likev commented May 13, 2024

HTTP.serve doesn't work and HTTP.WebSockets.is_upgrade is renamed in HTTP@1, use HTTP.listen and HTTP.WebSockets.isupgrade(http.message) instead.
I create a pull request to add example code to documentation. #1179

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants