Skip to content

Commit

Permalink
Update Phoenix and add LiveView
Browse files Browse the repository at this point in the history
  • Loading branch information
bgottlob committed Jan 16, 2021
1 parent 5fbf07e commit ef07336
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 17 deletions.
3 changes: 3 additions & 0 deletions apps/apex_dash/assets/assets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/apex_dash/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ import "phoenix_html"
// Local files can be imported directly using relative
// paths "./socket" or full ones "web/static/js/socket".

// import socket from "./socket"
import socket from "./socket"
9 changes: 8 additions & 1 deletion apps/apex_dash/assets/js/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
// To use Phoenix channels, the first step is to import Socket
// and connect at the socket path in "lib/web/endpoint.ex":
import {Socket} from "phoenix"
import LiveSocket from "phoenix_live_view"

let socket = new Socket("/socket", {params: {token: window.userToken}})
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}})
liveSocket.connect()

//let socket = new Socket("/socket", {params: {token: window.userToken}})

// When you connect, you'll often need to authenticate the client.
// For example, imagine you have an authentication plug, `MyAuth`,
Expand Down Expand Up @@ -51,6 +56,7 @@ let socket = new Socket("/socket", {params: {token: window.userToken}})
// Finally, pass the token on connect as below. Or remove it
// from connect if you don't care about authentication.

/*
socket.connect()
// Now that you are connected, you can join channels with a topic:
Expand All @@ -60,3 +66,4 @@ channel.join()
.receive("error", resp => { console.log("Unable to join", resp) })
export default socket
*/
3 changes: 3 additions & 0 deletions apps/apex_dash/assets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion apps/apex_dash/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
},
"dependencies": {
"phoenix": "file:../../../deps/phoenix",
"phoenix_html": "file:../../../deps/phoenix_html"
"phoenix_html": "file:../../../deps/phoenix_html",
"phoenix_live_view": "file:../../../deps/phoenix_live_view"
},
"devDependencies": {
"babel-brunch": "6.1.1",
Expand Down
6 changes: 4 additions & 2 deletions apps/apex_dash/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# is restricted to this project.
use Mix.Config

config :phoenix, :json_library, Jason

# General application configuration
config :apex_dash,
namespace: ApexDash
Expand All @@ -14,8 +16,8 @@ config :apex_dash, ApexDashWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "ZiAsbWn2nzouu36Ta3JR8jmCqz9Gycztbe8ppVYKRAFs+feb0nA/IizGPvlFBWZ5",
render_errors: [view: ApexDashWeb.ErrorView, accepts: ~w(html json)],
pubsub: [name: ApexDash.PubSub,
adapter: Phoenix.PubSub.PG2]
pubsub_server: ApexDash.PubSub,
live_view: [signing_salt: "kCzxEq4RWot7tflnE6589Ty//HhJdfa4"]

# Configures Elixir's Logger
config :logger, :console,
Expand Down
1 change: 1 addition & 0 deletions apps/apex_dash/lib/apex_dash/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule ApexDash.Application do
supervisor(ApexDashWeb.Endpoint, []),
# Start your own worker by calling: ApexDash.Worker.start_link(arg1, arg2, arg3)
# worker(ApexDash.Worker, [arg1, arg2, arg3]),
{Phoenix.PubSub, [name: ApexDash.PubSub, adapter: Phoenix.PubSub.PG2]}
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
4 changes: 0 additions & 4 deletions apps/apex_dash/lib/apex_dash_web/channels/user_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ defmodule ApexDashWeb.UserSocket do
## Channels
# channel "room:*", ApexDashWeb.RoomChannel

## Transports
transport :websocket, Phoenix.Transports.WebSocket
# transport :longpoll, Phoenix.Transports.LongPoll

# 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
Expand Down
20 changes: 20 additions & 0 deletions apps/apex_dash/lib/apex_dash_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,23 @@ defmodule ApexDashWeb.PageController do
render conn, "index.html"
end
end

defmodule ApexDashWeb.DashboardLive do
use Phoenix.LiveView

def render(assigns) do
~L"""
RPM: <%= @rpm %>
"""
end

def mount(_params, _session, socket) do
if connected?(socket), do: :timer.send_interval(300, self(), :update)
rpm = :rand.uniform(9000)
{:ok, assign(socket, :rpm, rpm)}
end

def handle_info(:update, socket) do
{:noreply, assign(socket, :rpm, :rand.uniform(9000))}
end
end
4 changes: 3 additions & 1 deletion apps/apex_dash/lib/apex_dash_web/endpoint.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule ApexDashWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :apex_dash

socket "/live", Phoenix.LiveView.Socket

socket "/socket", ApexDashWeb.UserSocket

# Serve at "/" the static files from "priv/static" directory.
Expand All @@ -24,7 +26,7 @@ defmodule ApexDashWeb.Endpoint do
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Poison
json_decoder: Jason

plug Plug.MethodOverride
plug Plug.Head
Expand Down
2 changes: 2 additions & 0 deletions apps/apex_dash/lib/apex_dash_web/router.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule ApexDashWeb.Router do
use ApexDashWeb, :router
import Phoenix.LiveView.Router

pipeline :browser do
plug :accepts, ["html"]
Expand All @@ -17,6 +18,7 @@ defmodule ApexDashWeb.Router do
pipe_through :browser # Use the default browser stack

get "/", PageController, :index
live "/live", DashboardLive
end

# Other scopes may use custom stacks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<%= csrf_meta_tag() %>

<title>Hello ApexDash!</title>
<link rel="stylesheet" href="<%= static_path(@conn, "/css/app.css") %>">
Expand All @@ -26,7 +27,7 @@
<p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p>

<main role="main">
<%= render @view_module, @view_template, assigns %>
<%= @inner_content %>
</main>

</div> <!-- /container -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<div class="jumbotron">
<h2><%= gettext "Welcome to %{name}!", name: "Phoenix" %></h2>
<p class="lead">A productive web framework that<br />does not compromise speed and maintainability.</p>

<%= live_render(@conn, ApexDashWeb.DashboardLive) %>
</div>

<div class="row marketing">
Expand Down
1 change: 1 addition & 0 deletions apps/apex_dash/lib/apex_dash_web/views/page_view.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
defmodule ApexDashWeb.PageView do
use ApexDashWeb, :view
import Phoenix.LiveView.Helpers
end
15 changes: 9 additions & 6 deletions apps/apex_dash/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ defmodule ApexDash.Mixfile do
# Type `mix help deps` for examples and options.
defp deps do
[
{:phoenix, "~> 1.3.4"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_html, "~> 2.10"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:plug_cowboy, "~> 1.0"}
{:phoenix, "~> 1.5.1"},
{:phoenix_pubsub, "~> 2.0.0"},
{:phoenix_html, "~> 2.14.2"},
{:phoenix_live_reload, "~> 1.2.2", only: :dev},
{:gettext, "~> 0.18.0"},
{:plug_cowboy, "~> 2.2.1"},
#{:apex, in_umbrella: true},
{:phoenix_live_view, "~> 0.12.1"},
{:jason, "~> 1.2.1"}
]
end
end

0 comments on commit ef07336

Please sign in to comment.