Skip to content

Instantly share code, notes, and snippets.

@bismark
Created March 29, 2022 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bismark/c827cfc6b4b145d5038cca889f747b83 to your computer and use it in GitHub Desktop.
Save bismark/c827cfc6b4b145d5038cca889f747b83 to your computer and use it in GitHub Desktop.
Phoenix Socket module with overridable callbacks
defmodule MyApp.Socket do
@moduledoc """
This module extracts the __using__ macro from Phoenix.Socket and makes the
Phoenix.Socket.Transport callbacks overridable
"""
defmacro __using__(opts) do
quote do
## User API
import Phoenix.Socket
@behaviour Phoenix.Socket
@before_compile Phoenix.Socket
Module.register_attribute(__MODULE__, :phoenix_channels, accumulate: true)
@phoenix_socket_options unquote(opts)
## Callbacks
@behaviour Phoenix.Socket.Transport
@doc false
def child_spec(opts) do
Phoenix.Socket.__child_spec__(__MODULE__, opts, @phoenix_socket_options)
end
@doc false
def connect(map), do: Phoenix.Socket.__connect__(__MODULE__, map, @phoenix_socket_options)
@doc false
def init(state), do: Phoenix.Socket.__init__(state)
@doc false
def handle_in(message, state), do: Phoenix.Socket.__in__(message, state)
@doc false
def handle_info(message, state), do: Phoenix.Socket.__info__(message, state)
@doc false
def terminate(reason, state), do: Phoenix.Socket.__terminate__(reason, state)
defoverridable Phoenix.Socket.Transport
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment