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
さっちゃん
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
17
火星曆
ne_sachirou
0
43
みんなのオブザーバビリティプラットフォームを作ってるんだがパフォーマンスがやばい #mackerelio #srenext
ne_sachirou
0
1.7k
作ってよかったgraceful shutdownライブラリ #kyotogo
ne_sachirou
0
1.4k
path 依存型って何?
ne_sachirou
0
860
野生の onbording と onbording 設計 #kyototechtalk
ne_sachirou
0
750
メトリックはいかにして見え續ける樣になったか #devio2022
ne_sachirou
0
140
名實一致
ne_sachirou
0
750
まかれるあなとみあ ―Mackerel のしくみを理解する 30 分― @ Hatena Engineer Seminar #16
ne_sachirou
0
3.3k
Other Decks in Programming
See All in Programming
数百円から始めるRuby電子工作
tarosay
0
120
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
200
そこに3びきプロダクトがいるじゃろう——生成AI時代における“価値が届かない理由”の構造
kosuket
0
380
PHP に部分適用が来るぞ!……ところで何それ?おいしいの? #phpcon / phpcon-2026
shogogg
0
480
FDEが実現するAI駆動経営の現在地
gonta
2
250
Google Apps Script で Ruby を動かす
kawahara
0
120
属人化した知識を、 AIが辿れる地図にする
pkshadeck
PRO
1
130
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
200
Prismを使った型安全な暗号化_関数型まつり2026
_fhhmm
0
160
Foundation Models frameworkで画像分析
ryodeveloper
1
320
言語を使う側から、作る側へ。 自作 Lisp で得た新たな気づき。
andpad
0
140
Welcome to the "Parametricity" 🏙️ − Generic だけど Specific な世界 −
guvalif
PRO
1
200
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.7k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
450
Context Engineering - Making Every Token Count
addyosmani
9
1k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
160
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
Ruling the World: When Life Gets Gamed
codingconduct
0
290
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.1k
Accessibility Awareness
sabderemane
1
170
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
590
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