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

wip: RealtimeWeb Socket macro #628

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/realtime_web/channels/user_socket.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule RealtimeWeb.UserSocket do
use Phoenix.Socket
use RealtimeWeb.Socket

require Logger

Expand Down
55 changes: 55 additions & 0 deletions lib/realtime_web/socket.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
defmodule RealtimeWeb.Socket do

Check warning on line 1 in lib/realtime_web/socket.ex

View workflow job for this annotation

GitHub Actions / Formatting Checks

Modules should have a @moduledoc tag.
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 drainer_spec(opts) do
Phoenix.Socket.__drainer_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
# count incoming message depending on the messag event
Phoenix.Socket.__in__(message, state)
end

@doc false
def handle_info(
{:socket_push, :text, [_, _, _, [_, [_ | event], _] | _]} = message,
state
)
when event in ["broadcast", "presence_state", "presence_diff", "postgres_changes"] do
# count outgoing message
Phoenix.Socket.__info__(message, state)
end

@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)
end
end
end
Loading