Jump. CredoChecks. LiveViewPubSubRequiresConnected
(Jump.CredoChecks v0.5.0)
View Source
Basics
This check is disabled by default.
Learn how to enable it via .credo.exs.
This check has a base priority of high and works with any version of Elixir.
Explanation
LiveView mount/3 runs twice: once for the disconnected static render and
once when the WebSocket connects. Phoenix.PubSub subscriptions in the
disconnected mount attach to a process that is about to die, so they should
only run when the socket is connected.
# ❌ Bad — subscribes during the disconnected mount *and* after the WebSocket connects
def mount(_params, _session, socket) do
PubSub.subscribe("my-topic")
{:ok, socket}
end
# ✅ Good — only subscribes once the LiveView is connected
def mount(_params, _session, socket) do
if connected?(socket) do
PubSub.subscribe("my-topic")
end
{:ok, socket}
endCheck-Specific Parameters
Use the following parameters to configure this check:
:custom_pubsub_functions
Additional functions that wrap Phoenix.PubSub.subscribe/{2,3}.
Each entry is {module, function} for qualified calls, {function, arity}
for local/imported calls, or {module, function, arity} to also constrain
arity.
Example:
{Jump.CredoChecks.LiveViewPubSubRequiresConnected,
custom_pubsub_functions: [
{MyAppWeb.PubSub, :subscribe},
{:subscribe_user_events, 1}
]}...would match:
MyAppWeb.PubSub.subscribe("topic")
subscribe_user_events(socket)This parameter defaults to [].
General Parameters
Like with all checks, general params can be applied.
Parameters can be configured via the .credo.exs config file.