@@ -746,7 +746,7 @@ defmodule Phoenix.Endpoint do
746746
747747 paths =
748748 if websocket do
749- websocket = put_auth_token ( websocket , opts [ :auth_token ] )
749+ websocket = put_socket_opts ( websocket , opts )
750750 config = Phoenix.Socket.Transport . load_config ( websocket , Phoenix.Transports.WebSocket )
751751 plug_init = { endpoint , socket , config }
752752 { conn_ast , match_path } = socket_path ( path , config )
@@ -757,7 +757,7 @@ defmodule Phoenix.Endpoint do
757757
758758 paths =
759759 if longpoll do
760- longpoll = put_auth_token ( longpoll , opts [ :auth_token ] )
760+ longpoll = put_socket_opts ( longpoll , opts )
761761 config = Phoenix.Socket.Transport . load_config ( longpoll , Phoenix.Transports.LongPoll )
762762 plug_init = { endpoint , socket , config }
763763 { conn_ast , match_path } = socket_path ( path , config )
@@ -769,8 +769,25 @@ defmodule Phoenix.Endpoint do
769769 paths
770770 end
771771
772- defp put_auth_token ( true , enabled ) , do: [ auth_token: enabled ]
773- defp put_auth_token ( opts , enabled ) , do: Keyword . put ( opts , :auth_token , enabled )
772+ # Copies top-level socket options into the transport config,
773+ # which is what the socket receives on connect.
774+ defp put_socket_opts ( true , opts ) , do: put_socket_opts ( [ ] , opts )
775+
776+ defp put_socket_opts ( transport_opts , opts ) do
777+ transport_opts = Keyword . put ( transport_opts , :auth_token , opts [ :auth_token ] )
778+
779+ case Keyword . fetch ( opts , :max_channels_per_transport ) do
780+ { :ok , max } when is_integer ( max ) and max > 0 ->
781+ Keyword . put ( transport_opts , :max_channels_per_transport , max )
782+
783+ { :ok , other } ->
784+ raise ArgumentError ,
785+ ":max_channels_per_transport must be a positive integer, got: #{ inspect ( other ) } "
786+
787+ :error ->
788+ transport_opts
789+ end
790+ end
774791
775792 defp socket_path ( path , config ) do
776793 end_path_fragment = Keyword . fetch! ( config , :path )
@@ -865,6 +882,15 @@ defmodule Phoenix.Endpoint do
865882
866883 Custom transports might implement their own mechanism.
867884
885+ * `:max_channels_per_transport` - the maximum number of channels that may be
886+ joined per transport process. Overrides the value given to `use Phoenix.Socket`,
887+ which defaults to `100`. This is useful for sockets you don't define yourself,
888+ such as `Phoenix.LiveView.Socket`:
889+
890+ socket "/live", Phoenix.LiveView.Socket,
891+ websocket: [connect_info: [session: @session_options]],
892+ max_channels_per_transport: 200
893+
868894 You can also pass the options below on `use Phoenix.Socket`.
869895 The values specified here override the value in `use Phoenix.Socket`.
870896
0 commit comments