Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Phoenix LiveReact
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
さっちゃん
June 30, 2019
Programming
570
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Phoenix LiveReact
#tokyoex
https://github.com/ne-sachirou/phoenix_react_realtime_example
さっちゃん
June 30, 2019
More Decks by さっちゃん
See All by さっちゃん
火星曆
ne_sachirou
0
27
みんなのオブザーバビリティプラットフォームを作ってるんだがパフォーマンスがやばい #mackerelio #srenext
ne_sachirou
0
1.7k
作ってよかったgraceful shutdownライブラリ #kyotogo
ne_sachirou
0
1.4k
path 依存型って何?
ne_sachirou
0
830
野生の onbording と onbording 設計 #kyototechtalk
ne_sachirou
0
730
メトリックはいかにして見え續ける樣になったか #devio2022
ne_sachirou
0
130
名實一致
ne_sachirou
0
740
まかれるあなとみあ ―Mackerel のしくみを理解する 30 分― @ Hatena Engineer Seminar #16
ne_sachirou
0
3.3k
tacit programming : Point-free, Concatenatives & J
ne_sachirou
0
1.1k
Other Decks in Programming
See All in Programming
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
250
TAKTでAI駆動開発の品質を設計する
j5ik2o
7
1.3k
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.2k
肥大化するレガシーコードに立ち向かうためのインターフェース分離と依存の逆転 / JJUG CCC 2026 Spring
hirokunimaeta
0
550
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
110
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
650
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.7k
AIだと陥りがちなJakarta EE最新技術への移行時の落とし穴と解決策
tnagao7
0
110
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
180
軽量Java基盤の設計 DIコンテナに頼らない、長期保守と1秒起動の実現 JJUG CCC 2026 Spring
macha64
0
520
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.4k
ふつうのFeature Flag実践入門
irof
7
3.9k
Featured
See All Featured
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
62
44k
A Tale of Four Properties
chriscoyier
163
24k
Discover your Explorer Soul
emna__ayadi
2
1.1k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
380
Product Roadmaps are Hard
iamctodd
PRO
55
12k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.8k
Unsuck your backbone
ammeep
672
58k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
310
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
250
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
160
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
1
350
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3.4k
Transcript
Phoenix LiveReact
.。oO(さっちゃんですよヾ(〃l _ l)ノ゙ ☆)
Phoenix LiveView https://github.com/phoenixframework/phoenix_live_view
Phoenix LiveView: Interactive, Real-Time Apps. No Need to Write JavaScript.
https://dockyard.com/blog/2018/12/12/phoenix-liveview- interactive-real-time-apps-no-need-to-write-javascript
_⼈⼈⼈⼈⼈⼈⼈⼈⼈⼈⼈⼈⼈⼈⼈⼈_ > No Need to Write JavaScript. <  ̄Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y ̄
Write Elixir. defmodule ExampleWeb.CounterLive do use Phoenix.LiveView def render(assigns) do
~L""" Count: <%= @count %> """ end def mount(%{}, socket) do if connected?(socket), do: :timer.send_interval(1000, self(), :count_up) {:ok, assign(socket, :count, 0)} end def handle_info(:count_up, socket) do count = socket.assigns.count {:noreply, assign(socket, :count, count + 1)} end end
Write EEx. <%= Phoenix.LiveView.live_render(@conn, ExampleWeb.CounterLive, session: %{}) %> Done!
No Need to Write JavaScript?
Taking the principle of excluded middle from the mathematician would
be the same, say, as proscribing the telescope to the astronomer or to the boxer the use of his fists. Hilbert (1927)
Taking the JavaScript from the Web UI would be the
same, say, as proscribing the principle of excluded middle from the mathematician.
UI and the changes of UI are difficult. We can't
escape from JavaScript.
If there is a useChannel React hook…
Improving UX with Phoenix Channels & React Hooks – Flatiron
Labs – Medium https://medium.com/flatiron-labs/improving-ux-with- phoenix-channels-react-hooks-8e661d3a771e
defmodule ExampleWeb.RoomChannel do use ExampleWeb, :channel def join("room:lobby", payload, socket)
do socket = assign(socket, :count, 0) :timer.send_interval(1000, self(), :count_up) {:ok, socket} end def handle_info(:count_up, socket) do count = socket.assigns.count + 1 socket = assign(socket, :count, count) push(socket, "count_up", %{count: count}) {:noreply, socket} end end
import React from "react"; import ReactDOM from "react-dom"; export default
function main() { ReactDOM.render( <SocketProvider wsUrl={"/socket"} options={{ token: window.userToken }}> <App /> </SocketProvider>, document.getElementById("app") ); }
import { Socket } from "phoenix"; import { createContext, useEffect
} from "react"; const SocketContext = createContext(); function SocketProvider({ wsUrl, options, children }) { const socket = new Socket(wsUrl, { params: options }); useEffect(() => { socket.connect(); }, [options, wsUrl]); return ( <SocketContext.Provider value={socket}>{children}</SocketContext.Provider> ); } SocketProvider.defaultProps = { options: {} };
import { useContext, useEffect, useReducer } from "react"; function useChannel(channelTopic,
reducer, initialState) { const socket = useContext(SocketContext); const [state, dispatch] = useReducer(reducer, initialState); useEffect(() => { const channel = socket.channel(channelTopic, {}); channel.onMessage = (event, payload) => { dispatch({ event, payload }); return payload; }; channel.join(); return () => { channel.leave(); }; }, [channelTopic]); return state; }
function appReducer(state, { event, payload }) { switch (event) {
case "count_up": return { ...state, count: payload.count }; default: return state; } } function App(props) { const initialState = { count: 0 }; const { count } = useChannel("room:lobby", appReducer, initialState); return <div>Count: {count}</div>; } Done!
use-phoenix-channel - npm https://www.npmjs.com/package/use-phoenix-channel