Skip to content

Commit d364e18

Browse files
committed
Consider a proper socket callback for starting sockets
1 parent cb56e85 commit d364e18

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

lib/phoenix/endpoint.ex

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,13 @@ defmodule Phoenix.Endpoint do
360360

361361
# Channels
362362

363+
@doc """
364+
Provide module specs for sockets to start and drain with the endpoint.
365+
366+
See `Phoenix.Socket.Transport` for more information.
367+
"""
368+
@callback sockets() :: [{module(), keyword()} | module()]
369+
363370
@doc """
364371
Subscribes the caller to the given topic.
365372
@@ -406,15 +413,13 @@ defmodule Phoenix.Endpoint do
406413
"""
407414
@callback local_broadcast_from(from :: pid, topic, event, msg) :: :ok
408415

416+
@optional_callbacks sockets: 0
417+
409418
@doc false
410419
defmacro __using__(opts) do
411420
quote do
412421
@behaviour Phoenix.Endpoint
413422

414-
def __start_sockets__ do
415-
unquote(Keyword.get(opts, :start_sockets, []))
416-
end
417-
418423
unquote(config(opts))
419424
unquote(pubsub())
420425
unquote(plug())

lib/phoenix/endpoint/supervisor.ex

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,14 @@ defmodule Phoenix.Endpoint.Supervisor do
119119
end
120120

121121
defp socket_children(endpoint, fun) do
122-
for {socket, opts} <- endpoint.__start_sockets__(),
122+
sockets =
123+
if function_exported?(endpoint, :sockets, 0) do
124+
endpoint.sockets()
125+
else
126+
[]
127+
end
128+
129+
for {socket, opts} <- Enum.map(sockets, &normalize_module_spec/1),
123130
# TODO is this the correct place for this?
124131
# Needs to know transport specific config
125132
# _ = check_origin_or_csrf_checked!(conf, opts),
@@ -129,6 +136,9 @@ defmodule Phoenix.Endpoint.Supervisor do
129136
end
130137
end
131138

139+
defp normalize_module_spec(module) when is_atom(module), do: {module, []}
140+
defp normalize_module_spec({module, kw}) when is_atom(module) and is_list(kw), do: {module, kw}
141+
132142
defp apply_or_ignore(socket, fun, args) do
133143
# If the module is not loaded, we want to invoke and crash
134144
if not Code.ensure_loaded?(socket) or function_exported?(socket, fun, length(args)) do

test/phoenix/integration/websocket_channels_test.exs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,14 @@ defmodule Phoenix.Integration.WebSocketChannelsTest do
214214

215215
defmodule Endpoint do
216216
use Phoenix.Endpoint,
217-
otp_app: :phoenix,
218-
start_sockets: [
217+
otp_app: :phoenix
218+
219+
def sockets do
220+
[
219221
{UserSocket, []},
220222
{UserSocketConnectInfo, []}
221223
]
224+
end
222225

223226
plug Plug.Session,
224227
store: :cookie,

test/phoenix/integration/websocket_socket_test.exs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,14 @@ defmodule Phoenix.Integration.WebSocketTest do
115115

116116
defmodule Endpoint do
117117
use Phoenix.Endpoint,
118-
otp_app: :phoenix,
119-
start_sockets: [
118+
otp_app: :phoenix
119+
120+
def sockets do
121+
[
120122
{UserSocket, custom: :value},
121123
{PingSocket, []}
122124
]
125+
end
123126

124127
plug Router
125128
end

0 commit comments

Comments
 (0)