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

Websock support #5063

Merged
merged 7 commits into from Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions integration_test/mix.lock
Expand Up @@ -47,4 +47,6 @@
"telemetry": {:hex, :telemetry, "1.1.0", "a589817034a27eab11144ad24d5c0f9fab1f58173274b1e9bae7074af9cbee51", [:rebar3], [], "hexpm", "b727b2a1f75614774cff2d7565b64d0dfa5bd52ba517f16543e6fc7efcc0df48"},
"telemetry_metrics": {:hex, :telemetry_metrics, "0.6.1", "315d9163a1d4660aedc3fee73f33f1d355dcc76c5c3ab3d59e76e3edf80eef1f", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7be9e0871c41732c233be71e4be11b96e56177bf15dde64a8ac9ce72ac9834c6"},
"telemetry_poller": {:hex, :telemetry_poller, "1.0.0", "db91bb424e07f2bb6e73926fcafbfcbcb295f0193e0a00e825e589a0a47e8453", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b3a24eafd66c3f42da30fc3ca7dda1e9d546c12250a2d60d7b81d264fbec4f6e"},
"websock": {:hex, :websock, "0.4.3", "184ac396bdcd3dfceb5b74c17d221af659dd559a95b1b92041ecb51c9b728093", [:mix], [], "hexpm", "5e4dd85f305f43fd3d3e25d70bec4a45228dfed60f0f3b072d8eddff335539cf"},
"websock_adapter": {:hex, :websock_adapter, "0.4.3", "30dd07a9fabf704845baf0176b3e8508d3468539422ffe9ff4229fa39b6277c9", [:mix], [{:bandit, "~> 0.5.9", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14.0", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:websock, "~> 0.4.3", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "304bf61322080bc0e6ea341263b63097f0c801af4638a86f264287ea8fd3a9ff"},
}
22 changes: 0 additions & 22 deletions lib/phoenix/endpoint/cowboy2_adapter.ex
Expand Up @@ -137,26 +137,4 @@ defmodule Phoenix.Endpoint.Cowboy2Adapter do
defp port_to_integer({:system, env_var}), do: port_to_integer(System.get_env(env_var))
defp port_to_integer(port) when is_binary(port), do: String.to_integer(port)
defp port_to_integer(port) when is_integer(port), do: port

@doc false
def websocket_upgrade(conn, handler, state, opts) do
cowboy_opts =
opts
|> Enum.flat_map(fn
{:timeout, timeout} -> [idle_timeout: timeout]
{:compress, _} = opt -> [opt]
{:max_frame_size, _} = opt -> [opt]
_other -> []
end)
|> Map.new()

process_flags =
opts
|> Keyword.take([:fullsweep_after])
|> Map.new()

handler_args = {handler, process_flags, state}
upgrade_args = {Phoenix.Endpoint.Cowboy2Handler, handler_args, cowboy_opts}
Plug.Conn.upgrade_adapter(conn, :websocket, upgrade_args)
end
end
83 changes: 0 additions & 83 deletions lib/phoenix/endpoint/cowboy2_handler.ex

This file was deleted.

19 changes: 14 additions & 5 deletions lib/phoenix/transports/websocket.ex
@@ -1,5 +1,18 @@
defmodule Phoenix.Transports.WebSocket do
@moduledoc false
#
# How WebSockets Work In Phoenix
#
# WebSocket support in Phoenix is implemented on top of the `WebSockAdapter` library. Upgrade
# requests from clients originate as regular HTTP requests that get routed to this module via
# Plug. These requests are then upgraded to WebSocket connections via
# `WebSockAdapter.upgrade/4`, which takes as an argument the handler for a given socket endpoint
# as configured in the application's Endpoint. This handler module must implement the
# transport-agnostic `Phoenix.Socket.Transport` behaviour (this same behaviour is also used for
# other transports such as long polling). Because this behaviour is a superset of the `WebSock`
# behaviour, the `WebSock` library is able to use the callbacks in the `WebSock` behaviour to
# call this handler module directly for the rest of the WebSocket connection's lifetime.
#
@behaviour Plug

import Plug.Conn
Expand Down Expand Up @@ -44,12 +57,8 @@ defmodule Phoenix.Transports.WebSocket do

case handler.connect(config) do
{:ok, arg} ->
upgrade =
conn.private[:phoenix_websocket_upgrade] ||
(&Phoenix.Endpoint.Cowboy2Adapter.websocket_upgrade/4)

conn
|> upgrade.(handler, arg, opts)
|> WebSockAdapter.upgrade(handler, arg, opts)
|> halt()

:error ->
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Expand Up @@ -72,9 +72,9 @@ defmodule Phoenix.MixProject do
{:plug_crypto, "~> 1.2"},
{:telemetry, "~> 0.4 or ~> 1.0"},
{:phoenix_pubsub, "~> 2.1"},

{:phoenix_view, "~> 2.0", optional: true},
{:phoenix_template, "~> 1.0"},
{:websock_adapter, "~> 0.4"},

# TODO drop castore when we require OTP 25+
{:castore, ">= 0.0.0"},
Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Expand Up @@ -34,4 +34,6 @@
"telemetry": {:hex, :telemetry, "1.1.0", "a589817034a27eab11144ad24d5c0f9fab1f58173274b1e9bae7074af9cbee51", [:rebar3], [], "hexpm", "b727b2a1f75614774cff2d7565b64d0dfa5bd52ba517f16543e6fc7efcc0df48"},
"telemetry_metrics": {:hex, :telemetry_metrics, "0.6.1", "315d9163a1d4660aedc3fee73f33f1d355dcc76c5c3ab3d59e76e3edf80eef1f", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7be9e0871c41732c233be71e4be11b96e56177bf15dde64a8ac9ce72ac9834c6"},
"telemetry_poller": {:hex, :telemetry_poller, "1.0.0", "db91bb424e07f2bb6e73926fcafbfcbcb295f0193e0a00e825e589a0a47e8453", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b3a24eafd66c3f42da30fc3ca7dda1e9d546c12250a2d60d7b81d264fbec4f6e"},
"websock": {:hex, :websock, "0.4.3", "184ac396bdcd3dfceb5b74c17d221af659dd559a95b1b92041ecb51c9b728093", [:mix], [], "hexpm", "5e4dd85f305f43fd3d3e25d70bec4a45228dfed60f0f3b072d8eddff335539cf"},
"websock_adapter": {:hex, :websock_adapter, "0.4.3", "30dd07a9fabf704845baf0176b3e8508d3468539422ffe9ff4229fa39b6277c9", [:mix], [{:bandit, "~> 0.5.9", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14.0", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:websock, "~> 0.4.3", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "304bf61322080bc0e6ea341263b63097f0c801af4638a86f264287ea8fd3a9ff"},
}