Skip to content

Commit

Permalink
Drab.Socket instead of Drab.Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
grych committed Feb 28, 2017
1 parent 0375713 commit b3b2d8c
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 109 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 0.3.0
Changes:
* depreciated `Drab.Endpoint`; Drab now injects in the `UserSocket` with `use Drab.Socket` (#8)
* move Commander setup to macros instead of use options
* before_handler callback (to run before each handler); stop processing when return false or nil
* after_handler, getting the return value of handler
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ $ npm install && node_modules/brunch/bin/brunch build
<script src="<%= static_path(@conn, "/js/app.js") %>"></script>
```

6. Initialize Drab websockets by adding the following to `lib/endpoint.ex`:
6. Initialize Drab sockets by adding the following to `web/channels/user_socket.ex`:

```elixir
use Drab.Endpoint
use Drab.Socket
```

Congratullations! You have installed Drab and you can proceed with your own Commanders.
Expand Down
3 changes: 1 addition & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
## 0.3.0
Changes:
* waiter functionality
* display information when handler die (like error 500 page), different for prod and dev
* allow user to create own channels in config.exs (#8), can't start with "/"
* display information when handler die (like error 500 page), different for prod and dev (Drab.Core)

## 0.4.0
Changes:
Expand Down
7 changes: 3 additions & 4 deletions lib/drab.ex
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ defmodule Drab do
waiting for server response
* `disable_controls_when_disconnected` (default: `true`) - disables control when there is no connectivity
between the browser and the server
* `socket` (default: `"/drab/socket"`) - path to Drab socket
* `socket` (default: `"/socket"`) - path to the socket where Drab operates
* `drab_store_storage` (default: :session_storage) - where to keep the Drab Store - :memory, :local_storage or
:session_storage; data in memory is kept to the next page load, session storage persist until browser (or a tab) is
closed, and local storage is kept forever
Expand All @@ -327,9 +327,8 @@ defmodule Drab do
disable_controls_while_processing: Application.get_env(:drab, :disable_controls_while_processing, true),
events_to_disable_while_processing: Application.get_env(:drab, :events_to_disable_while_processing, ["click"]),
disable_controls_when_disconnected: Application.get_env(:drab, :disable_controls_when_disconnected, true),
socket: Application.get_env(:drab, :socket, "/drab/socket"),
drab_store_storage: Application.get_env(:drab, :drab_store_storage, :session_storage),
additional_channels: Application.get_env(:drab, :additional_channels, [])
socket: Application.get_env(:drab, :socket, "/socket"),
drab_store_storage: Application.get_env(:drab, :drab_store_storage, :session_storage)
}
end
end
2 changes: 1 addition & 1 deletion lib/drab/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Drab.Channel do

use Phoenix.Channel

def join("drab:" <> url_path, _, socket) do
def join("__drab:" <> url_path, _, socket) do
# socket already contains controller and action
socket_with_path = socket |> assign(:url_path, url_path)

Expand Down
46 changes: 6 additions & 40 deletions lib/drab/endpoint.ex
Original file line number Diff line number Diff line change
@@ -1,50 +1,16 @@
defmodule Drab.Endpoint do
@moduledoc ~S"""
To enable drab, use the following clause in your projects `lib/endpoint.ex`:
use Drab.Endpoint
Depreciated. To enable Drab, `use Drab.Socket` in your `UserSocket` module.
"""

defmacro __using__(_options) do
IO.warn """
Injecting Drab into Endpoint is depreciated, Drab.Endpoint will be removed.
To enable Drab, `use Drab.Socket` in your `UserSocket` module.
""", Macro.Env.stacktrace(__ENV__)
quote do
# Module.register_attribute __MODULE__, :additional_channels, accumulate: true

# unquote do
# if options[:add_channels] do
# options[:add_channels] |> Enum.map(fn {channel, module} ->
# quote bind_quoted: [channel: channel, module: module] do
# # socket unquote(channel), unquote(module)
# @additional_channels {channel, module}
# end
# end)
# end
# end

socket Drab.config.socket, Drab.Socket
# do
# channel "drab:*", Drab.Channel
# end


# socket Drab.config.socket, Drab.Socket
end
end

# defmacro __before_compile__(env) do
# additional_channels = Module.get_attribute(env.module, :additional_channels)
# quote do
# def __additional_channels__, do: unquote(additional_channels)
# end

# end


# defmacro drab(channel, module) do
# quote do
# @additional_channels {unquote(channel), unquote(module)}
# end
# end

# def __additional_channels__() do
# @additional_channels
# end
end
86 changes: 29 additions & 57 deletions lib/drab/socket.ex
Original file line number Diff line number Diff line change
@@ -1,67 +1,39 @@
defmodule Drab.Socket do
# @external_resouce Mix.Project.config[:config_path]
@external_resource "/Users/grych/Dropbox/elixir/phoenix/drabrella/apps/drab_poc/config/config.exs"
@moduledoc false
IO.puts Mix.Project.config[:config_path]
@moduledoc """
Drab operates on websockets. To enable it, you should inject the Drab.Channel into your Socket module
(by default it is `UserSocket` in `web/channels/user_socket.ex`):
use Phoenix.Socket
require Logger
defmodule MyApp.UserSocket do
use Phoenix.Socket
use Drab.Socket
end
## Channels
channel "drab:*", Drab.Channel
IO.puts " ADD CHANNELS #{inspect Drab.config.additional_channels}"
Drab.config.additional_channels |> Enum.map(fn {name, module} ->
IO.puts "XXXXXx"
IO.puts name
IO.puts inspect(module)
case name do
"drab:" <> _ ->
Logger.error """
Channel name #{name} is restricted. This config entry for `additional_channels` will be ignored.
"""
_ ->
channel name, module
end
end)
This creates a channel "__drab:*" used by all Drab operations.
By default, Drab uses auto-generated socket with "/socket" path. In case of using different path, use config:
config :drab,
socket: "/my/socket"
"""

## Transports
transport :websocket, Phoenix.Transports.WebSocket
# transport :longpoll, Phoenix.Transports.LongPoll
defmacro __using__(_options) do
quote do
channel "__drab:*", Drab.Channel

# Socket params are passed from the client and can
# be used to verify and authenticate a user. After
# verification, you can put default assigns into
# the socket that will be set for all channels, ie
#
# {:ok, assign(socket, :user_id, verified_user_id)}
#
# To deny connection, return `:error`.
#
# See `Phoenix.Token` documentation for examples in
# performing token verification on connect.
def connect(%{"drab_return" => controller_and_action_token}, socket) do
case Phoenix.Token.verify(socket, "controller_and_action", controller_and_action_token) do
{:ok, controller_and_action} ->
[controller, action] = String.split(controller_and_action, "#")
{:ok , socket
|> assign(:controller, String.to_existing_atom(controller))
|> assign(:action, String.to_existing_atom(action))
}
{:error, _reason} -> :error
end
end

def connect(%{"drab_return" => controller_and_action_token}, socket) do
case Phoenix.Token.verify(socket, "controller_and_action", controller_and_action_token) do
{:ok, controller_and_action} ->
[controller, action] = String.split(controller_and_action, "#")
{:ok , socket
|> assign(:controller, String.to_existing_atom(controller))
|> assign(:action, String.to_existing_atom(action))
}
{:error, _reason} -> :error
end
end
def connect(_params, _socket), do: :error

# Socket id's are topics that allow you to identify all sockets for a given user:
#
# def id(socket), do: "users_socket:#{socket.assigns.user_id}"
#
# Would allow you to broadcast a "disconnect" event and terminate
# all active sockets and channels for a given user:
#
# DrabPoc.Endpoint.broadcast("users_socket:#{user.id}", "disconnect", %{})
#
# Returning `nil` makes this socket anonymous.
def id(_socket), do: nil
end
2 changes: 1 addition & 1 deletion priv/templates/drab/drab.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

this.socket = new this.Socket("<%= Drab.config.socket %>", {params: {drab_return: drab_return_token}})
this.socket.connect()
this.channel = this.socket.channel("drab:" + this.path, {})
this.channel = this.socket.channel("__drab:" + this.path, {})

this.channel.join()
.receive("error", function(resp) {
Expand Down
3 changes: 1 addition & 2 deletions test/drab_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ defmodule DrabTest do
assert Drab.config[:disable_controls_while_processing] == true
assert Drab.config[:events_to_disable_while_processing] == ["click"]
assert Drab.config[:disable_controls_when_disconnected] == true
assert Drab.config[:socket] == "/drab/socket"
assert Drab.config[:socket] == "/socket"
assert Drab.config[:drab_store_storage] == :session_storage
assert Drab.config[:additional_channels] == []
end

describe "helpers" do
Expand Down

0 comments on commit b3b2d8c

Please sign in to comment.