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
34
みんなのオブザーバビリティプラットフォームを作ってるんだがパフォーマンスがやばい #mackerelio #srenext
ne_sachirou
0
1.7k
作ってよかったgraceful shutdownライブラリ #kyotogo
ne_sachirou
0
1.4k
path 依存型って何?
ne_sachirou
0
840
野生の onbording と onbording 設計 #kyototechtalk
ne_sachirou
0
740
メトリックはいかにして見え續ける樣になったか #devio2022
ne_sachirou
0
130
名實一致
ne_sachirou
0
750
まかれるあなとみあ ―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
Performance Engineering for Everyone
elenatanasoiu
0
260
ローカルLLMでどこまでコードが書けるか -縮小版 / How much code can be written on a local LLM Shortened
kishida
2
180
AI 輔助遺留系統現代化的經驗分享
jame2408
1
1.2k
はてなアカウント基盤 State of the Union
cockscomb
1
1.3k
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
830
Vue × Nuxt × Oxc どこまで使える?実運用の現在地
andpad
0
360
Strategic Design in the Frontend: Moduliths & Micro Frontends @DDDEurope
manfredsteyer
PRO
0
140
エージェンティックRAGにAWSで入門しよう!
har1101
9
1.9k
SREは、MCPとSRE Agentをこう使え!
kazumax55
0
150
AIエージェントで 変わるAndroid開発環境
takahirom
1
450
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
240
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
390
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.8k
How to Think Like a Performance Engineer
csswizardry
28
2.7k
Design in an AI World
tapps
1
260
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
380
Statistics for Hackers
jakevdp
799
230k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
500
Building Flexible Design Systems
yeseniaperezcruz
330
40k
GitHub's CSS Performance
jonrohan
1033
470k
What's in a price? How to price your products and services
michaelherold
247
13k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
870
Faster Mobile Websites
deanohume
310
32k
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